wonjun
asked on
passing 2D array as a parameter in C
hi,
i have a working code here, but my problem is that i'm doing "void SORTIT(char* aWords[][MAX_STRING_LENGTH ]);"
but the question asks us to do with "void SORTIT(char *array[])"
i couldn't get my code to work if i implemented after declaring it as "void SORTIT(char *array[])"
because the array indexing gets out of hand and i coudln't figure it out.
I'd really appreciate it if one of you experts could get this to run correctly using "void SORTIT(char *array[])"
Thank you very much.
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 20
#define NUM_WORDS 20
void SORTIT(char* aWords[][MAX_STRING_LENGTH ]);
int main()
{
char aWords[NUM_WORDS][MAX_STRI NG_LENGTH] ;
FILE *pFile;
pFile=fopen("INPUT.TXT","r ");
if (pFile == NULL)
{
printf("\n file cannot be opened");
return 0;
}
else
{
int i = 0;
int iNumWords = 0;
char cTempWord[MAX_STRING_LENGT H];
while( fgets(cTempWord, MAX_STRING_LENGTH, pFile) != NULL)
{
strcpy(aWords[iNumWords], cTempWord);
++iNumWords;
}
fclose(pFile);
//print
printf("Before Sorting\n");
for(i = 0; i < iNumWords; ++i)
{
printf("%s", aWords[i]);
}
SORTIT(aWords);
pFile = fopen ("SORTED.TXT", "w");
//print
printf("\n\nAfter Sorting\n");
for(i = 0; i < iNumWords; ++i)
{
printf("%s", aWords[i]);
}
return 1;
}
}
//Bubble Sort
void SORTIT(char aWords[][MAX_STRING_LENGTH ])
{
int i, j;
int bIsSorted;
int iArrayLength;
char aTemp[MAX_STRING_LENGTH];
iArrayLength = sizeof(aWords)/sizeof(aWor ds[0][MAX_ STRING_LEN GTH]);;
for(i = iArrayLength - 1; i > 0; i--)
{
bIsSorted=1;
for(j = 0; j < i; j++)
{
if(strcmp(aWords[j], aWords[j+1]) > 0)
{
strcpy(aTemp, aWords[j]);
strcpy(aWords[j], aWords[j+1]);
strcpy(aWords[j+1], aTemp);
bIsSorted=0;
}
}
if(bIsSorted == 1)
{
//Sorted, Exit
break;
}
}
}
i have a working code here, but my problem is that i'm doing "void SORTIT(char* aWords[][MAX_STRING_LENGTH
but the question asks us to do with "void SORTIT(char *array[])"
i couldn't get my code to work if i implemented after declaring it as "void SORTIT(char *array[])"
because the array indexing gets out of hand and i coudln't figure it out.
I'd really appreciate it if one of you experts could get this to run correctly using "void SORTIT(char *array[])"
Thank you very much.
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 20
#define NUM_WORDS 20
void SORTIT(char* aWords[][MAX_STRING_LENGTH
int main()
{
char aWords[NUM_WORDS][MAX_STRI
FILE *pFile;
pFile=fopen("INPUT.TXT","r
if (pFile == NULL)
{
printf("\n file cannot be opened");
return 0;
}
else
{
int i = 0;
int iNumWords = 0;
char cTempWord[MAX_STRING_LENGT
while( fgets(cTempWord, MAX_STRING_LENGTH, pFile) != NULL)
{
strcpy(aWords[iNumWords], cTempWord);
++iNumWords;
}
fclose(pFile);
printf("Before Sorting\n");
for(i = 0; i < iNumWords; ++i)
{
printf("%s", aWords[i]);
}
SORTIT(aWords);
pFile = fopen ("SORTED.TXT", "w");
printf("\n\nAfter Sorting\n");
for(i = 0; i < iNumWords; ++i)
{
printf("%s", aWords[i]);
}
return 1;
}
}
//Bubble Sort
void SORTIT(char aWords[][MAX_STRING_LENGTH
{
int i, j;
int bIsSorted;
int iArrayLength;
char aTemp[MAX_STRING_LENGTH];
iArrayLength = sizeof(aWords)/sizeof(aWor
for(i = iArrayLength - 1; i > 0; i--)
{
bIsSorted=1;
for(j = 0; j < i; j++)
{
if(strcmp(aWords[j], aWords[j+1]) > 0)
{
strcpy(aTemp, aWords[j]);
strcpy(aWords[j], aWords[j+1]);
strcpy(aWords[j+1], aTemp);
bIsSorted=0;
}
}
if(bIsSorted == 1)
{
//Sorted, Exit
break;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
used strdup to allocate space for words, added a null to indicate the end, and checked to be sure we don't add more words than will fit