Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers.
Directions:
Using the following parallel array and array of vectors:
// may be declared outside the main function const int NUM_STUDENTS = 3;
// may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}};
Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp
Write a C++ program to run a menu-driven program with the following choices:
1) Display the grades
2) Add assignment grade
3) Quit
Make sure your program conforms to the following
requirements:
1. This program should be called
HelpWithGradesPtr.cpp
Include the basic header in your program. (5 points deduction if missing)
2.You must rewrite the following functions from the Help With Grading Program to only accept parameters that are either constant pointers or pointers; all functionality in the original program must remain intact :
3. Add comments wherever necessary. (5 points)
HERE IS MY CODE.
I need to rewrite my current functions to take in pointers
though.
#include <iostream> #include <vector> using namespace std; //Function prototypes int getValidGrade(); void DisplayMenuOptions(); int SelectValidMenuChoice(); void displayGrades(string students[],vector<int> grades[]); void addGrades(string students[],vector<int> g[]); const int NUM_STUDENTS = 3; // Number of students for program const int NUM_GRADES = 5; // Number of assignments per student int main() { string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; //Welcome message to welcome user to program cout << "Welcome to the Help With Grades program.." << endl; //Loop program until user chooses to quit program int MenuChoice = 0; do { //Called based upon user menu selection switch(MenuChoice) { case 1: displayGrades(students, grades); break; case 2: addGrades(students, grades); break; } // Get menu choice from user MenuChoice = SelectValidMenuChoice(); }while (MenuChoice !=3); return 0; } //***************************************************************** // Definition of function getValidGrade which validates grade * // entered by user * //***************************************************************** int getValidGrade() { int valid_grade; do{ cin >> valid_grade; if (valid_grade < 0 || valid_grade > 100) cout << "Please enter a valid grade.." << endl; }while(valid_grade < 0 || valid_grade > 100); return valid_grade; } //***************************************************************** // Definition of function DisplayMenuOptions which displays menu * // options for user to choose from * //***************************************************************** void DisplayMenuOptions() { cout << "1) Display the grades " << endl; cout << "2) Add assignment grade " << endl; cout << "3) Quit " << endl; } //***************************************************************** // Definition of function SelectValidMenuChoice which allows user * // to enter a menu selection and validates the entry * //***************************************************************** int SelectValidMenuChoice() { int MenuChoice; // Get menu selection from user DisplayMenuOptions(); cout << "Enter in your menu choice: " << endl; cin >> MenuChoice; while ((MenuChoice <=0) || (MenuChoice >3)) // input validation loop { // Get menu selection from user DisplayMenuOptions(); cout << "Enter in your menu choice: " << endl; cin >> MenuChoice; } return MenuChoice; } //***************************************************************** // Definition of function DisplayGrades which displays grades * // for each student for current assignments * //***************************************************************** void displayGrades(string students[],vector<int> grades[]) { cout<<"Name Assign.1 Assign.2 Assign.3 Assign.4 Assign.5"<<endl; for(int i=0;i<NUM_STUDENTS;i++){ cout<<students[i]<<" "; for(int j=0;j<NUM_GRADES;j++){ cout<<grades[i][j]<<" "; } cout<<endl; } } //***************************************************************** // Definition of function AddAssignmentGrade which allows user * // to enter in a new assignment grade for student * //***************************************************************** void addGrades(string students[],vector<int> grades[]) { for(int i=0;i<NUM_STUDENTS;i++){ cout<< "Student " << students[i] << ": Please enter in the grade... " <<endl; for(int j=0;j<1;j++){ grades[i].push_back(getValidGrade()); } } }
Hey, the notation [] itself implies that the parameter is of type
pointer. All your parameters are already using the notation [].
Anyway I changed the notations to expicitely to pointers and
attached the modified code. If you need assistant for anything else
or you get any queries, feel free to comment.
Program Screenshot for Indentation Reference:
Sample Output:
Program code to copy:
#include <iostream>
#include <vector>
using namespace std;
//Function prototypes
int getValidGrade();
void DisplayMenuOptions();
int SelectValidMenuChoice();
void displayGrades(const string* students,const vector<int>*
grades);
void addGrades(const string* students,vector<int>*
grades);
const int NUM_STUDENTS = 3; // Number of students for program
const int NUM_GRADES = 5; // Number of assignments per student
int main()
{
string students[NUM_STUDENTS] =
{"Tom","Jane","Jo"};
vector <int> grades[NUM_STUDENTS]
{{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}};
//Welcome message to welcome user to
program
cout << "Welcome to the Help With Grades
program.." << endl;
//Loop program until user chooses to quit
program
int MenuChoice = 0;
do {
//Called based upon user
menu selection
switch(MenuChoice)
{
case 1:
displayGrades(students, grades);
break;
case 2:
addGrades(students, grades);
break;
}
// Get menu choice from
user
MenuChoice =
SelectValidMenuChoice();
}while (MenuChoice !=3);
return 0;
}
//*****************************************************************
// Definition of function getValidGrade which validates
grade *
// entered by
user
*
//*****************************************************************
int getValidGrade()
{
int valid_grade;
do{
cin >>
valid_grade;
if (valid_grade < 0
|| valid_grade > 100)
cout << "Please enter a valid grade.." << endl;
}while(valid_grade < 0 || valid_grade >
100);
return valid_grade;
}
//*****************************************************************
// Definition of function DisplayMenuOptions which displays menu
*
// options for user to choose
from
*
//*****************************************************************
void DisplayMenuOptions()
{
cout << "1) Display the grades " <<
endl;
cout << "2) Add assignment grade "
<< endl;
cout << "3) Quit " << endl;
}
//*****************************************************************
// Definition of function SelectValidMenuChoice which allows user
*
// to enter a menu selection and validates the
entry
*
//*****************************************************************
int SelectValidMenuChoice()
{
int MenuChoice;
// Get menu selection from user
DisplayMenuOptions();
cout << "Enter in your menu choice: "
<< endl;
cin >> MenuChoice;
while ((MenuChoice <=0) || (MenuChoice
>3)) // input validation loop
{
// Get menu selection
from user
DisplayMenuOptions();
cout << "Enter in
your menu choice: " << endl;
cin >>
MenuChoice;
}
return MenuChoice;
}
//*****************************************************************
// Definition of function DisplayGrades which displays
grades *
// for each student for current
assignments
*
//*****************************************************************
void displayGrades(const string* students,const vector<int>*
grades)
{
cout<<"Name Assign.1 Assign.2 Assign.3
Assign.4 Assign.5"<<endl;
for(int i=0;i<NUM_STUDENTS;i++){
cout<<students[i]<<" ";
for(int
j=0;j<NUM_GRADES;j++){
cout<<grades[i][j]<<" ";
}
cout<<endl;
}
}
//*****************************************************************
// Definition of function AddAssignmentGrade which allows
user *
// to enter in a new assignment grade for
student
*
//*****************************************************************
void addGrades(const string* students,vector<int>*
grades)
{
for(int i=0;i<NUM_STUDENTS;i++){
cout<< "Student "
<< students[i] << ": Please enter in the grade... "
<<endl;
for(int
j=0;j<1;j++){
grades[i].push_back(getValidGrade());
}
}
}
Get Answers For Free
Most questions answered within 1 hours.