Thread: Calculating an average

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Calculating an average

    Hey,

    I am new to C and need to make a console app that allows a user to enter as many payments as they like and when a command say "STOP" is entered it calculates the average of the numbers they entered. Could some tell me the best way to achieve this....Would it be with an array, loop or something else.

    P.S I have a couple of manuals but am not sure what I should be looking for.

    Thanks

    Andy

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you only need the average, you could keep a running total and a count of the number of items and divide the running total by the number of items when they've finished inputting the numbers.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    1
    U can use FOR command !
    Ex:
    Code:
    For(i=0;i<n;,i++)
    {
       printf("type %d number:",i);
       scanf("%d",&i);
    }
       sum=0
       for(i=0;i<n;i++)
       sum = sum + i;
    
       printf("Avarage is %d",sum/n);
    That is simple code !

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by duochon
    U can use FOR command !
    Ex:
    Code:
    For(i=0;i<n;,i++)
    {
       printf("type %d number:",i);
       scanf("%d",&i);
    }
       sum=0
       for(i=0;i<n;i++)
       sum = sum + i;
    
       printf("Avarage is %d",sum/n);
    That is simple code !

    First off, it's for, not For. Secondly, why would you read into your control variable in a count control loop?
    Sent from my iPadŽ

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    To the OP, please completely ignore duochon's post, in case you're tempted not to. If they actually tested the code they gave, they'd not have posted it.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Yeah, ditto what cwr and sly said. Don't bother reading his post. But do try using a for loop, then show us what you come up with and we'll be happy to help you work through the problem.
    -Crazed

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's tough to flat out say, "Use a for loop", especially with the way he want to sum. If you're talking about an undefined amount of user input, then a while or do while loop would suit you better.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Oop my bad. You're right Sly it would be best to use a while loop for that, but, it would still be possible to do it using an infinite loop with a break statement.
    -Crazed

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Try this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    int average;
    int counter = 1;
    int total = 0;
    int num;
    
    while ( counter <= 25 )
    {
    printf("Enter an amount: ");
    
    scanf("%d", &num);
    
    total = total + num;
    
    counter++;
    }
    
    average = total / 10;
    
    printf("\nThe average is %d, average);
    
    getchar();
    
    return 0;
    }

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    EDIT

    Sorry, last line of code should read:

    Code:
    printf("\nThe average is %d", average);

  11. #11
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    That's not what he asked:
    when a command say "STOP" is entered
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah seriously, and if you were just going to write a count controlled while loop, I would have said to use a for loop.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. calculating average
    By coolca in forum C Programming
    Replies: 1
    Last Post: 10-11-2006, 05:55 PM
  4. calculating average
    By Sharlene in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2003, 03:10 PM
  5. Problem calculating average with "for"...
    By davidvoyage200 in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2002, 10:02 AM