sorting with vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeromytheoutlaw
    New Member
    • Mar 2008
    • 3

    sorting with vectors

    [CODE=cpp]/*
    Write a program that reads names and gpa’s from a text file. The file looks like:

    James 3.9
    Margaret 3.5
    Charles 1.2
    Jennifer 4.0

    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.
    Include a structure intt as in the model lab.

    */

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

    void sort(vector<dou ble>(gpaAry), vector<string>( names), int size);
    int smallest(vector <double>(gpaAry ), int first, int last);
    void swap(vector<dou ble>(gpaAry), vector<string>( names), int i, int j);


    int main(){

    ifstream in ("gpa.txt");
    if(!in.is_open ()){ // if file did not open die.
    cout << "Failed to open.\n " << endl;
    cout << "Now exiting...\n ";
    exit(-1);
    }

    string name;
    double gpa;
    vector<string>( names);
    int j = 0;
    vector<double>( gpaAry);

    while(in>>name> >gpa){
    names.push_back (name);
    gpaAry.push_bac k(gpa);
    j++;
    }

    // printing the values
    for(int i = 0; i<4; i++){
    sort(gpaAry,nam es,4);

    cout<<names.at( i)<<" "<<gpaAry.at(i) <<endl;
    }

    return 0;
    }


    void sort(vector<dou ble>(gpaAry), vector<string>( names), int size){

    for (int i = 0; i < size-1; i++){
    int j = smallest(gpaAry ,i,size-1);
    swap(gpaAry,nam es,i,j);
    }
    }

    int smallest(vector <double>(gpaAry ), int first, int last){
    // returns index of cell with smallest value in arr[first .. last]

    int small = first;
    for (int i = first + 1; i<= last; i++){
    if (gpaAry.at(i) < gpaAry.at(small )){
    small = i;
    }
    }
    return small;
    }

    void swap(vector<dou ble>(gpaAry), vector<string>( names), int i, int j){
    // swaps values in cells i and j

    double temp = gpaAry.at(i);
    string tempname = names.at(i);
    gpaAry.at(i) = gpaAry.at(j);
    names.at(i) = names.at(j);
    gpaAry.at(j) = temp;
    names.at(j) = tempname;
    }[/CODE]


    my problem is in my sort function. but i cant figure it out...
    if i use arrays this works but obviously vectors are different.
    help?
    Last edited by Ganon11; Mar 16 '08, 01:57 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I would use a struct or a class to store the name and GPA in the same container. That way, you are passing around and swapping within a single vector, rather than two.

    I also have no idea what you are doing with the statements:

    [CODE=cpp]vector<string>( names);
    vector<double>( gpaAry);[/CODE]

    It looks like you are declaring the variables, but usually this is done like this:

    [CODE=cpp]vector<string> names;
    vector<double> gpaAry;[/CODE]

    The same thing happens in your functions. I don't know if this causes your problems, but it impacts readability. Using structs will simplify matters significantly.

    Comment

    • jeromytheoutlaw
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by Ganon11
      I would use a struct or a class to store the name and GPA in the same container. That way, you are passing around and swapping within a single vector, rather than two.

      I also have no idea what you are doing with the statements:

      [CODE=cpp]vector<string>( names);
      vector<double>( gpaAry);[/CODE]

      It looks like you are declaring the variables, but usually this is done like this:

      [CODE=cpp]vector<string> names;
      vector<double> gpaAry;[/CODE]

      The same thing happens in your functions. I don't know if this causes your problems, but it impacts readability. Using structs will simplify matters significantly.

      the assignment was to use two vectors.
      also i fixed the declarations but to no avail.

      in class we haven't learned structs and we are just now learning classes.
      so im supposed to figure this out but doing it like this...

      ps. thanks for quick reply

      Comment

      • jeromytheoutlaw
        New Member
        • Mar 2008
        • 3

        #4
        Code:
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <vector>
        using std::string;
        using namespace std;
        
        void printvector(vector<double>gpaAry, vector<string>names, int size);
        void sort(vector<double> & gpaAry, vector<string> & names, int size);
        int smallest(vector<double>gpaAry, int start, int size);
        
        
        
        int main(){
        
        	ifstream in ("gpa.txt");
        	if(!in.is_open ()){ // if file did not open die.
        		cout << "Failed to open.\n " << endl;
        		cout << "Now exiting...\n ";
        		exit(-1);
        	}
        
        	string name;
        	double gpa;
        	vector<string>names;	
        	vector<double>gpaAry;
        	
        	while(!in.eof()){
        		in >> name >> gpa;
        		gpaAry.push_back(gpa);
        		names.push_back(name);
        	}
        	
        	
        	sort(gpaAry,names,4);
        	printvector(gpaAry,names,4);
        	in.close();
        
        	return 0;
        }
        
        void printvector(vector<double>gpaAry, vector<string>names, int size){
        	for(int i = 0; i < size; i++){
        		cout << names[i] << " " << gpaAry[i] <<endl;
        	}
        	cout << endl;
        }
        
        void sort(vector<double>& gpaAry, vector<string> & names, int size){
        
        	for (int i = 0; i < size; i++){
        		int j = smallest(gpaAry,i,size);
        		
        		double temp = gpaAry[i];
        		gpaAry[i] = gpaAry[j];
        		gpaAry[j] = temp;
        
        		string tempname = names[i];
        		names[i] = names[j];
        		names[j] = tempname;
        	}
        }
        
        int smallest(vector<double>gpaAry, int start, int size){
        	int small = start;
        	for(int i = start+1; i < size; i++){
        		if(gpaAry[i] < gpaAry[small]){
        			small = i;
        		}
        	}
        	return small;
        }
        Code:
        //Charles 1.2
        //Margaret 3.5
        //James 3.9
        //Jennifer 4
        //
        //Press any key to continue . . .
        i answered my own question.
        but thanks for the help

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Just a second, there.

          Is your class assignment to sort a vector or to write a sort?

          If is it to sort a vector, then use the STL sort. You just need to write a binary predicate to use on the sort call:

          [code=c]
          sort(myvector.b egin(), myvector.end(), mycompare);
          [/code]

          and you are done.

          Comment

          Working...