Apr 30, 2012 at 5:14pm UTC
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
void InputArray(string names[],int size);
void selectionSort(string [],int);
void showArray(string [], int);
//Main function
int main()
{
const int SIZE=20;
//Initializing array with given data
string names[SIZE];
int n;
cout<<"Enter number of names:";
cin>>n;
InputArray(names, n);
//Display the values
cout<<"The unsorted values are\n";
showArray(names,n);
//Sort the array
selectionSort(names,n);
//Display the values again
cout<<"The sorted values are\n";
showArray(names,n);
system("pause");
return 0;
}//End of Main
void selectionSort(string names[],int size)
{
int startScan,minIndex;
string minValue;
for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=names[startScan];
for(int index=startScan+1;index<size;index++)
{
/*if current indexed value less than minValue*/
if(names[index].compare(minValue)<0)
{
minValue=names[index];
minIndex=index;
}
}//End of inner-loop
names[minIndex]=names[startScan];
names[startScan]=minValue;
}//End of main loop
}//End of function
/*********************************************************
* showArray *
* This function displays the content of the array. *
*********************************************************/
void showArray(string names[],int size)
{
//Displaying data
for(int count=0;count<size;count++)
cout<<names[count]<<endl;
}//End of function
void InputArray(string names[],int size)
{
//Displaying data
for(int count=0;count<size;count++)
cin>>names[count];
}//End of function
May 1, 2012 at 1:50am UTC
You should learn some forum etiquette. I didn't really see an explanation of your problem. The lack of code tags makes it hard to read, as well. Fix these issues, and I'm sure you'll get some help