Arrays and Averages

Okay, getting towards the end of the semester and this self study course has been interesting to say the least.

This weeks issue deals with Arrays and Averages.

I'll give the simplified version since I may have more questions later.

I need to create some code that will create and array and then create a loop which will assign numbers to the array. I'm using student gpa's in my example. Once the numbers are input into the array, we then are supposed to average all the gpa's. I'm trying out some code which will just add the first two inputs so I know I'm on the right track.

Can you tell me what I need to change or add so the code will SUM the two elements from the array.

For example,

Let's say I enter in the following 5 numbers once we start the code.

10
20
30
40
50

I want this to add the 10 & 20 and COUT 30. What am I doing wrong here? Why isn't the sample(0) and sample(1) adding up? Thanks in advance.

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
#include <iostream>
using namespace std;
int main()
{
 int sample[5];
 int gpa;
 int total;
 
	 for (gpa = 0; gpa < 5; gpa = gpa++)
 				
do
{	 
	//enter input items
	cout << "Please enter a GPA." << endl;
	cin >> gpa;
	sample[gpa] = (gpa +1);

	}while (sample[gpa] != 5);	   

	 for (sample[gpa] = 0; sample[gpa] < 5; sample[gpa]++)
 		cout << sample[gpa] << " ";


//  TEST AT SUM OF FIRST TWO SAMPLES
 		
total = (sample[0] + sample[1]); 

cout<< " The sum is" <<total <<endl;
		
		 cout << endl << "Finished!" << endl;
 return 0;
}
And I just realized this doesn't stop when 5 entries happen. What is missing for that?

Can someone explain it or help me understand it rather than just giving an answer please?
Last edited on
closed account (z05DSL3A)
Have a look over this code:
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
#include <iostream>
using namespace std;
int main()
{
    int sample[5];
    int gpa;
    int total;
 
    for (int i = 0; i < 5; i++)
    {		
        //enter input items
        cout << "Please enter a GPA." << endl;
        cin >> gpa;
        sample[i] = gpa; 
    }

    int sum =0;
    for(int i = 0; i < 5; i++)
    {
        cout << sample[i] << " ";
        sum += sample[i];
    }

    cout<< " The sum is " << sum <<endl;
		
    cout << endl << "Finished!" << endl;
    
    return 0;
}
Last edited on
Thanks Wolf.

I broke everything down and looked at it carefully and added comments to help me understand.

I've added the avg part which works great when you just add a single digit but when I add something that inlcudes a decimal it doesn't work properly.

I thought making the sum and avg variables double would help. I guess not.

For example, if I enter in a 3.25 GPA as the first cin I get the following:

1
2
3
4
5
6
7
8
9
Please enter a GPA.
3.25
Please enter a GPA.
Please enter a GPA.
Please enter a GPA.
Please enter a GPA.

3 3 3 3 3 The sum is 15
The avg is 3



Here is my current code:

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
#include <iostream>
using namespace std;
int main()
{
    int sample[5]; // create sample with 5 elements
    int gpa;	// set up integer called gpa	
    int total;  // set up integer called total
 
 
 	
    for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
	
	// 1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once. 
	// 2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). 
	// 3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }. 
	// 4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2. 
	
    {		
        cout << "Please enter a GPA." << endl;  
        cin >> gpa;	//enter gpa
        sample[i] = gpa; // all the elements in the sample[i] are called gpa
    }

    double sum = 0.00; // set up double called sum
	double avg = 0.00;
	
    for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
    {
        cout << sample[i] << " ";
        sum += sample[i]; //sum = sum + sample[i]
		avg = sum/5;
    }

    cout<< " The sum is " << sum <<endl;
	cout<< " The avg is " << avg <<endl;
		
    cout << endl << "Finished!" << endl;
    
    return 0;
}
closed account (z05DSL3A)
Make your variables doubles if you want decimals.

1
2
3
4
    double sample[5]; // create sample with 5 elements
    double gpa;	// set up integer called gpa	
    double total;      // set up integer called total
Thanks Grey Wolf. Hopefully this is my last question...

What's the code to force the output to show two decimals? Even if someone enters a 2 I want it to show as 2.00 in the final comparison. I looked through the tutorials but didn't see it and remember something that forces or converts the variable to show a set number of places.

Here's what I got now. Thanks...

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
#include <iostream>
using namespace std;
int main()
{
    double sample[5]; // create sample with 5 elements
    double gpa = 0.0;	 // set up double called gpa	
    double total = 0.0;  // set up double called total
 
 
 	
    for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
	
		
    {		
        cout << "Please enter a GPA." << endl;  
        cin >> gpa;	//enter gpa
        sample[i] = gpa; // all the elements in the sample[i] are called gpa
    }

    double sum = 0.0; // set up double called sum
	double avg = 0.0;
	
    for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
    {
        // cout << sample[i] << " ";  THIS WOULD SHOW ALL THE NUMBERS ENTERED
        sum += sample[i]; //sum = sum + sample[i]
		avg = sum/5;
    }

    cout<< "The average GPA is " << avg <<endl<<endl;
	
	for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
	
	if (sample[i] > avg)
  		cout << sample[i] <<" is above average" << endl;
	else if (sample[i] < avg)
  		cout << sample[i] <<" is less than average" << endl;
	else
  		cout << sample[i] <<" is equal to the average" << endl;
	
	
	return 0;
}
You can use
1
2
cout.precision(2);
cout<< "The average GPA is " << avg <<endl<<endl;

or you can use
cout<< setprecision(2) << "The average GPA is " << avg <<endl<<endl;

I would personally prefer the former if I wanted it done for every case where I use cout. However, I would use the latter if I only wanted to output it for two decimal places once. You can find it in <iomanip> if it isn't already included.
That didn't quite work, but that's the code I was thinking of. I'll play around with it and do some research. Thanks.
Okay, it's not working. If I enter in a 1 as the GPA the cout give a 1 instead of a 1.00 like I want it to. Where am I going wrong? I thought the setprecision would work. Here is my code...

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
47
48
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double sample[5]; // create sample with 5 elements
    double gpa = 0.00;	  // set up double called gpa	 
    double total = 0.00;  // set up double called total
 
 
 	
    for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
	
	// 1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once. 
	// 2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). 
	// 3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }. 
	// 4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2. 
	
    {		
        cout << "Please enter a GPA." << endl;  
        cin >> gpa;	//enter gpa
        sample[i] = gpa; // all the elements in the sample[i] are called gpa
    }

    double sum = 0.00; // set up double called sum
	double avg = 0.00;
	
    for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
    {
        // cout << sample[i] << " ";  THIS WOULD SHOW ALL THE NUMBERS ENTERED
        sum += sample[i]; //sum = sum + sample[i]
		avg = sum/5;
    }
	
    cout << "The average GPA is " << setprecision (3) << avg << endl <<endl;
	
	for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
	
	if (sample[i] > avg)
		cout << setprecision (3) << sample[i] <<" is above average" << endl;
	else if (sample[i] < avg)
  		cout << setprecision (3) << sample[i] <<" is less than average" << endl;
	else
  		cout << setprecision (3) << sample[i] <<" is equal to the average" << endl;
	
	
	return 0;
}
floatfield needs to be changed to fixed point (I should have noted that in my previous post):
cout << fixed << setprecision (3) << samples[i] <<" is above average" << endl;
for example.
Thanks everyone. This works great now and I actually learned something.
Topic archived. No new replies allowed.