troubleshooting Question

passing 2D array as a parameter in C

Avatar of wonjun
wonjun asked on
C
2 Comments1 Solution5810 ViewsLast Modified:
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
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros