I implemented a solution for a homework assignment and everytime i run my program it crashes. could someone tell me what i'm doing wrong?
I so confused what to pass into the functions when using 2D arrays.
Assignment Q:
Implement version 1 of the string sort program
Implement the string sort program based on the following main( ) function.
int main()
{
static char input[MAX_SIZE][MAX_STRLEN + 1];
int size;
size = GetInput(input, MAX_SIZE);
StrSort(input, size);
StrPrint(input, size);
return(0);
}
Read each word using scanf( ) and store the words one on each “row” of the 2-D array. Make the program a filter, just reading from the standard input and writing to the standard output. Prompt or instructions to the user should not be used. Isolate the read string and print string components of the program in separate functions.
My code:
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<stdlib.h>
- //Manifest constants
- #define MAX_SIZE 20
- #define MAX_STRLEN 20
- //Function Prototypes
- int GetInput(static char*, int);
- void StrSort(static char string, int size);
- void StrPrint(static char input, int size);
- int main (void)
- {
- static char input[MAX_SIZE][MAX_STRLEN + 1];
- int size;
- size = GetInput(input, MAX_SIZE);
- StrSort(input, size);
- StrPrint(input, size);
- return(0);
- }
- /*************************************************************************
- *Function: StrSort
- This function sorts a 2d array of strings
- ***********************************************************************/
- void StrSort(static char *string, int size)
- {
- char *plist[MAX_SIZE];
- char *temp;
- int i,j;
- for(i=0; i<size;i++)
- plist[i] = string[i];
- // Sort the array
- for (i=0; i<size; i++)
- for (j=size-1; j>i; j--)
- if (plist[i]>plist[j]) { // We want ascending order, exchange these elements
- temp=plist[i];
- plist[i]=plist[j];
- plist[j]=temp;
- }
- }
- /*************************************************************************
- *Function: StrPrint
- This Functions prints out a 2D array of strings
- ********************************************************************/
- void StrPrint(static char string[MAX_SIZE][MAX_STRLEN+1], int size)
- {
- int i;
- // Display the student grades - after sort
- printf("After sort:\n");
- for (i=0; i<size; i++)
- printf("%02d:\t%s\n", i, string[i]);
- }
- /***********************************************************************8
- *Function: GetInput
- This function reads character strings and stroes it in a 2D array
- *******************************************************************/
- int GetInput(static char *string, int max_size)
- {
- int i=0;
- int rc;
- //Get user input
- do{
- rc= scanf("%s", string[i]);
- i++;
- }
- while (i<MAX_SIZE && string[i] != EOF);
- return(i);
- }