Referencing a 2D array in order to sort strings
Oct 30, 2014 at 7:48pm Oct 30, 2014 at 7:48pm UTC
I am utterly confused on how to set up a function that references a 2D array and compares he strings of each.
How do I fix this.
Here is my code for sorting function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
char BubbleSort (char Names [], int NumElements)
{
int i;
bool Sorted;
char Temp;
do {
Sorted = true ;
NumElements--;
i++;
strcmp (Names[i], Names[i + 1]);
if (Names [i] > Names [i + 1])
{
Sorted = false ;
strcpy(Names[i], Names[i + 1]);
}
else ;
} while (!Sorted);
return (Names[i], NumElements);
}
And here is the code to call up the function in the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
char BubbleSort(char Names [], int NumElements);
void main()
{
const int MaxNames(20);
const int MaxLetters(15);
char Names [MaxNames + 1] [MaxLetters + 1];
int NumRows;
int Row;
int Col;
int NumCols = MaxLetters;
int x;
int y;
cout << "How many names shall you enter?" << endl;
NumRows = ReadInteger ();
cout << "Enter " << NumRows << " names: " << endl;
for (Row = 0; Row < NumRows; Row++)
{
cin.getline (Names [Row], MaxLetters + 1, '\n' );
}
cout << "You entered " << endl;
for (Row = 0; Row < NumRows; Row++)
{
cout << Names[Row];
cout << endl;
}
for (x = 0; x < NumRows; x++)
{
BubbleSort (Names [] [NumCols], NumRows);
}
}
And my header file for sorting:
1 2 3 4 5 6
#ifndef SORTING_H
#define SORTING_H
char BubbleSort (char [], int );
#endif
Oct 30, 2014 at 8:53pm Oct 30, 2014 at 8:53pm UTC
For starters
main()
must always be declared as
int main {}
never as type
void
or it will never be executed.
Nov 1, 2014 at 7:34pm Nov 1, 2014 at 7:34pm UTC
Also, you have duplicates declarations of
char BubbleSort (char Names [], int NumElements)
. You do not need a header file.
Here with some corrections:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
char BubbleSort(char Names [], int NumElements);
int main()
{
const int MaxNames(20);
const int MaxLetters(15);
char Names [MaxNames + 1] [MaxLetters + 1];
int NumRows;
int Row;
int Col;
int NumCols = MaxLetters;
int x;
int y;
cout << "How many names shall you enter?" << endl;
NumRows = ReadInteger ();
cout << "Enter " << NumRows << " names: " << endl;
for (Row = 0; Row < NumRows; Row++)
{
cin.getline (Names [Row], MaxLetters + 1, '\n' );
}
cout << "You entered " << endl;
for (Row = 0; Row < NumRows; Row++)
{
cout << Names[Row];
cout << endl;
}
for (x = 0; x < NumRows; x++)
{
BubbleSort (Names [] [NumCols], NumRows);
}
}
NumElements)
{
int i;
bool Sorted;
char Temp;
do {
Sorted = true ;
NumElements--;
i++;
strcmp (Names[i], Names[i + 1]);
if (Names [i] > Names [i + 1])
{
Sorted = false ;
strcpy(Names[i], Names[i + 1]);
}
else ;
} while (!Sorted);
return (Names[i], NumElements);
}
Also, you cannot return an array nor can you loop a return statement:
return (Names[i], NumElements);
. You would have to store the result and return a single variable.