Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Im trying to remove any hard coded values in my code which will be provided

ID: 3876507 • Letter: C

Question

C++:

Im trying to remove any hard coded values in my code which will be provided below. we are not supposed to use hardcoded values instead we are supposed to use the heap by dynamically-allocated memory for how much the user enters the number of students. So for example the user can enter as many students as they want and everything should be going through the heap and pointerss and refrences. My header for the input Function should be like this: int inputData(string*&, double**&); for the harded coded valuse i mean like: int numberOfTests[10]; OR char calgrade[10]; so these shouldnt be like this it shoud be the user can enter as many as they want.

so here is my code:

#include <iostream>

#include <string>

using namespace std;

int numberOfTests[10];

char calgrade[10];

int howManyStudents;

int inputData(string* str, double** grade);

char* calcGrade(double** grade, int howManyStudents);

void displayData(string* students, double** grade, char* calgrade, int howManyStudents);

void cleanHeap(string* students, double** grade, char* calgrade, int howManyStudents);

int main()

{

string students[10];

double** ary = new double*[10];

for(int i = 0; i < 10; ++i)

ary[i] = new double[10];

inputData(students,ary);

calcGrade(ary,howManyStudents);

displayData(students,ary,calgrade,howManyStudents);

}

int inputData(string* str, double** grade)

{

int numStudents;

double gr;

string students;

cout << "How many students do you have in the system? ";

cin >> numStudents;

cout << endl;

str = new string[numStudents];

for(int i = 0; i < howManyStudents; i++)

{

cout << "Enter the student's name: ";

cin.ignore();

getline(cin, students);

label1 :

cout << "How many tests " << students << " took: ";

cin >> numberOfTests;

if( numberOfTests[i] < 0)

{

cout << "Please enter a positive number of tests taken" << endl;

goto label1;

}

str[i] = students;

for(int j = 0; j < numberOfTests[i]; j++)

{

label2 :

cout << "please enter grade #"<< j+1 << ": ";

cin >> gr;

if( gr < 0 || gr > 100)

{

cout << "Please enter a grade between 0 and 100." << endl;

goto label2;

}

grade[i][j] = gr;

}

cout << endl;

}

}

char* calcGrade(double** grade, int howManyStudents)

{

double average;

for(int i = 0; i < howManyStudents; i++)

{

average = 0;

for(int j = 0; j < numberOfTests[i]; j++)

average += grade[i][j];

average = average/numberOfTests[i];

if(average >= 90 && average <= 100)

calgrade[i] = 'A';

else if(average >= 80 && average <= 89)

calgrade[i] = 'B';

else if(average >= 70 && average <= 79)

calgrade[i] = 'C';

else if(average >= 60 && average <= 69)

calgrade[i] = 'D';

else if(average < 60)

calgrade[i] = 'F';

}

return calgrade;

}

void displayData(string* students, double** grade, char* calgrade, int howManyStudents)

{

cout << " You have " << howManyStudents << " students in the system. ";

for(int i = 0; i < howManyStudents; i++)

{

cout << " Name of the student #" << i+1 << ": " << students[i];

cout << " Grades for student #" << i+1 << ": ";

for(int j = 0; j < numberOfTests[i]; j++)

cout << grade[i][j] << " ";

cout << " Average of grades for student #" << i+1 << ": " << calgrade[i];

cout << endl;

cout << endl;

}

}

void cleanHeap(string* students, double** grade, char* calgrade, int howManyStudents)

{

delete[] students;

delete[] calgrade;

for(int i = 0; i < 10; ++i)

delete[] grade[i];

delete[] grade;

}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
string *students;
int *numberOfTests;
char *calgrade;
int howManyStudents;

int inputData(string* str, double** grade);
char* calcGrade(double** grade, int howManyStudents);
void displayData(string* students, double** grade, char* calgrade, int howManyStudents);
void cleanHeap(string* students, double** grade, char* calgrade, int howManyStudents);

int main()
{
double** ary ;
inputData(students,ary);
//calcGrade(ary,howManyStudents);
//displayData(students,ary,calgrade,howManyStudents);


}
int inputData(string* str, double** grade)
{
int numStudents;
double gr;
//string students;

cout << "How many students do you have in the system? ";
cin >> numStudents;
howManyStudents=numStudents;
cout << endl;
  
str = new string[numStudents];
numberOfTests = new int[numStudents];
calgrade = new char[numStudents];
grade = new double*[numStudents];
   for(int i=0;i<numStudents;i++)grade[i]=new double[numStudents];
  
for(int i = 0; i < howManyStudents; i++)
{
cout << "Enter the student's name: ";
cin.ignore();
string students;
getline(cin, students);
label1 :
cout << "How many tests " << students << " took: ";
cin >> numberOfTests[i];

if( numberOfTests[i] < 0)
{
cout << "Please enter a positive number of tests taken" << endl;
goto label1;
}
str[i] = students;
for(int j = 0; j < numberOfTests[i]; j++)
{
           label2 :
cout << "please enter grade #"<< j+1 << ": ";
cin >> gr;

if( gr < 0 || gr > 100)
{
cout << "Please enter a grade between 0 and 100." << endl;
goto label2;
}
grade[i][j] = gr;
}
cout << endl;
}
}

output:

How many students do you have in the system? 2

Enter the student's name: henna
How many tests henna took: 2
please enter grade #1: 10
please enter grade #2: 20

Enter the student's name: jeny
How many tests jeny took: 3
please enter grade #1: 30
please enter grade #2: 10
please enter grade #3: 20

Process exited normally.
Press any key to continue . . .

How many students do you have in the system? 2

Enter the student's name: jenn
How many tests jenn took: -1
Please enter a positive number of tests taken
How many tests jenn took: 1
please enter grade #1: 101
Please enter a grade between 0 and 100.
please enter grade #1: 40

Enter the student's name: fer
How many tests fer took: 2
please enter grade #1: 10
please enter grade #2: 20

Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote