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 Lazar Bulic · February 17, 2015
Yup that is the solution, gj man :)
0 J show · February 14, 2015
#include <stdio.h>
#include <ctype.h>

int main(void)
{

char value;
char alphaNum[100];
char digit[100];
char alpha[100];
char upper[100]
char total;


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


if (isdigit(value)){
digit[value];
}
if (isalpha(value)){
alpha[value];
}
if (isupper(value)){
upper[value]
}

} while (isalpha(value));

total = alphaNum + digit + alpha + upper;

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


return 0;

}


I've got this so far but this is just a wild try.
0 Lazar Bulic · February 14, 2015
I didn't test your code but i wouldn't do the same declaration. I would do:
int value;

and scanf
scanf("%c", &value);


I am expecting c student or some else to tell me this isn't correct but it works in practice. 



 / spoiler / merge scanf and while for the ultimate result.


How do you do the average of  anything? Check your code! I will give you an example for average of sum of digits in a number. http://pastebin.com/25mpFaCa

I hope this helps!

 


 
0 J show · February 14, 2015
#include <stdio.h>
#include <ctype.h>

int main(void)
{

int value;
int loop = 0;
char alphaNum[100];
char digit[100];
char alpha[100];
char upper[100]
char total;


do{
printf("Enter a alpha numeric character(enter a non aplha numeric character to stop):");
scanf("%c", &value);


if (isdigit(value)){
digit[value];
}
if (isalpha(value)){
alpha[value];
}
if (isupper(value)){
upper[value]
}

loop++;

} while (isalpha(value));

total = alphaNum + digit + alpha + upper;

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


return 0;

}


Okay i made a few changes but should i store the values in arrays or should i just say like alpha+=value, digit+=value, and upper+=value and then take the averages of those?
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?

 
0 J show · February 15, 2015
Alright thanks! i'll play around with it and see if i can get it to work. 
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 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 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 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

C

106,938 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator