60x7 array for sudents grades

I'm having an extremely hard time with my code. I must Write a program that can be used to determine grades at the end of the semester.
For each student, who is identified by an integer number between 1 and 60, four examination grades
must be kept. Additionally, two final grade averages must be computed. The first grade average is
simply the average of all four grades. The second grade average is computed by weighting the four
grades as follows: the first grade gets a weight of 0.2, the second grade gets a weight of 0.3, the third
grade a weight of 0.3, and the fourth grade a weight of 0.2; that is computed as:
0.2 * grade1 + 0.3 * grade2+ 0.3* grade3 + 0.2 * grade4
Using this information, you are to construct a 60 X 7 two dimensional array, in which the first column
is used for the student number, the next four columns for the grades, and the last two columns for the
computed final grades. The output of the program should be a display of the data in a completed array.
Im having a hard time trying to assign the test grades to the table and dont know to to computed up to 60 students test grades.
Plase any help will be appreciated.
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
  #include <iostream> 
using namespace std;
int test1;
int test2;
int test3;
int test4;
void printarray(int[][60]);


int main()
{
	
	cout << "Enter grade for test one: ";
	cin >> test1;
	cout << "Enter grade for test two: ";
	cin >> test2;
	cout << "Enter grade for test three: ";
	cin >> test3;
	cout << "Enter grade for test four: ";
	cin >> test4;
	cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;
	int a[60][7], row, col;
	for (row = 0; row < 60; row++)
		for (col = 0; col < 7; col++)
			cin >> a[5][6];


	while (test1 != -1)
	{
		cin >> test1;
	}
	

	cin.get();
	return 0;
	
}
......
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;
void printarray(float  a[][7]);


int main()
{
	float a[60][7];
	int row, col;
	cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;
	
	for (row = 0; row < 60; row++)
	{
		a[row][0] = row;
		cout << "Enter grade for test one: ";
		cin >> a[row][1];
		cout << "Enter grade for test two: ";
		cin >> a[row][2];
		cout << "Enter grade for test three: ";
		cin >> a[row][3];
		cout << "Enter grade for test four: ";
		cin >> a[row][4];

		a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
		a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;
	}

	printarray(a);

	cin.get();
	return 0;

}

void printarray(float a[][7]) {
	for (int i = 0; i < 60; i++) 
	{
		for (int j = 0; j < 7; j++)
			cout << a[i][j] << "\t";
		cout << endl;
	}
}
what would you suggest I use if the user doesn't want to calculate all 60 students? they can calculate up to 60 students, but if they wanted to break out and calculate a certain number of students what would you suggest I use?
Make another variable
1
2
int studentNum = 20;
a[studentNum][7];
thank you so much. Another question how would I construct the code in a while loop?
Something like this
1
2
3
4
5
6
7
row = 0;
while(row < studentNum){
//...
//same code
//...
row++;
}
Last edited on

}


[/code]
Last edited on
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
49
50
51
#include <iostream>

using namespace std;
void printarray(float  a[][7], int num);


int main()
{
	int studentNum;
	float a[60][7];
	int row, col;
	cout << "How many students do you want to add?" << endl;

	cin >> studentNum;

	cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;

	row = 0;
	while (row < studentNum) {

		a[row][0] = row;
		cout << "Enter grade for test one: ";
		cin >> a[row][1];
		cout << "Enter grade for test two: ";
		cin >> a[row][2];
		cout << "Enter grade for test three: ";
		cin >> a[row][3];
		cout << "Enter grade for test four: ";
		cin >> a[row][4];

		a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
		a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;

		row++;
	}

	printarray(a, studentNum);

	cin.get();
	return 0;

}

void printarray(float a[][7], int num) {
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < 7; j++)
			cout << a[i][j] << "\t";
		cout << endl;
	}
}
Last edited on
You don't have to use 2 different loops just use one of them.

Use for( ; ; ){} when you know exact number of iterations that you need

Use while(){} when you do not know ahead of time the number of iterations that you need to do.

But you can imitate for loop with while loop like i showed you before:
1
2
3
4
5
6
int i = 0;
while(i < 5)
{
    //...
    i++;
}
Topic archived. No new replies allowed.