Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My program reads from a csv file and sorts by lastname and ssn. I need help on assigning each pointer of array of pointers by

My program reads from a csv file and sorts by lastname and ssn. I need help on assigning each pointer of array of pointers by indexBySSN and indexByLastname to each elements of studArray (index by index) and calling the functions. I cannot sort the original array, only the pointer arrays(indexBySSN and indexByLastname) I already have code started with loading csv file, sorting by lastname and ssn but not sure how to assign each pointers. This is the overview.

Student studArray[NUM_PEOPLE]; Student* indexBySSN[NUM_PEOPLE]; Student* indexByLastname[NUM_PEOPLE]; // Load csv file into studArray // Assign each pointer of array of pointers indexBySSN to each element of gradesArray (index by index) // Assign each pointer of array of pointers indexByLastname to each element of gradesArray (index by index) // call sortBySSSN function passing indexBySSN as an argument // call sortByLastname function passing indexByLastname as an argument

My main.cpp code:

// global number of students

const int NUM_STUDENTS = 20;

int main()

{

Student studArray[NUM_STUDENTS];

// array of pointers

Student *indexBySSN[NUM_STUDENTS];

Student *indexByLastname[NUM_STUDENTS];

//opening csv file

ifstream readFile;

readFile.open("students.csv");

if(!readFile)

{

cout << "Error opening file!";

exit(0);

}

// loading csv file into studArray

readCsvToArray(studArray, NUM_STUDENTS, readFile);

int index = 0;

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

{

//printing the array

cout << studArray[index].id << "," << studArray[index].lastName << ","

<< studArray[index].firstName << "," << studArray[index].ssn << ",";

//prints out the test scores 4 times

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

{

cout << studArray[index].test[i] << ",";

}

cout << studArray[index].final << ","

<< studArray[index].letterGrade << endl;

index++;

}

readFile.close();

My grades.h:

//grades.h

#ifndef GRADES_H

#define GRADES_H

#include

#include

#include

#include

#include

#include

using namespace std;

struct Student

{

int id;

string lastName;

string firstName;

string ssn;

float test[4];

float final;

string letterGrade;

char delim = ',';

};

//function declarations

void menu();

void readCsvToArray(Student array[], int size, ifstream& file);

void sortByLastname(Student *indexByLastname[], int size);

void sortBySSN(Student *indexBySSN[], int size);

My grades.cpp:

//reads through the csv file to an array and uses commas a delimeters

void readCsvToArray(Student studArray[], int size, ifstream& file)

{

//reads the first line

string dummyString;

getline(file, dummyString);

int index = 0;

char delim;

while((!file.eof()) && (index < size))

{

file >> studArray[index].id >> delim; // read the id and the comma delimiter(in delim)

getline(file, studArray[index].lastName, ','); // read the lastname

getline(file, studArray[index].firstName, ','); // read the firstname

getline(file, studArray[index].ssn, ','); // read ssn

// loop to read the 4 test scores

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

{

file >> studArray[index].test[i] >> delim;

}

// read the final

file >> studArray[index].final >> delim;

// read the letter grade (till the end of line)

getline(file, studArray[index].letterGrade);

index++;

}

}

//sorts the indexByLastname pointer array

void sortByLastname(Student *indexByLastname[], int size)

{

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

{

for (int j = 0; j < size; j++)

{

if ((*indexByLastname)[j].lastName > (*indexByLastname)[j + 1].lastName)

{

Student *temp = indexByLastname[j + 1];

indexByLastname[j + 1] = indexByLastname[j];

indexByLastname[j] = temp;

}

}

}

}

//this function sorts the SSN pointers

void sortBySSN(Student *indexBySSN[], int size)

{

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

{

//we set minimum index to i, it means in alphabetical order i comes earlier

int minIndex = i;

/*then we compare it with every other variable,

the one that has the least in alphabetical order is assigned to minIndex*/

for(int j = i + 1; j < size; j++)

{

if((*indexBySSN)[minIndex].ssn > (*indexBySSN)[j].ssn)

minIndex = j;

}

//here we swap ith position with the minimum index variable

Student *temp = indexBySSN[i + 1];

indexBySSN[i + 1] = indexBySSN[i];

indexBySSN[i] = temp;

}

}

The cvs file:

id,Last Name,First Name,SSN,Test1,Test2,Test3,Test4,Final,Grade 1,Alfalfa,Aloysius,123-45-6789,40,90,100,83,49,D- 2,Alfred,University,123-12-1234,41,97,96,97,48,D+ 3,Gerty,Gramma,567-89-0123,41,80,60,40,44,C 4,Android,Electric,087-65-4321,42,23,36,45,47,B- 5,Bumpkin,Fred,456-78-9012,43,78,88,77,45,A- 6,Rubble,Betty,234-56-7890,44,90,80,90,46,C- 7,Noshow,Cecil,345-67-8901,45,11,-1,4,43,F 8,Buff,Bif,632-79-9939,46,20,30,40,50,B+ 9,Airpump,Andrew,223-45-6789,49,1,90,100,83,A 10,Backus,Jim,143-12-1234,48,1,97,96,97,A+ 11,Carnivore,Art,565-89-0123,44,1,80,60,40,D+ 12,Dandy,Jim,087-75-4321,47,1,23,36,45,C+ 13,Elephant,Ima,456-71-9012,45,1,78,88,77,B- 14,Franklin,Benny,234-56-2890,50,1,90,80,90,B- 15,George,Boy,345-67-3901,40,1,11,-1,4,B 16,Heffalump,Harvey,632-79-9439,30,1,20,30,40,C 17,de Paul,Martin,829-34-3412,34,49,12,90,34,B+ 18,Madrigal,Jay,309-12-4312,18,2,34,34,5,F 19,Osborne,Demi,025-24-7821,50,98,100,89,99,A 20,Garrett,Keith,723-35-9082,23,53,10,34,84,C+

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions