I need to put the names entered by the user in alphabetical order. What are my variables to declare? Im wanting to use something like this in my code but do not know what my array is or anything. selection_sort(arr1, arrSize);

using namespace std;

int main ()
    string temp;
    int numStrings = 0, maxStrings = 40;
    string names[maxStrings];
    cout << "Enter Student using Last Name First (done to stop)" << endl;
    while ((temp!="done") && (numStrings < maxStrings))
          names[numStrings++] = temp;
          cout << "Enter another student (or done)"<< endl;
    for (int j = 0; j < numStrings; j++)
        cout << j << " " << names[j] << endl;
 //void selection_sort(int arr1[9], int size)
    // int index, temp;
    // for (int i = 0; i < size-1 ; i++ ) 
       //  index = i; 
       //  for (int j = i+1; j < size ; j++)
        //     if (arr1[j] > arr1[index]) index = j;
       //  temp = arr1[index];
       //  arr1[index] = arr1[i];
       //  arr1[i] = temp;

    return 0;
6 Years
Discussion Span
Last Post by stevanity

The code is pretty messy.
From the looks of the sorting code u are using selection sort. Ok

Here is a basic algorithm for the sort. But u need to change the variables according to ur required datatype i.e string.

here is a general code for selection sort.
Modify the datatypes for ure needs (i.e String)
I hope u can understand the logic behind the technique.

For more clear explanation: http://en.wikipedia.org/wiki/Selection_sort

void selectionSort(int *array,int length)//selection sort function 
	int i,j,min,minat;

      for(j=i+1;j<(length);j++) //select the min of the rest of array
		  if(min>array[j])   //ascending order for descending reverse
			  minat=j;  //the position of the min element 
	  int temp=array[i] ;
	  array[i]=array[minat];  //swap 


And your array is a string array. and the size of the array will be equal to numStrings.
and also u will have a problem when u use names starting with capital letters and small letters together. So think of a solution to that problem. I think u can get an idea if u read more about the C++ string object. http://www.cplusplus.com/reference/string/string/ Good Luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.