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 3
Oldest  Newest  Rating
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
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)
{

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.

C

124,420 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator