Question: Heres my quest 3 solution: #include #include #include using namespace std; int main() { const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; int
Heres my quest 3 solution:
#include
#include
#include
using namespace std;
int main()
{
const int MAX_STUDENTS = 30;
const int MAX_ASSIGNMENTS = 10;
int numAssignments = 0;
int numStudents;
int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS];
string studentNames[MAX_STUDENTS];
char choice;
//Asks whether the user wants to load a file or not.
cout << "Do you want to load from file(y or n)? ";
cin >> choice;
// An if statement is used so if the answer is y then it will open up the last saved file and if the answer is
// n then it will let the user create a new file.
if (choice == 'y') {
ifstream in("students.txt");
if (in.is_open()) {
in >> numStudents;
in >> numAssignments;
for (int i = 0; i < numStudents; ++i) {
in >> studentNames[i];
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
in >> assignments[i][j];
}
}
in.close();
}
}
else {
cout << "Please enter the number of students currently enrolled (max " << MAX_STUDENTS << "): ";
cin >> numStudents;
if (numStudents > 30) {
numStudents = 30;
cout << "The number of students has been reduced to the max " << MAX_STUDENTS << "." << endl;
}
for (int i = 0; i < numStudents; i++) {
cout << "Enter student " << i + 1 << "'s name: ";
cin >> studentNames[i];
}
}
char command = ' ';
while (command != 'q') {
system("CLS");
// print the gradebook to screen.
for (int i = 0; i < numStudents; i++) {
cout << studentNames[i] << "\t";
if (studentNames[i].size() < 8) {
cout << "\t";
}
for (int j = 0; j < numAssignments; j++) {
cout << assignments[i][j] << "\t";
}
cout << endl;
}
cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade (q)uit? ";
cin >> command;
system("CLS");
switch (command) {
case 's':
cout << "Please enter the new student's name: ";
cin >> studentNames[numStudents];
for (int i = 0; i < numAssignments; i++) {
cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": ";
cin >> assignments[numStudents][i];
}
numStudents++;
break;
case 'a':
for (int i = 0; i < numStudents; i++) {
cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: ";
cin >> assignments[i][numAssignments];
}
numAssignments++;
break;
case 'c': {
int assignmentToCurve = -1;
cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): ";
cin >> assignmentToCurve;
//adjust for arrays starting at 0;
assignmentToCurve--;
int sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += assignments[i][assignmentToCurve];
}
float average = (float)sum / numStudents;
if (average < 70) {
for (int i = 0; i < numStudents; i++) {
assignments[i][assignmentToCurve] += (70 - average);
}
cout << "curve of " << (int)(70 - average) << " applied!" << endl;
}
else {
cout << "No curve needed!" << endl;
}
break;
}
case 'q':
break;
default:
cout << command << " is an invalid command!" << endl;
break;
}
system("PAUSE");
}
// this is the output which opens up the last file created and loads up if the user says y when the program is started.
ofstream out("students.txt");
out << numStudents << endl;
out << numAssignments << endl;
for (int i = 0; i < numStudents; ++i) {
out << studentNames[i] << endl;
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
out << assignments[i][j] << " ";
}
out << endl;
}
out << endl;
out.close();
}
Use your (or my) solution from Quests 2, 3, or 4 and add an extra option to the main menu. The idea is that we want the teacher to be able to randomly call upon a student. Specifically you are to modify the quest so that:
There is an extra menu item: Pick (r)andom student
When the user presses "r" or "R" your program will
Create a uniform int distribution from 0 to numStudents-1
randomly choose a number from that distribution
clear the console
use the randomly generatednumber to retrieve the name of the student at that index
display that student's name on the screen
pause the program until the user presses a key.
Return to the main menu.
Steps 1-6 above should be done in a function with the following declaration: void randomStudent(int numStudents, int studentNames[]);
Here is my solution from quest 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
