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)
*/
Re: Pointers/Linked lists
Quote:
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.