Link to home
Start Free TrialLog in
Avatar of wonjun
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_STRING_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_LENGTH];

          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(aWords[0][MAX_STRING_LENGTH]);;
      
      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
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I declared SORTIT take a char *array[], declared aWords as char *aWords[NUM_WORDS+1];
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