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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std; //learned that this always has to come before prototypes or else it tells you that everything is an ambigious symbol
const int numScores = 3; //const int 3 to be used in arrays and in the main body of the file.
//prototype functions
int averageScorePerStudent(int[], int);
int maxScorePerAssignIndex(int[][numScores], int, int);
int overallAverage(int[][numScores], int);
vector <string> lowerThanAverageList(int[][numScores], int, string[]);
int maxScoreIndexPerStudent(int[], int);
int main()
{
const int Size = 15;
int choice, scores[Size][numScores]; //scores array with 15 and 3
int IndexCall, MaxScoreCall, MaxIndex, currentAvg, choice2; //these are placeholders for the function calls
string name, names[Size]; //string array names with 15
vector<string> UnderAvg;
ifstream infile;
infile.open("studentScores.txt");
int i = 0;
while (i < Size && infile >> names[i])
{
for (int j = 0; j < numScores; j++) //using numScores instead of three to be easier to change later
infile >> scores[i][j];
i++;
}
infile.close();
do .
{
cout << "1. Display a student’s scores as well their average score: " << endl;
cout << "2. What is the name of the student with the highest average score?" << endl;
cout << "3. What is the name of the student with the highest score on a certain assignment:" << endl;
cout << "4. Display the list of the students whose average score is lower than the overall average score." << endl;
cout << "5. Display the names of all students, their max score, and the assignment that they made their maximum score on " << endl;
cout << "6. Exit " << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Please enter the student's last name: " << endl;
cin >> name;
for (int i = 0; i < Size; i++)
{
if (names[i] == name)
cout << names[i] << " average score : " << averageScorePerStudent(scores[i], numScores) << endl;
}
cout << "This is not a valid student name, please try again." << endl; // tried putting this with an else statement under the if statement, but would run continously and still show up even if the if statement is true.
break;
case 2:
MaxIndex = 0;
MaxScoreCall = averageScorePerStudent(scores[0], numScores);
for (int i = 0; i < Size; i++)
{
currentAvg = averageScorePerStudent(scores[i], numScores);
if (currentAvg > MaxScoreCall)
{
MaxScoreCall = currentAvg;
MaxIndex = i;
}
}
cout << "Student with the highest average score: " << names[MaxIndex] << " - " << MaxScoreCall << endl;
break;
case 3: //had dificulties not allowing the user to go beyond assignment 3.
cout << "Please enter the assignment # (1-3) " << endl;
cin >> choice2;
IndexCall = maxScorePerAssignIndex(scores, Size, choice2);
cout << "Student " << names[IndexCall] << " has made the highest score of " << scores[IndexCall][choice2] << " on assignment # " << choice2 << endl;
break;
case 4:
UnderAvg = lowerThanAverageList(scores, Size, names);
cout << "Students with average score less than overall average: " << endl;
for (auto& names : UnderAvg)
cout << names << endl;
cout << endl;
break;
case 5:
for (int i = 0; i < Size; i++)
{
IndexCall = maxScoreIndexPerStudent(scores[i], numScores);
cout << names[i] << " Max Score: " << scores[i][IndexCall] << " Assignment# " << IndexCall + 1 << endl; //adding plus one because it starts at 0 if not
}
cout << endl;
break;
}
} while (choice != 6);
cout << "Have a good day!" << endl;
return 0;
}
int averageScorePerStudent(int scores[], int num)
{
int total = 0;
for (int i = 0; i < num; i++)
total += scores[i];
return total / num;
}
int maxScorePerAssignIndex(int scores[][numScores], int num, int num2)
{
int MaxScore = scores[0][num2];
int MaxIndex = 0;
for (int i = 0; i < num; i++)
{
if (scores[i][num2] > MaxScore)
{
MaxScore = scores[i][num2];
MaxIndex = i;
}
}
return MaxIndex;
}
int overallAverage(int scores[][numScores], int num)
{
int total = 0;
for (int i = 0; i < num; i++)
{
total += averageScorePerStudent(scores[i], numScores);
}
return total / num;
}
vector <string> lowerThanAverageList(int scores[][numScores], int num, string names[])
{
vector<string> LowerThanAvg;
int OverAvg = overallAverage(scores, num);
for (int i = 0; i < num; i++)
{
int AvgScore = averageScorePerStudent(scores[i], numScores);
if (AvgScore < OverAvg)
LowerThanAvg.push_back(names[i]);
}
return LowerThanAvg;
}
int maxScoreIndexPerStudent(int scores[], int num)
{
int MaxIndex = 0;
int MaxScore = scores[0];
for (int i = 1; i < num; i++)
{
if (scores[i] > MaxScore)
{
MaxScore = scores[i];
MaxIndex = i;
}
}
return MaxIndex;
}
|