array parameter

hello! i would like to know how to get the average of the ratings with 2 decimal places. i have tried some that i saw in the net but it doesnt seem to 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[code]#include<iostream>
using namespace std;
//structure type declaration
struct studRec{
    int IDnum;
    int code;
    int mRating;
    int fRating;
    float fGrade;
    string remarks;
};

void inputRating(struct studRec s[], int n){
    int mRating;
    int fRating;
    string remarks;
    cout<<"Enter student's ratings (Seperated by a space)" <<endl; 
    cout<<endl;
    for(int j=0; j<n; j++){
    cout<<"CSC"<<(j+111)<<":\t"; cin>> s[j].mRating >> s[j].fRating;
     if(s[j].mRating<= 69){
        cout<< "You have entered an invalid Midterm Rating.\n\n";
    }
    if(s[j].mRating >= 101){
        cout<< "You have entered an invalid Midterm rating.\n\n";
    }
     if(s[j].fRating <= 69){
        cout<< "You have entered an invalid Final rating.\n\n";
    }
    if( s[j].fRating >= 101){
        cout<< "You have entered an invalid Final rating.\n\n";
    }
     
}
  
}

void displayTable(struct studRec s[], int n){
//displaying an array of struct
    cout<<endl;
    int mRating;
    int fRating;
    
        cout<<"# "<<"\t";
        cout<<"id"<<"\t\t";
        cout<<"code "<<"\t\t";
    	cout<<"Midterm "<<"\t";
    	cout<<"Rating "<<"\t\t";
    	cout<<"Rating "<<endl;
        for(int j=0; j<n; j++){
        cout<<(j+1)<<"\t";
        cout<<"2020-"<<(j+1001)<<"\t";
        cout<<"CSC"<<(j+111)<<"\t\t";
        cout<<s[j].mRating<<"\t\t";
        cout<<s[j].fRating<<endl;
        
    
    }
      
}

int main(){

    //declaring/initializing an array of struct
    int n=2;    
    struct studRec s[n];
    
    //inputting user values into fields
    inputRating(s, n);
                            
    //displaying an array of struct
    displayTable(s, n);
}
Last edited on
First,
* Lines 14-16 and 41-42 declare variables that you never use
* Line 66: You don't need the keyword 'struct' in declarations in C++. studRec s[n]; is enough
* Pedantically, the 'n' on line 65 should be constexpr, because the size of array (on line 66) must be a compile-time constant

You have only two student records, so their average would be trivial (a+b)/2.0.

Overall, average is sum of values divided by their count.
However, sum of integers divided by '2' -- an integer -- is also an integer.
Integer divided by 2.0 -- a double -- produces a double result.

Sum of values in array can be computed with std::accumulate http://www.cplusplus.com/reference/numeric/accumulate/
but you could as well use a loop.


One does not compute result "with 2 decimal places". Calculate with the precision of your data type (double).
One does show 2 decimal places:
http://www.cplusplus.com/reference/iomanip/setprecision/
It's easier to compute the average as part of the input.

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
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <utility>
#include <iomanip>
using namespace std;

struct studRec {
	//int IDnum {};
	//int code {};
	int mRating {};
	int fRating {};
	//float fGrade {};
	//string remarks;
};

 auto inputRating(studRec s[], size_t n) {
	double midav {}, finav {};

	cout << "Enter student's ratings (Midterm Final - separated by a space)\n";

	for (size_t j = 0; j < n; ++j) {
		cout << "CSC" << (j + 111) << ":\t";
		cin >> s[j].mRating >> s[j].fRating;

		if (s[j].mRating <= 69 || s[j].mRating >= 101)
			cout << "You have entered an invalid Midterm Rating.\n\n";

		if (s[j].fRating <= 69 || s[j].fRating >= 101)
			cout << "You have entered an invalid Final rating.\n\n";

		midav += s[j].mRating;
		finav += s[j].fRating;
	}

	return std::pair {midav / n, finav / n};	// Return the average
}

void displayTable(const studRec s[], size_t n) {
	cout << "\n# " << "\t";
	cout << "id" << "\t\t";
	cout << "code " << "\t\t";
	cout << "Midterm " << "\t";
	cout << "Final " << "\t\t";
	cout << "Rating\n";

	for (size_t j = 0; j < n; ++j) {
		cout << (j + 1) << "\t";
		cout << "2020-" << (j + 1001) << "\t";
		cout << "CSC" << (j + 111) << "\t\t";
		cout << s[j].mRating << "\t\t";
		cout << s[j].fRating << '\n';
	}
}

int main() {
	constexpr int n {2};
	studRec s[n];
	const auto [midav, finav] {inputRating(s, n)};

	displayTable(s, n);

	cout << "\nThe midterm average is " << fixed << setprecision(2) << midav;
	cout << "\nThe final average is " << fixed << setprecision(2) << finav << '\n';
}



Enter student's ratings (Midterm Final - separated by a space)
CSC111: 78 82
CSC112: 76 85

#       id              code            Midterm         Final           Rating
1       2020-1001       CSC111          78              82
2       2020-1002       CSC112          76              85

The midterm average is 77.00
The final average is 83.50

Last edited on
Topic archived. No new replies allowed.