I'm revising for an exam and we have been given some mock questions to practice on which be alike what is on the actual exam, but i'm stuck with one of them and have come no closer to figuring for a while now.
This is my complete 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>
using namespace std;
const int STUDENTNO(3);
const int MODULENO(4);
int main() {
void displayAllMarks(const double marks[][MODULENO]);
void displayModuleMarks(const double marks[][MODULENO], const string modules[], int modno);
int getValidStudentIndex();
void showStudentResults(const double marks[][MODULENO], int studIndex);
double marks[STUDENTNO][MODULENO] = { { 89.0, 22.5, 43.2, 34.0 },
{ 66.5, 26.0, 47.4, 0.0 },
{ 89.0, 30.8, 71.3, 12.0 } };
string modules[MODULENO] = { "Maths", "Sciences", "French", "English" };
cout << "\n\n---Display all marks: \n";
displayAllMarks(marks);
cout << "\n\n---Display marks for given module: \n";
displayModuleMarks(marks, modules, 2);
//LineA
int studIndex(getValidStudentIndex());
showStudentResults(marks, studIndex);
//lineB
cout << endl << endl;
system("pause");
return 0;
}
void displayAllMarks(const double marks[][MODULENO]) {
//show all marks
cout << "\n" << fixed << setprecision(2);
for (int stud(0); stud < STUDENTNO; ++stud)
{
for (int mod(0); mod < MODULENO; ++mod)
cout << marks[stud][mod] << "\t";
cout << "\n";
}
}
void displayModuleMarks(const double marks[][MODULENO],
const string modules[], int modno) {
//show marks for module
assert(modno >= 0 && modno < MODULENO); //valid module number
cout << "\n" << fixed << setprecision(2);
cout << "\nModule title: \t" << modules[modno];
for (int stud(0); stud < STUDENTNO; ++stud)
cout << "\nStudent no " << stud << ": \t" << marks[stud][modno] << "\t";
}
int getValidStudentIndex() {
cout << "\n\nEnter a student index number" << endl;
int studIndex = 0;
cin >> studIndex;
while (studIndex < 0 || studIndex >= 3) {
cout << "Please enter a valid student index number" << endl;
cin >> studIndex;
}
return (studIndex);
}
void showStudentResults(const double marks[][MODULENO], int studIndex) {
double average = 0;
double sum = 0;
assert(studIndex >= 0 && studIndex < STUDENTNO);
cout << "\n" << fixed << setprecision(2);
cout << "Results for student #" << studIndex << " are:" << "\nMarks: ";
for (int module(0); module < MODULENO; ++module) {
cout << marks[studIndex][module] << " ";
sum = sum + marks[studIndex][module];
}
average = sum / 4;
cout << "\nAverage: " << average << "%" << endl;
}
}
|
I need help solving this question
Write a C++ function called modulePassRate, which calculates the rate of pass marks (i.e., mark higher than 50.0) for a given module.
The function takes two parameters (which you will need to determine based on the example below).
It has a precondition to ensure that the module index number given is valid.
For example, in the following code placed as indicated in the main function (between the lines marked as //lineA and //lineB), the call to the modulePassRate function is used to display the percentage (to a precision of 2 decimal places) of pass marks for the module whose index value is 2.
cout << "\nPass rate for module #2 is "
<< fixed << setprecision(2)
<< modulePassRate(marks, 2) << "%";
Using the data provided in the appendix, it displays the following:
Pass rate for module #2 is 33.33%
I don't know how I meant to get back the values I need to for it to correctly work, I can't return anything by value and have can have only 2 parameters in the function. Any advice would be great.