I have an assigment to make a program which sorts names by abc order. I think it should be done in two-dimensional array but I don't know how to do it. The program code must be written in c or c++.
How to sort a string array in c++ or c?
Tamara evo resenja
#include %26lt;iostream.h%26gt;
#include %26lt;stdlib.h%26gt;
# include %26lt;string%26gt;
void selectionSort(string array[ ], const int arraySize);
int findSmallest(string array[ ], int startIndex, int endIndex);
void swap(string%26amp; first, string%26amp; second);
void print(string array[ ], const int arraySize);
int main( )
{
const int ARRAY_SIZE = 3;
string someArray[ARRAY_SIZE] = {"pera", "aca", "mika"};
selectionSort(someArray, ARRAY_SIZE);
print(someArray, ARRAY_SIZE);
system("PAUSE");
return 0;
}
void selectionSort(string array[ ], const int arraySize)
{
int smallestIndex = 0;
for(int i = 0; i %26lt; arraySize; i++)
{
smallestIndex = findSmallest(array, i, arraySize - 1);
swap(array[i], array[smallestIndex]);
}
}
int findSmallest(string array[ ], int startIndex, int endIndex)
{
int smallestIndex = startIndex;
for(int i = startIndex + 1; i %26lt;= endIndex; i++)
{
if(array[i] %26lt; array[smallestIndex])
smallestIndex = i;
}
return smallestIndex;
}
void swap(string%26amp; first, string%26amp; second)
{
string temp = first;
first = second;
second = temp;
}
void print(string array[ ], const int arraySize)
{
for(int i = 0; i %26lt; arraySize; i++)
cout %26lt;%26lt; array[i] %26lt;%26lt; endl;
}
Reply:simple:
http://www.egr.unlv.edu/~jjtse/codes/arr...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment