C program -- Average Calculator

Discussion in 'Programming General' started by Zeh Progage, Aug 14, 2007.

C program -- Average Calculator
  1. Unread #1 - Aug 14, 2007 at 5:20 PM
  2. Zeh Progage
    Referrals:
    0

    Zeh Progage Guest

    C program -- Average Calculator

    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;
    }
    
    This code simply allows the user to enter a number from 1 to 25 to determine how many numbers will be calculated when averaging the set. I hope you like! :)
     
  3. Unread #2 - Sep 3, 2007 at 8:16 AM
  4. The Supreme Intelligence
    Joined:
    Apr 29, 2007
    Posts:
    738
    Referrals:
    0
    Sythe Gold:
    0

    The Supreme Intelligence Apprentice
    Banned

    C program -- Average Calculator

    nice work :). i have several calculators from when i first started C. i made addition,subtraction,multiplication, and division calcs. they could only use 2 numbers at once. but it was very simple and 100% efficient. i actually still have them.

    just one comment on your comments, you have too many of them!
     
< I'm making a text based game... | help with c >

Users viewing this thread
1 guest


 
 
Adblock breaks this site