Class Average

Hello. Our teacher has finished the loop exercice yesturday and he gave us directly an exercice to solve it.

We need to calculate the average of all the class. Solve this problem in 2 ways: using for loop and using while loop.



I don't know from where to start, Any idea? I need to get the number of student? and what to do next?

we're still new to loop I asked my friends and they couldn't solve it

thank you guys, this forum is my only hope to continue learning C++.

My result so far taking 5 students only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
	int i, t1, t2, ava;
	for (i =1 ; i < 5 ; i++)
	{
		cout <<"Please enter your Test1:\n\n";
		cin >> t1;
        cout <<"\n\nPlease enter your Test2:";
		cin >> t2;
	}
	getch();
}


but the Problem is i need to show the avarage after each calculation.

Thanks
Average =   Σ marks  
          # of marks

You need the loop to evaluate the sum of the marks
I added the ava

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void main()
{
	int i, t1, t2, ava;
	for (i =1 ; i < 5 ; i++)
	{
		cout <<"\nPlease enter your Test1:\n\n";
		cin >> t1;
        cout <<"\n\nPlease enter your Test2:\n\n";
		cin >> t2;
		ava = (t1+t2)/2;
	}cout <<"Your Grade is : " << ava;
	getch();
}


How can i get the ava after each calculation ? because it's not showing 1 time only
Last edited on
Finally i made this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
	int i, t1, t2, ava;
	for (i =1 ; i < 42 ; i++)
	{
		cout << "Student " << i << " out of 42, Please enter your Test1: ";
		cin >> t1;
		cout << "\n\nPlease enter your test2 :";
		cin >> t2;
		cout << "\n\n";
	    ava = (t1+t2)/2;
       

	}cout <<"the Grade of the Class is : " << ava;
	getch();
}


what do you think :D ?
Firstly, main must return something and therefore cannot be void! So change line 6 toint main(). Your last line must also be return 0;

Secondly, don't use getch(). Refer to here for better methods: http://www.cplusplus.com/forum/articles/7312/

Lastly, all you need to do is put your cout statement in side your for loop, after the ava calculation.
ava = (t1+t2)/2; Not right: you need a variable which will contain the sum of all values, then divide it by the number of values -after the loop-
Topic archived. No new replies allowed.