Thread: averaging numbers?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    averaging numbers?

    any idea how i would take a group of numbers, entered in the comand window, and make it spit out the average? I JUST started C so im very newb atm.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by rock4christ
    any idea how i would take a group of numbers, entered in the comand window, and make it spit out the average?
    Yes.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Quote Originally Posted by kermit
    Yes.
    Could you help me whith it please?

    Code:
    #include <stdio.h>	
    
    int main()                            /* Most important part of the program!
    */
    {
        int age;                          /* Need a variable... */
      
        printf( "Please enter your age" );  /* Asks for age */
        scanf( "%d", &age );                 /* The input is put in age */
        if ( age < 100 ) {                  /* If the age is less than 100 */
         printf ("You are pretty young!\n" ); /* Just to show you it works... */
      }
      else if ( age == 100 ) {            /* I use else just to show an example */ 
         printf( "You are old\n" );       
      }
      else {
        printf( "You are really old\n" );     /* Executed if no other statement is
        */
      }
      return 0;
    }
    why cant i enter an age in the command window with this? i hit enter and type a # then hit enter again and it closes.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Is this your problem?

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by rock4christ
    any idea how i would take a group of numbers, entered in the comand window, and make it spit out the average? I JUST started C so im very newb atm.
    Hope this helps:

    Code:
    input limit
    
    sum=0
    count=0
    
    while (count<limit)
    {
    	read num
    	add num to sum
    	increment count
    }
    
    average = sum/count
    
    print average
    Also look up this excellent link (slide 4 onwards explains the algorithm and program): http://www.csse.monash.edu.au/course...ect/lect10.ppt
    Last edited by noodles; 09-08-2006 at 07:19 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    i actually remembered another way i was shown, that works on mine:
    at end of code
    getch ();
    return0;

    but could you help on the averaging question?

  7. #7
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    Hey,
    Enter your code within a while loop. You are getting only one age as input.
    insert this while loop.

    total = 0;
    count = 0;
    do
    {
    //your code here.

    count ++;
    total += age;
    printf("Do you want to continue(y/n) :");
    res = getc()
    }while( res == 'y' )

    average = total / count;

    Jag

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    75

    Arrow

    Quote Originally Posted by rock4christ
    but could you help on the averaging question?
    Read post#5 above and see the external link as well.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    getch() is non-standard. Meaning it may exist on your compiler but not on others. It's best for you to use a standard function like getchar() while you have one.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    HWJR?

    How would Jesus ROCK? HAHAHA You guys crack me up.

    When you're not creeping me out that is.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What I don't get: how's the code copied and pasted from a tutorial related to your problems? What does a program that decides if a person is pretty yound, old or very old have to do with averaging input?

    People like it if you show your efforts, not just any piece of code you can find.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    they were 2 seperate questions, i just didnt want to make another thread.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The way to approach this, and certainly more taxing programs, is to think how would you get the average of a sequence of numbers? Add up all the numbers, and divide by the number of 'em, right? So,

    Code:
    int NumOfNumbers = 0;
    int Total = 0;
    
    while ( You still wish to add numbers )
    {
      print - "do you want to add more numbers";
      get - answer
    
      if ( yes )
      {
        NumOfNumbers++;
        Get the number;
        Number += The Gotten Number;
      }
    }
    if ( NumOfNumbers isn't 0 )
    {
      average = Number/NumberOfNumbers;
    }
    That's not real C, obviously, but it puts you in the thinking mode about how to accomplish the task. (as #7 said!!!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. averaging random numbers
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-27-2001, 03:48 PM