Thread: Array Question

  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
    28,413
    Pass the number of elements as another argument to the getAverage function, then you can use i < num_elements as the loop condition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    You forgot to change the place where the function is called.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07: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, 06: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