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;
	}

}

To add new items to an empty vector, use the std::vector::push_back() method:

#include <iostream>
#include <vector>
#include <string>

int main(){
    /* Make a vector of string to store the names */
    std::vector< std::string > names;
    
    /* Read in an arbitrary number of names */
    std::cout << "Tyep \"exit\" to finish" << std::endl;
    while(true){
        /* First read in the name */
        std::string temp;
        std::cout << "Enter a name: ";
        std::cin >> temp;

        /* Check that it's not the exit keyword */
        if(temp == "exit")    break;
        
        /* Put the name in the vector using push_back(), if it's not the exit keyword */
        names.push_back(temp);
    }

    /* Print out the names using the [] notation */
    for(unsigned i = 0; i < names.size(); ++i)
        std::cout << names[i] << std::endl;

    return 0;
}

That's about all you need to know to complete this task. There is much more that std::vector can d, but you can read up on that elsewhere :)

i have to read in a file though. Is it possible to use push_back by doing the following:

while(!in.eof){
in >> (string) temp1
in >> (double) temp2

student.push_back(temp1);
studentGPA.push_back(temp2);
}

What I am trying to get at first, is to stream the file onto 2 parallel variables, then maybe use push_back to store the variables onto 2 parallel vectors?!

I just did some researching on it, but tell me if that sounds right.

i have to read in a file though. Is it possible to use push_back by doing the following:

while(!in.eof){
in >> (string) temp1
in >> (double) temp2

student.push_back(temp1);
studentGPA.push_back(temp2);
}

Er, it's certainly possible to do:

while(in.fail() == false){
    std::string temp1;
    double temp2;
    in >> temp1;
    in >> temp2;
    
    // You could also do:
    // in >> temp1 >> temp2;
    // they're pretty much the same

    student.push_back(temp1);
    studentGPA.push_back(temp2);
}

which is basically what you said, but with the errors corrected.

thanks rav, you always give great tips and information.

So after the information is in the parallel vectors, i can then use the original algorithm for sorting the GPAs ascending? Or is there a method that I am not aware of?

Thanks again!

thanks rav, you always give great tips and information.

So after the information is in the parallel vectors, i can then use the original algorithm for sorting the GPAs ascending? Or is there a method that I am not aware of?

Thanks again!

Well, you can use the same algorithm for sure. You will have to change the actual function a bit though, so that it takes a vector argument instead of an array, but that should be about it. There are some other conveniences that you get by using vectors too, such as knowing the size and things like that. Have fun!

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

template <class T>
void swap(T* a, T* b){
	T temp = a;
	a = b;
	b = temp;
}

int main() {
	ifstream	infile;
	vector<double>	vecGPA;
	vector<string>	vecNames;
	double		gpa;
	string		name;
	
        // load data from file
	infile.open("gpa.txt");
	while(infile >> name >> gpa){
		vecNames.push_back(name);
		vecGPA.push_back(gpa);
	}
	infile.close();
	
        // sort vectors in parallel (largest GPA to smallest)
	for(int i = 0; i < vecNames.size(); i++)
	    for(int j = i; j < vecNames.size(); j++)
	        if( vecGPA[i] < vecGPA[j] ){
		    swap(vecGPA[i], vecGPA[j]);
		    swap(vecNames[i], vecNames[j]);
		}

        // print contents of vectors
	for(int i = 0; i < vecNames.size(); i++)
	    cout << vecNames[i] << '\t' << vecGPA[i] << endl;
	return 0;
}

Edited 5 Years Ago by user422: n/a

This question has already been answered. Start a new discussion instead.