selection sort records of chars

This is a discussion on selection sort records of chars within the C++ Programming forums, part of the General Programming Boards category; Hello The book I am using is not of help, so I would love if someone could give me a ...

  1. #1
    hew
    hew is offline
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    selection sort records of chars

    Hello
    The book I am using is not of help, so I would love if someone could give me a few hints.

    I have already declared three 2D arrays of lname, fname, and grades and have written a function to read the input file "scores.txt" into the arrays and returned the length.

    The problem I am having is working on a void function to sort records of students by last name. I have to use sort selection and sort the names in ascending order by last name. The last name, first name, and grades are all 2D arrays, which I have declared as lname, fname, and grades. The list below is an example of what I need to sort. It includes the last name, first name, and 5 grades.

    Code:
    Smith Tom 25 25 20 15 24
    Broom Linda 22 23 25 25 20
    Tanner Joe 22 15 24 18 20
    This is what I have so far
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    
    using namespace std;
    
    const char LNAME_SIZE = 20;
    const char FNAME_SIZE = 15;
    const char GRADES_SIZE = 20;
    
    const char STUDENTS = 32;
    
    
    int readScores(ifstream& fin, char lname[][LNAME_SIZE + 1], 
    			    char fname[][FNAME_SIZE + 1], char grades[][GRADES_SIZE + 1]);
    	// reads scores.txt into the arrays and returns the length
    
    void sortRecords(char lname[][LNAME_SIZE + 1], int length);
    	// sorts the records by last name in ascending order
    
    
    
    
    void main(void)
    {
    	ifstream fin;
    	ofstream fout;
    
    	char lname[STUDENTS][LNAME_SIZE + 1];
    	char fname[STUDENTS][FNAME_SIZE + 1];
    	char grades[STUDENTS][GRADES_SIZE + 1];
    	int length;
    
    
    	// connect the input stream to the file scores.txt
    	fin.open("scores.txt");
    	
    	// tests the input stream
    	if ( fin.fail() )
    	{
    		cerr << "Error opening file scores.txt for reading.  Aborting!"
    			 << endl
    			 << endl;
    		exit(1);
    	}
    
    
    	// read the file into the arrays
    	length = readScores(fin, lname, fname, grades);
    
    	// sorts the records in alphabetical order by last name
    	sortRecords(lname, length);
    
    
    	// connect the output stream to the file records.txt
    	fout.open("records.txt");
    	
    	// tests the output stream
    	if ( fout.fail() )
    	{
    		cerr << "Error opening file records.txt for reading.  Aborting!"
    			 << endl
    			 << endl;
    		exit(1);
    	}
    
    
    	fin.close();
    	fout.close();
    
    
    }// end main()
    
    
    
    int readScores(ifstream& fin, char lname[][LNAME_SIZE + 1], 
    			    char fname[][FNAME_SIZE + 1], char grades[][GRADES_SIZE + 1])
    {
    	int length = 0;
    	
    
    	fin.getline(lname[length], LNAME_SIZE + 1, ' ');
    	while ( !fin.eof() )
    	{
    		fin.getline(fname[length], FNAME_SIZE + 1, ' ');
    		fin.getline(grades[length], GRADES_SIZE + 1, ' ');
    		length++;
    		fin.getline(lname[length], LNAME_SIZE + 1, ' ');
    	}// end while
    
    
    	cout << length << endl << endl;
    	for (int i = 0; i < length; i++)
    	{
    		cout << lname[i] << " ";
    		cout << fname[i] << " ";
    		cout << grades[i] << " ";
    	}
    
    	cout << endl << endl;
    
    
    	return(length);
    }// end readScores()
    
    
    
    
    
    void sortRecords(char lname[][LNAME_SIZE + 1], int length)
    {
    	int minPos;
    
    	// finds minimum position
    	for (int i = 0; i < length; i++) {
    		if (lname[i] < lname[minPos]) {
    		minPos = i;
    		}
    	}
    
    
    }// end sortRecords()
    I have no clue what to do next for the sortRecords. Like I said, my book is of no help. If someone could please help, it would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i looked at your code and it seems to me that u have
    the function prototypes in 2 places

    and your main() cant both take void and get void

    it should get an int instead

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    i looked at your code and it seems to me that u have
    the function prototypes in 2 places
    Where? I don't see the prototypes in two places.

    and your main() cant both take void and get void
    Yes, it can.

    I think you should look into a sorting algorithm like bubblesort, or quick sort or something. Just do a search on google.

  4. #4
    hew
    hew is offline
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    I did a search on google and it didn't help. As for the bubble sort or quick sort, I haven't been over those in my class. All we have done is a linear search and a selection sort, and we have to use a selection sort. All my teachers examples of selection sort have used integers, not characters, but he wants us to sort characters out. Thanks for the help though. If anyone else can give me a hint or help of any kind, I would appreciate it. Thanks in advance.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    it should take an int because the functions are returning int's

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    it should take an int because the functions are returning int's
    No, it should be taking an int because that's standard. What you said was that it can't (not shouldn't) take an int. I simply corrected you.

    If you need to sort chars, cast int on them and compare as if they were int.

    Assuming the first character of the last name is uppercase, (if not, use toupper in ctype.h) do the following. (or something similar)

    Code:
    if ((int) lname[i][0] < (int) lname[ii][0])
    {
    	switch(); //Do whatever action nessicary;
    }
    I'm not sure of the way selection sort works.. but I think you get the idea.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i meant in that code!

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Talking The best way to do it ...

    I hope that you get helped from our friends Above ....

    believe me ... the best way to do it ... is to get a new book...
    that will do the first step...
    and I hope you do the same thing that I am doing ....
    2 hours everyday .... on this GREAT web site...
    Read and Read and Read other problems.....

    You Got it now
    C++
    The best

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Why don't you use it as Class

    try to make a class of :
    name
    Array[ number of grads]
    then make an array of this class
    it is easier this way....

    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-17-2009, 10:48 PM
  2. Selection sort, not sorting
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2007, 12:17 PM
  3. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 05:03 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Selection Sort confusion
    By Crankit211 in forum C Programming
    Replies: 5
    Last Post: 12-01-2001, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21