Thread: help with averagine array function

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    help with averagine array function

    hi people, i needed to write a program that would ask the user for numbers and store them in an array, entering -1 to stop entering numbers. then the program will use a function to find the average of the numbers in the array. the only thing bugging me is i cant get it to stop when you enter -1. thanks for any help or directions to the right way.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        
        int a[1000];
        int num;
        int total = 0;
        int answer;
        int i = 0;
        
        void calc_avg( int[] , int, int, int );
    
       
       while(i != -1) {
          printf( "Enter a rating between 0 and 100 " );
          scanf( "%d", &num );
          a[i] = num;
     }  
     
            calc_avg(a, num, total, answer);
            
       system("PAUSE");
          return 0;
          }
          
          void calc_avg( int a[], int num, int total, int answer ) {
             int i = 0;
             
             
             for (i = 0; i < 1000; i++) {
                 total += a[i];
                 }
                 
             answer = total / i;
             
             printf ("The average of the numbers was: %d", answer);
             }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       while(i != -1) {
          printf( "Enter a rating between 0 and 100 " );
          scanf( "%d", &num );
          a[i] = num;
     }
    At which point will i be -1?
    In fact, when does i change in this loop?

    Code:
          void calc_avg( int a[], int num, int total, int answer ) {
             int i = 0;
             
             
             for (i = 0; i < 1000; i++) {
                 total += a[i];
                 }
                 
             answer = total / i;
             
             printf ("The average of the numbers was: %d", answer);
             }
    Do you always have 1000 elements?

    You certainly don't need to pass a variable "total" or "answer" - declare them inside this function. "num" is not even used, so that could be completely omitted. Naturally, the relevant changes also need to be made in the code above for calling the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by matsp View Post
    Code:
       while(i != -1) {
          printf( "Enter a rating between 0 and 100 " );
          scanf( "%d", &num );
          a[i] = num;
     }
    At which point will i be -1?
    In fact, when does i change in this loop?

    Code:
          void calc_avg( int a[], int num, int total, int answer ) {
             int i = 0;
             
             
             for (i = 0; i < 1000; i++) {
                 total += a[i];
                 }
                 
             answer = total / i;
             
             printf ("The average of the numbers was: %d", answer);
             }
    Do you always have 1000 elements?

    You certainly don't need to pass a variable "total" or "answer" - declare them inside this function. "num" is not even used, so that could be completely omitted. Naturally, the relevant changes also need to be made in the code above for calling the function.

    --
    Mats
    Sure there is a big mistake because no matter what you want to do with the arguments in the funtion that calculates the avarage you must declare them as int *answer or int *total and in the function block to say:
    *total += a[i];
    and *answer = ................; //Whatever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM