Can someone tell me why my random number is not working. I am trying to find ran
ID: 3721803 • Letter: C
Question
Can someone tell me why my random number is not working. I am trying to find random student from gradebook. #include <iostream> #include <string> #include <fstream> #include <random> #include <ctime> #include <stdlib.h>using namespace std; const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; void startUp(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int &numAssignments) { //this loads from file 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(); } }
void displayGradebook(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) { // prints the gradebook to the screen. for (int i = 0; i < numStudents; i++) { cout << studentNames[i] << " "; if (studentNames[i].size() < 8) { cout << " "; } for (int j = 0; j < numAssignments; j++) { cout << assignments[i][j] << " "; } cout << endl; } }
void diplayMenu() { cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade, (q)uit, or choose a (r)andom student? "; }
void addStudent(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int numAssignments) { // adds student to the grade book cout << "Please enter the new student's name: "; cin >> studentNames[numStudents]; // asks for the grade for the new student for (int i = 0; i < numAssignments; i++) { cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": "; cin >> assignments[numStudents][i]; } numStudents++; }
void addAssignment(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int &numAssignments) { // allows you to add assignment for students for (int i = 0; i < numStudents; i++) { cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: "; cin >> assignments[i][numAssignments]; } numAssignments++; }
void curveGrade(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], int numStudents, int numAssignments) { int assignmentToCurve = -1; cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): "; cin >> assignmentToCurve; 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; } }
void shutDown(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// this opens the last file that was created if the user chooses y 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(); } // chooses a random student from the gradebook void randomStudent(int numStudents, string studentNames[MAX_STUDENTS]) { std::random_device rdev; default_random_engine randomStudent(rdev()); uniform_int_distribution<int> studentNames(1, numStudents);
cout << "You randomly selected: " << studentNames(randomStudent) << endl; } int main() { int numAssignments = 0; int numStudents; int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS]; string studentNames[MAX_STUDENTS]; char choice; cout << "Do you want to load from file(y or n)? "; cin >> choice; if (choice == 'y') { startUp(assignments, studentNames, numStudents, numAssignments); } 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"); displayGradebook(assignments, studentNames, numStudents, numAssignments);
diplayMenu(); cin >> command;
system("CLS"); switch (command) { case 's': addStudent(assignments, studentNames, numStudents, numAssignments); break; case 'a': addAssignment(assignments, studentNames, numStudents, numAssignments); break; case 'c': curveGrade(assignments, numStudents, numAssignments); break; case 'r':
// this will allow the user to randomly choose a student.
system("CLS");
randomStudent(numStudents, studentNames);
break; case 'q': break; default: cout << command << " is an invalid command!" << endl; break; } system("PAUSE"); } shutDown(assignments, studentNames, numStudents, numAssignments); } Can someone tell me why my random number is not working. I am trying to find random student from gradebook. #include <iostream> #include <string> #include <fstream> #include <random> #include <ctime> #include <stdlib.h>
using namespace std; const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; void startUp(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int &numAssignments) { //this loads from file 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(); } }
void displayGradebook(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) { // prints the gradebook to the screen. for (int i = 0; i < numStudents; i++) { cout << studentNames[i] << " "; if (studentNames[i].size() < 8) { cout << " "; } for (int j = 0; j < numAssignments; j++) { cout << assignments[i][j] << " "; } cout << endl; } }
void diplayMenu() { cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade, (q)uit, or choose a (r)andom student? "; }
void addStudent(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int numAssignments) { // adds student to the grade book cout << "Please enter the new student's name: "; cin >> studentNames[numStudents]; // asks for the grade for the new student for (int i = 0; i < numAssignments; i++) { cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": "; cin >> assignments[numStudents][i]; } numStudents++; }
void addAssignment(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int &numAssignments) { // allows you to add assignment for students for (int i = 0; i < numStudents; i++) { cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: "; cin >> assignments[i][numAssignments]; } numAssignments++; }
void curveGrade(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], int numStudents, int numAssignments) { int assignmentToCurve = -1; cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): "; cin >> assignmentToCurve; 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; } }
void shutDown(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// this opens the last file that was created if the user chooses y 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(); } // chooses a random student from the gradebook void randomStudent(int numStudents, string studentNames[MAX_STUDENTS]) { std::random_device rdev; default_random_engine randomStudent(rdev()); uniform_int_distribution<int> studentNames(1, numStudents);
cout << "You randomly selected: " << studentNames(randomStudent) << endl; } int main() { int numAssignments = 0; int numStudents; int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS]; string studentNames[MAX_STUDENTS]; char choice; cout << "Do you want to load from file(y or n)? "; cin >> choice; if (choice == 'y') { startUp(assignments, studentNames, numStudents, numAssignments); } 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"); displayGradebook(assignments, studentNames, numStudents, numAssignments);
diplayMenu(); cin >> command;
system("CLS"); switch (command) { case 's': addStudent(assignments, studentNames, numStudents, numAssignments); break; case 'a': addAssignment(assignments, studentNames, numStudents, numAssignments); break; case 'c': curveGrade(assignments, numStudents, numAssignments); break; case 'r':
// this will allow the user to randomly choose a student.
system("CLS");
randomStudent(numStudents, studentNames);
break; case 'q': break; default: cout << command << " is an invalid command!" << endl; break; } system("PAUSE"); } shutDown(assignments, studentNames, numStudents, numAssignments); } #include <iostream> #include <string> #include <fstream> #include <random> #include <ctime> #include <stdlib.h>
using namespace std; const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; void startUp(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int &numAssignments) { //this loads from file 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(); } }
void displayGradebook(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) { // prints the gradebook to the screen. for (int i = 0; i < numStudents; i++) { cout << studentNames[i] << " "; if (studentNames[i].size() < 8) { cout << " "; } for (int j = 0; j < numAssignments; j++) { cout << assignments[i][j] << " "; } cout << endl; } }
void diplayMenu() { cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade, (q)uit, or choose a (r)andom student? "; }
void addStudent(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int numAssignments) { // adds student to the grade book cout << "Please enter the new student's name: "; cin >> studentNames[numStudents]; // asks for the grade for the new student for (int i = 0; i < numAssignments; i++) { cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": "; cin >> assignments[numStudents][i]; } numStudents++; }
void addAssignment(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int &numAssignments) { // allows you to add assignment for students for (int i = 0; i < numStudents; i++) { cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: "; cin >> assignments[i][numAssignments]; } numAssignments++; }
void curveGrade(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], int numStudents, int numAssignments) { int assignmentToCurve = -1; cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): "; cin >> assignmentToCurve; 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; } }
void shutDown(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// this opens the last file that was created if the user chooses y 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(); } // chooses a random student from the gradebook void randomStudent(int numStudents, string studentNames[MAX_STUDENTS]) { std::random_device rdev; default_random_engine randomStudent(rdev()); uniform_int_distribution<int> studentNames(1, numStudents);
cout << "You randomly selected: " << studentNames(randomStudent) << endl; } int main() { int numAssignments = 0; int numStudents; int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS]; string studentNames[MAX_STUDENTS]; char choice; cout << "Do you want to load from file(y or n)? "; cin >> choice; if (choice == 'y') { startUp(assignments, studentNames, numStudents, numAssignments); } 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"); displayGradebook(assignments, studentNames, numStudents, numAssignments);
diplayMenu(); cin >> command;
system("CLS"); switch (command) { case 's': addStudent(assignments, studentNames, numStudents, numAssignments); break; case 'a': addAssignment(assignments, studentNames, numStudents, numAssignments); break; case 'c': curveGrade(assignments, numStudents, numAssignments); break; case 'r':
// this will allow the user to randomly choose a student.
system("CLS");
randomStudent(numStudents, studentNames);
break; case 'q': break; default: cout << command << " is an invalid command!" << endl; break; } system("PAUSE"); } shutDown(assignments, studentNames, numStudents, numAssignments); } #include <iostream> #include <string> #include <fstream> #include <random> #include <ctime> #include <stdlib.h>
using namespace std; const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; void startUp(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int &numAssignments) { //this loads from file 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(); } }
void displayGradebook(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) { // prints the gradebook to the screen. for (int i = 0; i < numStudents; i++) { cout << studentNames[i] << " "; if (studentNames[i].size() < 8) { cout << " "; } for (int j = 0; j < numAssignments; j++) { cout << assignments[i][j] << " "; } cout << endl; } }
void diplayMenu() { cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade, (q)uit, or choose a (r)andom student? "; }
void addStudent(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int numAssignments) { // adds student to the grade book cout << "Please enter the new student's name: "; cin >> studentNames[numStudents]; // asks for the grade for the new student for (int i = 0; i < numAssignments; i++) { cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": "; cin >> assignments[numStudents][i]; } numStudents++; }
void addAssignment(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int &numAssignments) { // allows you to add assignment for students for (int i = 0; i < numStudents; i++) { cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: "; cin >> assignments[i][numAssignments]; } numAssignments++; }
void curveGrade(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], int numStudents, int numAssignments) { int assignmentToCurve = -1; cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): "; cin >> assignmentToCurve; 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; } }
void shutDown(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// this opens the last file that was created if the user chooses y 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(); } // chooses a random student from the gradebook void randomStudent(int numStudents, string studentNames[MAX_STUDENTS]) { std::random_device rdev; default_random_engine randomStudent(rdev()); uniform_int_distribution<int> studentNames(1, numStudents);
cout << "You randomly selected: " << studentNames(randomStudent) << endl; } int main() { int numAssignments = 0; int numStudents; int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS]; string studentNames[MAX_STUDENTS]; char choice; cout << "Do you want to load from file(y or n)? "; cin >> choice; if (choice == 'y') { startUp(assignments, studentNames, numStudents, numAssignments); } 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"); displayGradebook(assignments, studentNames, numStudents, numAssignments);
diplayMenu(); cin >> command;
system("CLS"); switch (command) { case 's': addStudent(assignments, studentNames, numStudents, numAssignments); break; case 'a': addAssignment(assignments, studentNames, numStudents, numAssignments); break; case 'c': curveGrade(assignments, numStudents, numAssignments); break; case 'r':
// this will allow the user to randomly choose a student.
system("CLS");
randomStudent(numStudents, studentNames);
break; case 'q': break; default: cout << command << " is an invalid command!" << endl; break; } system("PAUSE"); } shutDown(assignments, studentNames, numStudents, numAssignments); }
Explanation / Answer
If you need any clarifications, please give me comment...
void randomStudent(int numStudents, string studentNames[MAX_STUDENTS])
{
random_device rdev;
default_random_engine randStudent(rdev());
uniform_int_distribution<int> studNames(1, numStudents);
int v = studNames(randStudent);
cout << "You randomly selected: " << studentNames[v] << endl;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.