Advertisement

12.13.2006 at 01:02AM PST, ID: 22091578
[x]
Attachment Details

passing 2D array as a parameter in C

Asked by wonjun in C Programming Language

Tags: array, c, 2d

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;
            }
      }
}
Start Free Trial
 
Loading Advertisement...
 
[+][-]12.13.2006 at 01:31AM PST, ID: 18129213

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: array, c, 2d
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 1
Solution Grade: B
 
 
[+][-]12.14.2006 at 06:55PM PST, ID: 18144049

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.07.2007 at 10:47AM PST, ID: 18262671

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32