Thread: Selection Sort on 2-D character array

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Selection Sort on 2-D character array

    I hope that I posted this correctly. If not, please forgive me. The below program creates a 2-D character array, displays the unsorted values, calls the selectSort function to sort the array, and displays the sorted array. I have worked through all the problems that I encountered creating the program except the 4 below. I have seen similar programs on the board, but still cannot determine the cause of these errors. I have a similar program that worked with integers, similar to the problems that others have posted but cannot figure out the character array.
    I do not know if the errors were corrected if the selectionSort function would work. If you can suggest corrections, could you also determine if the function would work as typed? Any help would be greatly appreciated.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std; 
    
    const int MAX_COLUMNS = 17;
    const int MAX_ROWS = 20;
    
    
    // Function Prototypes
    void selectionSort(char [20] [17], int);
    void showArray(char [20] [17], int);
    
    
    int main()
    {   
    	char names[MAX_ROWS] [MAX_COLUMNS] = {"Collins, Bill",    "Smith, Bart",
    	                                      "Allen, Jim",       "Griffin, Jim",
    					      "Stamey, Marty",    "Rose, Geri",
    					      "Taylor, Terri",    "Johnson, Jill",
    					      "Allison, Jeff",    "Looney, Joe",
    					      "Wolfe, Bill",      "James, Jean",
    					      "Weaver, Jim",      "Pore, Bob",
    					      "Rutherford, Greg", "Javens, Renee",
    					      "Harrison, Rose",   "Setzer, Cathy",
    					      "Pike, Gordon",     "Holland, Beth" };
    
    
    	cout << "The unsorted array values are: " << endl << endl;
    
    	showArray(names, MAX_ROWS); // calls show array function to display unsorted values
    
    	selectionSort(names, MAX_ROWS); // calls selectionSort function to sort values
    
    
    	showArray(names, MAX_ROWS); // calls show array function to display sorted values
    
    //Creates a pause before exiting program
    	cout << endl << endl
    		 << "Press the <ENTER> key to exit the program."
    		 << endl << endl;
    	cin.get();
    
    	cout << endl << endl
    		 << "The sorted array values are: " << endl << endl;
    
    
    	return 0; 
    }
    /*****************************************************************
    * This function performs an ascending order selection sort on    *
    * an array. elems is the number of elements in the array.        *
    *****************************************************************/
    
    void selectionSort(char array [20] [17], int size)
    {
    
    // for(bottom = 0 to size)
    // findMin (input part of the data array)
    // swap bottom with minPos (value with min)
    
    	char minValue [17];
    	int startScan, minIndex;
    	
    	for (startScan = 0; startScan < size - 1; startScan++)
    	{
    		minIndex = startScan; // sets minIndex to zero
    //ERROR ON NEXT LINE (left operand must be l-value)
    		minValue = array[startScan]; // sets minValue to the 1st value in the array
    		for (int index = startScan + 1; index < size; index++)
    		{
    			if (array[index] < minValue) // test to see if current array value is less 
    			{                            // than the current minValue
    //ERROR ON NEXT LINE (left operand must be l-value)
    				minValue = array[index]; // if so, sets minValue equal to current array value
    				minIndex = index;        // captures the index of the new minValue
    			}
    		}
    //ERROR ON NEXT LINE (left operand must be l-value)
    		array[minIndex] = array[startScan];// resets the array[minIndex] to the value in array[startScan] 
    //ERROR ON NEXT LINE (left operand must be l-value)
    		array[startScan] = minValue; // sets array[startScan] to the minValue
    	}
    
    }
    
    /*****************************************************************
    * This function displays the contents of an array. elems is the  *
    * number of elements.                                            *
    *****************************************************************/
    
    void showArray(char array[] [17], int elems)
    {
    	int newLine = 0; // New line counter
    
    	for (int count = 0; count < elems; count++)
    	{
    		cout << setw(20);
    		cout.setf(ios_base::left, ios_base::adjustfield);
    		cout << array[count]; // displays array value
    		
    		if (newLine == 3) // Test for new line, new line after 4 names
    		{
    			cout << endl;// if 3rd is reached, a new line is entered
    			newLine = 0; // if 3rd is reached, newLine counter is reset
    		}
    		else
    			newLine++; // Increment newLine counter between breaks
    
    	}
    }
    
    /* ERRORS
    
    --------------------Configuration: vc Asgn X06 - Win32 Debug--------------------
    Compiling...
    vc Asgn X06.cpp
    F:\C++ 03\vc Asgn X06\vc Asgn X06.cpp(78) : error C2106: '=' : left operand must be l-value
    F:\C++ 03\vc Asgn X06\vc Asgn X06.cpp(83) : error C2106: '=' : left operand must be l-value
    F:\C++ 03\vc Asgn X06\vc Asgn X06.cpp(87) : error C2106: '=' : left operand must be l-value
    F:\C++ 03\vc Asgn X06\vc Asgn X06.cpp(88) : error C2106: '=' : left operand must be l-value
    Error executing cl.exe.
    
    vc Asgn X06.exe - 4 error(s), 0 warning(s)
    
    */

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    When you are working with strings you have to use the string functions to copy and compare values.

    I made a few quick changes and the code worked as expected...

    Now consider what I am telling you and go look at your sort function again.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Tank, if you dont understand what he is talking about, Rog means look at the functions within the <cstring> library. Most accordingly the strcpy() function.
    Last edited by stumon; 07-19-2003 at 11:33 PM.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Smile Porblem Solved

    Thank all of you for responding. Rog, your comments were the perfect example of pointing a struggling programmer in the right direction without doing the work for them. For that I am thankful, but sometimes its easier to understand solution code I struggled with it just a little more. The 4 errors went away fast but I could not get the sort to work until I changed the if statement. The corrected code for that function reads as follows:
    Code:
    void selectionSort(char array [20] [17], int size)
    {
    
    	char minValue [17];
    	int startScan, minIndex;
    	
    	for (startScan = 0; startScan < size - 1; startScan++)
    	{
    		minIndex = startScan; // sets minIndex to zero
    
    		strcpy(minValue, array[startScan]); // sets minValue to the 1st value in the array
    		for (int index = startScan + 1; index < size; index++)
    		{
    			if (strcmp(array[index], minValue) < 0) // test to see if current array value is less 
    			{                            // than the current minValue
    
    				strcpy(minValue, array[index]); // if so, sets minValue equal to current array value
    				minIndex = index;        // captures the index of the new minValue
    			}
    		
    		}
    		strcpy(array[minIndex], array[startScan]);// resets the array[minIndex] to the value in array[startScan] 
    		strcpy(array[startScan], minValue); // sets array[startScan] to the minValue
    	}
    
    }
    Salem,
    your comments have helped me in the past. I did realize that I had the constants but was having problems so I just used the numbers while working the problem. Once the program worked, I was going to change back.

    Thanks again,
    Alan

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Which is why I wrote copy and compare.

    You were so close I just couldn't bring myself to give you anything more than a nudge in the right direction.

    BTW - you can optimize your sort by using an array of pointers to the strings. Instead of moving the strings around in the array, you order the pointers. I hope this makes sence... let me know if you want a little more info.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Pointers/Linked lists

    I really have a tough time with pointers and linked lists. I understad the logic behind them to a point. But in all my C/C++ classes, we never really do more than briefly touch on them. Can anyone recommend an in-depth book (a good one for beginners) on pointers/linked lists?

    Alan

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    78

    Re: Pointers/Linked lists

    Originally posted by TankCDR
    Can anyone recommend an in-depth book (a good one for beginners) on pointers/linked lists?
    You really need to understand pointers before you can understand linked lists. Pointers come to C++ directly from C. In C pointers and arrays are very closely related.

    In my opinion one of the best papers ever written on pointers is Ted Jensen's "A TUTORIAL ON POINTERS AND ARRAYS IN C". It's available at:

    http://www.sundialsoft.freeserve.co.uk/sddocs008.htm

    I think I really started understanding pointers when I read chapter 5 of "The C Programming Language". People tend to either love or hate this book. I guess you know where I stand.

    Take a look at Ted Jensen's paper and let us know if you have any specific questions. I may not be able to answer them all, but I should be able to help you get a grasp in enough to get you over the hump and on your way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2006, 11:12 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. confuse with character array
    By dv007 in forum C Programming
    Replies: 6
    Last Post: 08-09-2002, 01:05 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM