Displaying the total and average for an array input I'm currently taking my first class for c++ and our current lab is asking us to:


declare an array with a maximum of 100 integer
display total of all the scores
display the average of scores entered
have a negative as a sentinel since the scores all will be positive (the sentinel is -1)


afterwards We're then supposed to display the entered scores in descending order.

What I currently have is this:



#include &lt;iomanip&gt;
#include &lt;iostream&gt;
using namespace std;

void header();

int main()
{

    const int number = 100; // total number of allocated array inputs.
    int myArray[number], //array set
        counter = 0; // current array input number (1~100)
    double total = 0, // total for all scores added up
        average; // the average point for the scores.

    header(); // for calling title and description.

    cout &lt;&lt; "Please enter the test scores you recieved." &lt;&lt; endl;
    cout &lt;&lt; "Once all scores are in, enter -1 to end input" &lt;&lt; endl &lt;&lt; endl;


    while (cin &gt;&gt; myArray[counter] &amp;&amp; myArray[counter] != -1)
    {
         total += myArray[counter]; // adds all inputs for grand total.
         counter++;
        average = total / counter; // grand total / number of input
    }

    cout &lt;&lt; "The total for all the scores is " &lt;&lt; total &lt;&lt; endl;
    cout &lt;&lt; endl &lt;&lt; "Average score is " &lt;&lt; fixed &lt;&lt; average &lt;&lt; endl;


    cout &lt;&lt; endl &lt;&lt; "Program ending, shutting down." &lt;&lt; endl;
    return 0;
}

void header() // simple header for lab description.
{
    cout &lt;&lt; "***********************************" &lt;&lt; endl;
    cout &lt;&lt; "Lab 7:score array " &lt;&lt; endl;
    cout &lt;&lt; "Programmed by: ~~~ " &lt;&lt; endl;
    cout &lt;&lt; "This array will allow the user to enter up to 100 test scores," &lt;&lt; endl;
    cout &lt;&lt; "Show the total for the the inputted scores, and display the average score rounded." &lt;&lt; endl;
    cout &lt;&lt; "***********************************" &lt;&lt; endl &lt;&lt; endl;
}


It works for the most part, but think that the while loop is probably wrong.

Am I on the right track or is this a mess?
Avoid Using Namespace std

If you are coding professionally you probably should get out of the habit of using the using namespace std; statement. The code will more clearly define where cout and other identifiers are coming from (std::cin, std::cout). As you start using namespaces in your code it is better to identify where each function comes from because there may be function name collisions from different namespaces. The function cout you may override within your own classes. This stack overflow question discusses this in more detail.

Variable Naming

Use descriptive variable names so the code is more meaningful and comments are not as necessary, some examples

Current name: Suggested name
number      : maxTestScores
myArray     : testScores  

Not currently in code
endTestScoresList : -1 (make -1 a symbolic constant like number is).

Variable Declarations

A good programming habit to get into is to put each variable declaration in a separate statement on a separate line. Instead of 

    int myArray[number], //array set
            counter = 0; // current array input number (1~100)
    double total = 0, // total for all scores added up
            average; // the average point for the scores.


it would be better the following way because it is easier to add or delete a variable and it is easier to read and maintain the code.

    int myArray[number];
    int counter = 0;
    double total = 0;
    double average = 0;


In C++ it is also a good habit to initialize all the variables. 

Move Loop Invariants out of the Loop

In the following code the variable average is a loop invariant and should not be calculated within the loop. Optimizing compilers may do this for you, but it is better to remove things that don't belong in the loop.

    while (std::cin &gt;&gt; myArray[counter] &amp;&amp; myArray[counter] != -1)
    {
        total += myArray[counter]; // adds all inputs for grand total.
        counter++;
        average = total / counter; // grand total / number of input
    }


would be better as 

    while (std::cin &gt;&gt; myArray[counter] &amp;&amp; myArray[counter] != -1)
    {
        total += myArray[counter]; // adds all inputs for grand total.
        counter++;
    }

    average = total / counter; // grand total / number of input


Possible Bug

As was observed in another answer, if the user entered more than 100 test scores the previous code could throw an index out of range error and the program would crash. Rather than user a for loop as shown in the other answer, the code could be modified to test the index in the while loop:

    while ((counter &lt; number) &amp;&amp; std::cin &gt;&gt; myArray[counter] &amp;&amp; myArray[counter] != -1)
    {
        total += myArray[counter++]; // adds all inputs for grand total.
    }

    average = total / counter; // grand total / number of input


By putting the test (counter &lt; number) first the std::cin will not be called and the loop will exit, preventing the out of range problem.

Complexity

The function main() is too complex (does too much). As programs grow in size the use of main() should be limited to calling functions that parse the command line, calling functions that set up for processing, calling functions that execute the desired function of the program, and calling functions to clean up after the main portion of the program.

There is also a programming principle called the Single Responsibility Principle that applies here. The Single Responsibility Principle states:


  that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.


There are at least 3 possible additional functions that can be called from main().
  - Get the user input
  - Calculate and print the total and average
  - List the scores in decsending order  

By reducing the complexity the code becomes easier to read, write, modify and debug.

Prefer '\n' Over std::endl.

As mentioned in another review std::endl calls a system function to flush the output buffer. Sometimes this is needed, but generally to improve performance std::endl isn't called. It might be called after a loop of std::cout has been completed.