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.

return 0;
}
/*****************************************************************
* This function performs an ascending order selection sort on *
* an array. elems is the number of elements in the array. *
*****************************************************************/

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. *
*****************************************************************/

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

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.

07-20-2003

stumon

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.

07-20-2003

TankCDR

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:

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(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

07-20-2003

Rog

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.

07-20-2003

TankCDR

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

07-20-2003

Rog

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:

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.