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