Arrays in computing averages

Ok i am right now working on a homework due tonight and need some help in figuring out how to take my grade averages and make them put in average.
The end product needs to take 10 numbers and average them. Can anyone help/ explain what is needed to create an average. I have spent time trying to figure it out but I am confused.
Final should do something like this:
Grade 1: 90
Grade 2: 86
Grade 3: 95
Grade 4: 76
Grade 5: 92
Grade 6: 83
Grade 7: 100
Grade 8: 87
Grade 9: 91
Grade 10: 0
Average Grade: 80%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>    // for CIN and COUT
#include <cassert>     // for ASSERTs
using namespace std;

/**********************************************************************
 * main(): Simple demo program for looping through an array
 ***********************************************************************/
int main()
{
   // Declare the variables
   const int SIZE = 10;                   // this is a CONST so we are safe
   int listDestination[SIZE];             // copy data to here
   int listSource[SIZE];                  // copy data from here
   assert(SIZE == sizeof(listSource) / sizeof(listSource[0]));
   assert(SIZE == sizeof(listDestination) / sizeof(listDestination[0]));

   // Fill the list
   
   for (int i = 0; i < SIZE; i++)
   {
      assert(i >= 0 && i < SIZE);         // yes, I am paranoid
      cout << "Grade " << i + 1 << ": ";    // humans start counting at 1
      cin  >> listSource[i];              // computers start at 0
   }

   // Copy the list
   for (int i = 0; i < SIZE; i++)         // same loop we use for all arrays
   {
      assert(i >= 0 && i < SIZE);         // very, very paranoid
      listDestination[i] = listSource[i]; // copy one at a time
   }

   // Display the results
   cout << "Average Grade: "
        << listDestination[0];            // display the first item
   for (int i = 1; i < SIZE; i++)         // standard loop skipping the 1st
   {
      assert(i > 0 && i < SIZE);          // did I mention I am paranoid?
      cout << ", " << listDestination[i]; // all the rest have a , before
   }
   cout << endl;

   return 0;
}
Last edited on
Ok, what about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main() 
{
    const int SIZE = 10;

    int Grades[SIZE];  //*Why two arrays and copying?
    int average; //*this will store our avarage

    average =0;

    for (int i = 0; i < SIZE; i++)
    {
         cin >> Grades[i];
         average = average + Grades[i]; //*accumulates the total
     }

     average = average / SIZE; //*calculates the average value Total/SIZE

     cout << average; //*prints the average
 
     return(0);

}


Note that to just calculate the average value, all we would need is to accumulate, the array is never really used. Its just storing the values.
Of course afterwards you can be paranoid as much as you want :)


http://cpp.sh/8y2a
Last edited on
so how would that look or how would i put this in the final code here is what i got
It compiles but doesnt solve the question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>    // for CIN and COUT
#include <cassert>     // for ASSERTs
using namespace std;

/**********************************************************************
 * main(): Simple demo program for looping through an array
 ***********************************************************************/
int main()
{
   // Declare the variables
   const int SIZE = 10;                   // this is a CONST so we are safe
   int listDestination[SIZE];             // copy data to here
   int listSource[SIZE];                  // copy data from here
   assert(SIZE == sizeof(listSource) / sizeof(listSource[0]));
   assert(SIZE == sizeof(listDestination) / sizeof(listDestination[0]));

   // Fill the list
   
   for (int i = 0; i < SIZE; i++)
   {
      assert(i >= 0 && i < SIZE);         // yes, I am paranoid
      cout << "Grade " << i + 1 << ": ";    // humans start counting at 1
      cin  >> listSource[i];              // computers start at 0
   }

   


    int Grades[SIZE];  //*Why two arrays and copying?
    int average; //*this will store our avarage

    average =0;

    for (int i = 0; i < SIZE; i++)
    {
         cin >> Grades[i];
         average = average + Grades[i]; //*accumulates the total
     }

     average = average / SIZE; //*calculates the average value Total/SIZE

     cout << average; //*prints the average
 

   return 0;
}

Last edited on
ok i think i get it now let me come back in a few
Just tested the code I sent you, it works on the shell.

Basicly what the code does is filling up an array and accumulating the values on the variable "average".

Once you have the sum of all values, all you need to do is to divide by the number of grades (SIZE) and, you are done.

The array is unnecessary, its there because I assumed its part of the home work.

Anyways, I am sure you can figure how to add text and everything from your previous code, good luck!
Ok the percentage isn't working take a look. It displays a weird final answer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
   const int SIZE = 10;                   // this is a CONST so we are safe
   int listDestination[SIZE];             // copy data to here
   int listSource[SIZE];                  // copy data from here
   int Grades[SIZE];  //*Why two arrays and copying?
   int average = 0; //*this will store our avarage
   
   
   for (int i = 0; i < SIZE; i++)
   {
      
      cout << "Grade " << i + 1 << ": ";    // humans start counting at 1
      cin  >> listSource[i];              // computers start at 0
   }

   

   for (int i = 0; i < SIZE; i++)
   {
      //cin >> Grades[i];
      average = average + Grades[i]; //*accumulates the total
   }

   average = average / SIZE; //*calculates the average value Total/SIZE

   cout << average;
   cout << endl; //*prints the average

   return 0;

}

ok i solved it thanks for the help SOLVED! :)
Topic archived. No new replies allowed.