The marks of student in 3 subjects for 3 examseach are stored in an array.The result needs to be calculated by first taking average score of three exams for each of the subject.Store the average result obtained in an array.If any of the three averages are less than 60,mention which subjects need a retake.
#include<bits/stdc++.h>
using namespace std;
void getSubjects(string data[],int x)
{
int averageSubjectScore;
int maxAvgScore = INT_MIN;
// List to store subject name
// having maximum average score
vector<string> subjects;
// Traversing the subject array
for (int i = 0; i < x; i += 4)
{
// Find average score of a subject and add to arary
averageSubjectScore = (stoi(data[i + 1]) + stoi(data[i + 2]) + stoi(data[i + 3])) / 3;
if (averageSubjectScore < 60)
{
subjects.push_back(data[i]);
}
}
//Display name of subject with < 60 average
for (int i = 0; i < subjects.size(); i++) {
cout <<subjects[i] + " ";
}
}
// Main program code
int main()
{
//List subject and score data as string array
string data[] = {
"Algebra", "20", "30","10",
"Science", "100", "50", "40",
"Geography", "100", "50", "40"
};
// Number of elements in string array
int n= sizeof(data)/sizeof(data[0]);
cout<<"You need to retake the following subject(s) with average below 60%: ";
getSubjects(data,n);
}
Comments
Leave a comment