Noob console programme question

Hi all.

Could someone please tell me why nGrade comes out as 0 every time?

Its as if im not inputting the values into the dynamic arrays in int main.

Thank u

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

#include <iostream>
using namespace std;

void Calc(int a_anScores[], int a_anPerc[], int a_nTotal){
	
	for( int iii = 0; iii < a_nTotal; ++iii){
		a_anScores[iii] = (a_anScores[iii] * (a_anPerc[iii]/100));
	}
	int nGrade=0;
	for(int jjj =0; jjj<a_nTotal; jjj++){
		nGrade += a_anScores[jjj];
	}
		
    cout << "Your total grade is:" << nGrade;

	
}


int main (){
		
	 int nTotal;
	 cout << "How many assessments do you have in your course: " << endl << endl;
	 cin >> nTotal;
	 int* panScores = new int[nTotal];
	 int* panPerc = new int[nTotal];
	 int jjj = 1;

	 for (int iii = 0; iii < nTotal; ++iii){
		 
		cout << "Please enter grade of assessment " << jjj++ << " and then its percentage: " << endl << endl;
		 cin >> panScores[iii];
		 cin >> panPerc[iii];
		 }
	 
	 Calc(panScores, panPerc, nTotal);
	
	 system("pause");
}
Last edited on
closed account (D80DSL3A)
It's happening at line 8. Integer division gives the quotient. The remainder is dropped.
For example: 8/5 = 1 . 3/5 = 0.
This quantity on line 8: a_anPerc[iii]/100 = 0 if a_anPerc[iii] is < 100. You will get better results using float or double type for panScores and panPerc instead of int.
Thanks! Changing those arrays to doubles as well as nGrade worked
Topic archived. No new replies allowed.