Question: Please solve it by using c++(Xcode). Please and thank you! Goals (1) Learn how to use array for storing object pointers, instead of objects. (2)
Please solve it by using c++(Xcode). Please and thank you!
Goals
(1) Learn how to use array for storing object pointers, instead of objects.
(2) Learn how to conduct Dynamical Memory Allocation by using the operators new and delete.
Requirements, Design and Implementation
- The raw data of the student information are kept in the file StudentInfo.txt.
| 123456789 John Johnson 3.5 512434990 Mary Jackson 3.9 342432444 Peter Young 2.3 470068625 Jim Lee 2.9 234324324 Tammy Gaddis 3.1 121219000 Ester Schwab 2.7 |
The four columns in the file are: (1) student ID, (2) first name, (3) last name, and (4) GPA.
- Design a struct named studentInfor for storing the data of the students.
- The program should read from the StudentInfo.txt and load the data in an array which stores the pointers of studentInfor objects. (The array should store the pointers, not he objects.)
- Implement three global functions as described below.
| display() | Displays the student information in four columns. |
| resetGPA() | Resets the GPAs of all students to 0.0. |
| sortStud() | Sorts the student records in the array in the sequence of ascending student IDs. |
- Display your output in the following manner: (1) display the student records in the way like they are stored in the studentInfo.txt file, (2) display the sorted student records, and (3) display student records with 0.0 GPA.
Sample array DMA txt:
/*
Goal: The program intends to load the student information and store them
in the array.
The records are sorted by the sortStud() function and the GPAs are reset
to 0.0 by
resetGPA() function.
The code, which is written by a C++ novice, exhibits an incorrect way of
storing objects in the array.
The array is able to store the pointers of the objects. However, if we use
debugger to trace the program,
we can see that in the second iteration of the while loop, the first
object is lost. In the third
iteration, the first two objects are lost. And so on.
Could you point out why the objects are not correctly stored in the array.
How can the
problem be fixed?
*/
#include
#include
#include
#include
using namespace std;
struct studentInfo
{
int ID;
string firstName;
string lastName;
double GPA;
};
const int SIZE = 100;
void display(/* parameters */);
void resetGPA(/* parameters */);
void sortStud(/* parameters */);
int main()
{
int counter = 0;
int ID;
string firstName;
string lastName;
double GPA;
studentInfo * arrStud[SIZE];
ifstream inputFile;
inputFile.open("StudentInfo.txt");
if (inputFile.is_open())
{
while(!inputFile.eof())
{
inputFile >> ID >> firstName >> lastName >> GPA;
studentInfo si;
si.ID = ID;
si.firstName = firstName;
si.lastName = lastName;
si.GPA = GPA;
arrStud[counter] = &si;
// cout << "ID: " << arrStud[counter]->ID << endl;
counter++;
}
for (int i = 0; i< counter; i++)
cout << "ID2: " << i << " " << arrStud[i]->ID << endl;
inputFile.close();
}
else
cout << "File cannot be opened.";
inputFile.close();
display();
cout << endl;
cout << "Sorting students by ID..." << endl;
cout << endl;
sortStud();
display();
cout << endl;
cout << "Resetting GPA data..." << endl;
cout << endl;
resetGPA();
display();
/// How should the pointers in the array be deleted?
}
void display()
{
}
void resetGPA()
{
}
void sortStud()
{
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
