Hey everyone,
In my COMP 208 class, I was asked to do the following:
"Redo Lab 2 Problem 2, only this time use vectors to hold the names and gpa’s instead of arrays. So you will work with 2 vectors, one holding the gpa’s (type double) and the other holding the names (type string)."
THIS IS THE PROBLEM I NEED TO CONVERT TO VECTORS:
"Your program sorts the students ascending by gpa, and prints the sorted list including names. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers. Sort the numbers. Whenever you swap 2 numbers also swap their matching names in the names array."
I've already coded with arrays in this problem, but now my teacher is asking me to replace the arrays with vectors. The only problem is that we haven't gone over the least bit of vectors, and all I know from research is that vectors are inter-changeable, can be re-sized throughout the program, etc.
I'm asking for help on how to go about changing the student[] and studentGPA[] into vectors and if there is any other importance to vectors, let me know!
Any help would be great, and here is my code for the original w/arrays:
/*
gpaReader.cpp
Write a program that reads names and gpa's from a text file (TextFile1.txt).
The program will:
- sort the students from lowest gpa to highest.
- print the sorted list, including names.
Use 2 parallel arrays, one for the gpa, the other for the students.
Sort the numbers: Whenever you swap 2 gpa's, also swap the students array.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000
using namespace std;
ifstream in;
int readArray(string* student, double* studentGPA);
void gpa_Sorter(string student[], double studentGPA[], int size);
int main(){
string student[MAX];
double studentGPA[MAX];
in.open("TextFile1.txt");
if(!in.is_open ()){
cout << "Failed to open "<< endl;
cout << "Now exiting program...\n";
}
if (in.fail()){
cout << "failed" << endl;
}
int size = readArray(student, studentGPA);
gpa_Sorter(student, studentGPA, size);
in.close();
return 0;
}
int readArray(string* student, double* studentGPA){
int i = 0;
while(i < MAX && !in.eof()){
in >> student[i];
in >> studentGPA[i];
i++;
}
if (i > MAX){
cout << "the file truncated at " << MAX << endl;
// if the file is more than 2k char, output an error message
}
return i;
}
void gpa_Sorter(string student[], double studentGPA[], int size){
//sorts student[] and studentGPA[] ascending
for (int pass = 0; pass < size - 1; pass++){
// 1 less pass than size
for( int cell = 0; cell < size - 1 - pass; cell++){
if(studentGPA[cell] > studentGPA[cell + 1]){
//swap now because they're out of order
double temp = studentGPA[cell];
studentGPA[cell] = studentGPA[cell + 1];
studentGPA[cell + 1] = temp;
string temp2 = student[cell];
student[cell] = student[cell + 1];
student[cell + 1] = temp2;
}
}
}
for (int i = 0; i < size; i++){
cout << student[i] << "\t\t" << studentGPA[i] << "\n";
}
}
Also, I've tried starting with this:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define MAX 2000
using namespace std;
ifstream in;
int main()
{
vector<string> student();
vector<double> studentGPA();
in.open("Textfile1.txt"); //opening file
if(!in.is_open()){ //testing if file opens
cout << "Failed to open..." <<endl;
cout << "Now exiting program...\n";
}
if(in.fail()){
cout << "Failed" <<endl;
}
return 0;
}
int readVector(string student, double studentGPA)
{
while(!in.eof()){
in >> student;
in >> studentGPA;
}
}