Challenge

+1 J show · February 14, 2015
Write a program that reads alphanumeric characters from the keyboard, and computes the average ascii value of the alpha numeric characters, the average alphabetical character, the average numeric character and the average uppercase character. Outputting each, you program should terminate reading once it read a non-alphanumeric character.


How would you guys go about doing this problem?
using ctype.h and isalnum?

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 J show · February 16, 2015
Okay i've kind of took this and changed it up a bit haha. But it still wont run!!!

#include 
#include

int main(void)
{

int value;
int digit_loop = 0;
int alpha_loop = 0;
int upper_loop = 0;
int loop = 1;

float digit=0;
float alpha=0;
float upper= 0;
int total;
int x;


while (loop==1){
printf("Enter a alpha numeric character(enter a non aplha numeric character to stop):");
x = scanf("%d", &value);
printf("inside while before if %d\n",x);

//printf("%d", x);

if (x == 1){
printf("inside if %d\n",x);
digit += value;
digit_loop++;
}
else{
printf("inside else %c\n",value);

if (value >= 65 && value
0 J show · February 16, 2015
alright i have a question now. So get char is getting values and characters. But i think need my while loop to stop whenever i enter a punctuation. Like ! or # $. And scanf wouldnt work for this one? 
0 J show · February 16, 2015
Thanks a lot!
0 Lazar Bulic · February 16, 2015
Sorry, I forgot about that. 
Just correct the while loop like this: 

while((ch = getchar()) != '\n'){
        if(isalpha((char)ch)){
            if(isupper((char)ch)){
                coun1++;
                sum1 += ch;
            }else{
            coun2++;
            sum2 += ch;
            }
        }else{
            if(isdigit((char)ch)){
                coun3++;
                sum3 += ch;
            }else
                break;
        }
    }


If you can't understand any part of my cod please ask? Also when there are no numbers and caps in the input the printf fill be strage as division by 0 isn't defined but that can be fixed with couple of if statements.
0 J show · February 16, 2015
"you program should terminate reading once it read a non-alphanumeric character"

Also i need it to do this. The program you wrote i think just terminates when you press enter
0 J show · February 16, 2015
I think i was just getting spun around doing it cause im new to the while look. But i know what average is i just must have not been thinking. Average is the sum of the inputs values divided the number of inputs. But thanks for your help!
0 Lazar Bulic · February 15, 2015
Please revise how you do the average. You have problem with math. Instead of correcting your code its easier to write new. The main problem is that you don't know how to do average which has nothing to do with programming.
http://pastebin.com/QhYTn30Y
0 J show · February 15, 2015
#include <stdio.h>
#include <ctype.h>

int main(void)
{

int value = 0;
int digit_loop = 0;
int alpha_loop = 0;
int upper_loop = 0;

int digit=0;
int alpha=0;
int upper= 0;
int total;


while (isalnum(value)){
printf("Enter a alpha numeric character(enter a non aplha numeric character to stop):");
scanf_s("%d", &value);


if (isdigit(value)){
         digit += value;
         digit_loop++;
}
if (isalpha(value)){
         alpha += value;
         alpha_loop++;
       if (isupper(value)){
                upper += value;
                upper_loop++;
             }
      }



total = digit + alpha + upper;

printf("The average ascii value of the alpha numeric characters is %d.\n", (alpha + digit) / (alpha_loop + digit_loop));
printf("The average numeric character is %d.\n", digit / digit_loop);
printf("The average alpha character is %c.\n", alpha / alpha_loop);
printf("The average upper case character is %c.\n", upper / upper_loop);


return 0;

}




Alright i made a few changes again! Idk why its not working?
0 J show · February 15, 2015
Alright thanks! i'll play around with it and see if i can get it to work. 
0 Lazar Bulic · February 14, 2015
Well i though you would figure it out but here it goes:

Try just using while in this case, you don't need do while.

Please don't use array here, it is possible but it isn't necessary. Also you shouldn't divide them all with loop. If you are looking for
 the average ASCII value of the alpha numeric characters you then dived it with loop as its all characters but in ohter cases you need a counter for every. (one for number, one for alpha, one for Uppercase) 


Also I forgot to ask, "the average alphabetical character", "average numeric character" "the average uppercase character" Is this average ASCII value of these?

 

C

128,089 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator