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

I posted this question, and someone answered, but there is an erroer and nobody

ID: 3830703 • Letter: I

Question

I posted this question, and someone answered, but there is an erroer and nobody correct it. Therefore, I am going to post it again, and I hope that I will get the correct answer.

IT MUST BE IN C++

Chapter 11, Programming Challenge 12: Course Grade

Write a program that uses a structure to store structure data as describe in

Program template:

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;

// Declaration of Student structure
struct Student
{
// TODO: structure for
// 1) Student name,
// 2) Student ID number,
// 3) Pointer to array of test scores
// 4) Average test score
// 5) Course grade
};

// Function prototypes
Student *initArrays(int, int);
void getInfo(Student[], int, int);
void showInfo(Student[], int, int);

int main()
{
int numStudents; // Number of students
int numTests; // Number of tests
Student *list = nullptr; // Pointer to Student array

// Get the number of students.
cout << "How many students? ";
cin >> numStudents;

// Get the number of tests per student.
cout << "How many tests per student? ";
cin >> numTests;

// TODO: Create an array of Students
list = initArrays(numStudents, numTests);

// TODO: Populate the array with data.
getInfo(list, numStudents, numTests);

// TODO: Display the data.
showInfo(list, numStudents, numTests);

return 0;
}

//**************************************************
// Function initArrays *
// This function dynamically allocates an array *
// of Student structures and for each element in *
// the array, allocates an array of ints to hold *
// tests scors. The parameter s is the number of *
// element to allocate for the array of structures *
// and the parameter t is the number of elements *
// allocate for each array of ints. *
//**************************************************
Student *initArrays(int s, int t)
{
Student *ptr = nullptr;

// Allocate the array of Student structures.
ptr = new Student[s];

// TODO: Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.

// Return a pointer to the array of structures.
return ptr;
}

//*****************************************************
// Function getInfo *
// This function populates the Student array s with *
// data entered by the user. The paramater ns is the *
// number of students and nt is the number of tests. *
//*****************************************************

void getInfo(Student s[], int ns, int nt)
{
int total;

//TODO: Get the data for each student.
}

Explanation / Answer

Try this code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Declaration of Student structure
struct Student
{
   // TODO: structure for
   // 1) Student name,
   string name;
   // 2) Student ID number,
   int ID;
   // 3) Pointer to array of test scores
   int* testScores;
   // 4) Average test score
   double avgTest;
   // 5) Course grade
   char grade;
};
// Function prototypes
Student *initArrays(int, int);
void getInfo(Student[], int, int);
void showInfo(Student[], int, int);
int main()
{
int numStudents; // Number of students
int numTests; // Number of tests
Student *list = nullptr; // Pointer to Student array
// Get the number of students.
cout << "How many students? ";
cin >> numStudents;
// Get the number of tests per student.
cout << "How many tests per student? ";
cin >> numTests;
// TODO: Create an array of Students
list = initArrays(numStudents, numTests);
// TODO: Populate the array with data.
getInfo(list, numStudents, numTests);
// TODO: Display the data.
showInfo(list, numStudents, numTests);
return 0;
}
//**************************************************
// Function initArrays *
// This function dynamically allocates an array *
// of Student structures and for each element in *
// the array, allocates an array of ints to hold *
// tests scors. The parameter s is the number of *
// element to allocate for the array of structures *
// and the parameter t is the number of elements *
// allocate for each array of ints. *
//**************************************************
Student *initArrays(int s, int t)
{
Student *ptr = nullptr;
// Allocate the array of Student structures.
ptr = new Student[s];
// TODO: Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.
for(int i = 0; i < s; i++)
   ptr[i].testScores = new int[t];
// Return a pointer to the array of structures.
return ptr;
}
//*****************************************************
// Function getInfo *
// This function populates the Student array s with *
// data entered by the user. The paramater ns is the *
// number of students and nt is the number of tests. *
//*****************************************************
void getInfo(Student s[], int ns, int nt)
{
int total;
//TODO: Get the data for each student.
for(int i = 0; i < ns; i++)
{
    cout << "Enter the name of student # " << i+1 << ": ";
    cin >> s[i].name;
    cout << "Enter the ID of student # " << i+1 << ": ";
    cin >> s[i].ID;
    cout << "Enter " << nt << " test scores: ";
    for(int j = 0; j < nt; j++)
    {
       cin >> s[i].testScores[j];
       while(s[i].testScores[j] < 0 || s[i].testScores[j] > 100)
       {
          cout << "Test scores should be in range [0 - 100]" << endl;
          j--;
          s[i].testScores[j] = 0;
       }
       total += s[i].testScores[j];
    }
    s[i].avgTest = (double)total / nt;   
    if(s[i].avgTest > 90)
        s[i].grade = 'A';
    else if(s[i].avgTest > 80)  
        s[i].grade = 'B';
    else if(s[i].avgTest > 70)  
        s[i].grade = 'C';
    else if(s[i].avgTest > 60)  
        s[i].grade = 'D';
    else
        s[i].grade = 'F';  
}
}
//*****************************************************
// Function showInfo *
// This function displays all of the data in the *
// array s. The paramater ns is the number of *
// students and nt is the number of tests. *
//*****************************************************
void showInfo(Student s[], int ns, int nt)
{
// TODO: Displays all of the data in the array s
for(int i = 0; i < ns; i++)
{
    cout << "Name: " << s[i].name << endl;
    cout << "ID: " << s[i].ID << endl;
    cout << "Test scores: ";
    for(int j = 0; j < nt; j++)
        cout << s[i].testScores[j] << " ";
    cout << endl;
    cout << "Average: " << s[i].avgTest << endl;
    cout << "Grade: " << s[i].grade << endl;  
}
}