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

Your classmate has started the following C++ program to read a le of data on 100

ID: 3936114 • Letter: Y

Question

Your classmate has started the following C++ program to read a le of data on 100 students
from le. Because keeping track of four arrays is getting complicated, he is not sure how to
proceed with the computations to be performed on the data.


#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream input_file("student_data.txt");


const int SIZE = 100;
string names[SIZE];
int uids[SIZE];
double gpas[SIZE];
int num_of_courses[SIZE];


for(int i = 0; i < SIZE; i++) {
input_file >> names[i] >> uids[i] >> gpas[i] >> num_of_courses[i];
}
// not sure how to proceed from here . . .
}


Help your classmate simplify the program by using just one array of structures. Rewrite the
program below.

Thanks

Explanation / Answer

Hi,

I have updated the code with one array of structures. Highlighted the code changed below.

#include <iostream>
#include <fstream>
using namespace std;
struct STUDENT_DATA
{
string name;
int uids;
double gpas;
int num_of_courses;
} ;

int main() {
ifstream input_file("student_data.txt");

const int SIZE = 100;
STUDENT_DATA data[SIZE];
//string names[SIZE];
//int uids[SIZE];
//double gpas[SIZE];
//int num_of_courses[SIZE];

for(int i = 0; i < SIZE; i++) {
input_file >> data[i].name >> data[i].uids >>data[i].gpas >> data[i].num_of_courses;
}
}

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