Z
Zeh Progage
Guest
Code:
#include <stdio.h>
#include <stdlib.h>
/*
* Name: Average Calculator
* Author: Zeh Progage
* Date: 14/08/07 16:16
* Description: Calculates the average of a series of numbers.
*/
int main(int argc, char *argv[]){
int average[24]; /* average[] holds the numbers that will be used when calculating */
int number = 0; /* number holds the value a user enters when deciding how many integers will be used when calculating */
int i = 0; /* index integer, used in both a for loop and when handling the average[] array */
int j = 0; /* Another index, same as above */
int add = 0; /* Adds the numbers together; used in the calculation */
float averaged = 0.0; /* Divides add into number, which, mathematically, should determine the average of all the numbers */
printf("This program will allow you to average a series of numbers\n");
printf("However, this program will only allow you to do up to 25 numbers at a time, any higher is unreasonable, in my opinion\n");
printf("Enter the amount of numbers you'd like to have averaged (max. 25): ");
scanf("%d", &number);
fflush(stdin);
if(number > 25){ /* If the user enters a number over 25, number will be reinitiaalized to 25 */
printf("Since you've entered a number over 25, I'll reinitialize the number to be 25.");
number = 25;
}
for(i = 0;i < number;i++){ /* for loop to determine the values stored within average[] */
printf("Enter the number: ");
scanf("%d", &average[i]);
fflush(stdin);
}
for(j = 0;j < number;j++) /* for loop to add the values stored in average[] together and put them in add */
add += average[j];
averaged = (float)add / (float)number; /* Makes add and number into floats to calculate the average of the numbers and put them in averaged */
printf("The average of all of the numbers you have entered is %.1f\n", averaged);
/* Calculates to the first decimal place the average of the numbers entered. */
getchar();
return 0;
}

