Array Question

This is a discussion on Array Question within the C++ Programming forums, part of the General Programming Boards category; (For easier understanding, I simplified this problem* In short, I'm trying to write a program that has a function, getAverage(), ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    Question Array Question

    (For easier understanding, I simplified this problem*

    In short, I'm trying to write a program that has a function, getAverage(), which should calculate and return the average of the values stored in the array testscores[].

    I'm uncertain as to what to set as the 'for loop' conditions; I'd appreciate any help.
    Thank you in advance!

    Code:
    #include <iostream>
    using namespace std;
    
    double getAverage(int testscores[]);
    
    int main ()
    {
    
        int testscores[] = {100, 90, 95};
        
        cout << getAverage(testscores);
        
        system ("pause");
        return 0;
    }
    
    double getAverage(int testscores[])
    { 
        int sum = 0;
        
        int i;
        for (i = 0; ; i++) // what would I put in between the semicolons?
        {
             sum += testscores[i];
        }
        
        int average;
        average = sum/i;
    
        return average;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Pass the number of elements as another argument to the getAverage function, then you can use i < num_elements as the loop condition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    what? like this?: (It doesn't work?)

    Code:
    #include <iostream>
    using namespace std;
    
    double getAverage(int testscores[], int num_elements);
    
    int main ()
    {
        int num_elements;
        int testscores[num_elements] = {100, 90, 95};
        
        cout << getAverage(testscores);
        
        system ("pause");
        return 0;
    }
    
    double getAverage(int testscores[], int num_elements)
    { 
        int sum = 0;
        
        int i;
        for (i = 0; i < num_elements ; i++)
        {
             sum += testscores[i];
        }
        
        int average;
        average = sum/i;
        
        cout << "sum" << sum << "average" << average;
        return average;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    You forgot to change the place where the function is called.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Right. Regardless, it still won't work. The error message says "variable-sized object 'testscores' may not be initialized"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Oh, I missed that. Your main function should actually be:
    Code:
    int main()
    {
        int testscores[] = {100, 90, 95};
        
        cout << getAverage(testscores, sizeof(testscores) / sizeof(testscores[0]));
        
        system ("pause");
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Oh gosh, there was actually a predefined function all along..? (Fixed!)

    Thank you again, laserlight! You've been more than helpful

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by ieatcalculus
    there was actually a predefined function all along..?
    If you are talking about sizeof, then no, it is an operator, not a function. Take care when using it: a common mistake is to attempt to use it on a pointer to the first element of an array, in order to try and find out the size in bytes of the array, only to end up with the size in bytes of the pointer instead.

    You may want to use a std::vector or std::tr1::array container instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21