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

The name of the C++ file must be parallel.cpp Write a program that reads a data

ID: 3753484 • Letter: T

Question

The name of the C++ file must be parallel.cpp

Write a program that reads a data file containing students’ data. The name of the file is “grade.dat” (the user will not specify the name). Each line in the file represents a student record containing his numeric student ID, student last name, and exam score. You need to create three parallel arrays to store students’ records read from the file.

Your program will do the following:

1. Sort the parallel arrays by “exam score” using a modified version of the selection sort algorithm. Refer to the lecture discussion on dualSort function. Make sure you use findMin and swap functions.

2. Use showArray function to show elements of the arrays before and after sorting.

Explanation / Answer

If you have any doubts, please give me comment...

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

#define MAX_STUDS 20

int findMin(int scores[], int start, int n);

void swap(int ids[], string names[], int scores[], int pos1, int pos2);

void showArray(int ids[], string names[], int scores[], int n);

int main(){

int ids[MAX_STUDS], scores[MAX_STUDS];

string names[MAX_STUDS];

ifstream in;

in.open("grade.dat");

if(in.fail()){

cout<<"Unable to open file"<<endl;

return 0;

}

int n=0;

while(!in.eof()){

in>>ids[n]>>names[n]>>scores[n];

n++;

}

cout<<"The students before sorting are: "<<endl;

showArray(ids, names, scores, n);

for(int i=0; i<n; i++){

int min = findMin(scores, i, n);

swap(ids, names, scores, min, i);

}

cout<<"The students after sorting are: "<<endl;

showArray(ids, names, scores, n);

return 0;

}

int findMin(int scores[], int start, int n){

int min = start;

for(int i=start; i<n; i++){

if(scores[min]>scores[i])

min = i;

}

return min;

}

void swap(int ids[], string names[], int scores[], int pos1, int pos2){

int temp = ids[pos1];

ids[pos1] = ids[pos2];

ids[pos2] = temp;

temp = scores[pos1];

scores[pos1] = scores[pos2];

scores[pos2] = temp;

string tmp = names[pos1];

names[pos1] = names[pos2];

names[pos2] = tmp;

}

void showArray(int ids[], string names[], int scores[], int n){

cout<<"ID Name Scores"<<endl;

for(int i=0; i<n; i++){

cout<<ids[i]<<" "<<names[i]<<" "<<scores[i]<<endl;

}

}