I am trying to calculate the average of the numbers the user entered but I can't figure out how many numbers the user actually entered to so I can divide by the sum. The user can enter up to 10 numbers or a zero to exit at anytime. I need to be able to calculate the average but not include zero.
Code:
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char theNums[10][50];
int nums[10];
int i, j, sum = 0;
int average, qty;
printf("Start by entering up to 10 numbers. To exit enter a zero.\n");
printf("\n");
for(i=0; i < 10; i++)
{
printf("Enter a Number: ");
fgets(theNums[i], 50, stdin);
j = strlen(theNums[i])-1;
theNums[i][j] = '\0'; /* get rid of '\n' */
if(theNums[i][0] == '0')
break;
else if(!isdigit(theNums[i][0]))
{
printf("%s is not a valid entry.\n", theNums[i]);
nums[i] = 0;
}
else
nums[i] = atoi(theNums[i]);
}
printf("\n\n");
for(j=0; j < i; j++)
sum+=nums[j];
average = (sum/qty);
printf("The sum of your numbers is %d", sum);
printf("\n");
printf("The average of the numbers is %d", average);
printf("\n")
getchar(); /*wait for keystroke before exiting*/
return 0;
}