Nick
Nick

Reputation: 213

2D array function logic

I created this program that scans a 2D array for vertical and horizontal pairs, where row = 20 and column = 30 , and then displays the output.

I think I may have messed up my logic in Functions 2 & 3. Can anyone check it for me and help me figure out what I did wrong? I would greatly appreciate it.

Full Program Code:

    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h> 

    #define ROW 20
    #define COLUMN 30

    void intro_msg (void);    
    char function1 (char [ROW] [COLUMN]);
    char function2 (char [ROW] [COLUMN]);
    char function3 (char [ROW] [COLUMN]);
    void function4 (int , int);
    void goodbye_msg (void);

int main( void )
{  // MARKS THE BEGINNING OF THE main( ) BLOCK OF STATEMENTS

   char randchar_array [ROW] [COLUMN]; 
   int hor_pairs = 0 , vert_pairs = 0;
   srandom ( (unsigned) time (NULL) ); 

    intro_msg ( ) ;

    function1 (randchar_array);

    hor_pairs = function2 (randchar_array);

    vert_pairs = function3 (randchar_array);

    function4 (hor_pairs , vert_pairs); 

    goodbye_msg ( ) ;

    return ( 0 ) ;

}    // MARKS THE END OF THE main( ) BLOCK OF STATEMENTS

void   intro_msg   (void) 
{
    printf( "\n Welcome user, this program creates a 2D array of characters " 
            "that has 20 rows\n and 30 columns and then populates it with random"
            " characters. The program then\n displays the number of vertical "
            "and horizontal pairs in the array.\n\n"); 
    return ;
}

char function1 (char randchar_array[ROW] [COLUMN]) 

{
    int r = 0 ;
    int c = 0 ;
    char nextchar = 'A';

     for (r = 0; r < ROW ; r++)
     {
         for (c = 0; c < COLUMN ; c++)
         {
            nextchar = 'A' + random() % 26;
            randchar_array [r] [c] = nextchar;
            printf(" %c", nextchar);
         }

         printf("\n");
      }

      return (randchar_array [ROW] [COLUMN]);
}

char function2 (char randchar_array[ROW] [COLUMN]) 

{
    int r = 0 ;
    int c = 0 ;
    int horpairs = 0;

     for (r = 0; r < ROW -1; r++)
     {
         for (c = 0; c < COLUMN ; c++)
         {
           {
           if(randchar_array[r][c] == randchar_array[r][c+1])
             horpairs++;
           }
         }
      }

      return (horpairs);
}

char function3 (char randchar_array[ROW] [COLUMN]) 

{
    int r = 0 ;
    int c = 0 ;
    int vertpairs = 0;

     for (r = 0; r < ROW ; r++)
     {
         for (c = 0; c < COLUMN -1; c++)
         {
           {
           if(randchar_array[r][c] == randchar_array[r+1][c])
             vertpairs++;
           }
         }
      }

      return (vertpairs);
}

void function4 (int hor_pairs, int vert_pairs) 
{
    printf ( " \n Number of horizontal pairs: %d /n" , hor_pairs ); 
    printf ( " \n Number of vertical pairs: %d /n" , vert_pairs ); 
    return ;
}

void   goodbye_msg   (void) 
{
    printf ( "\n Thank you for using this program.\n\n") ; 
    return ;
}

Upvotes: 0

Views: 273

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

You made the same mistake twice:

 for (c = 0; c < COLUMN ; c++)
 {
     if(randchar_array[r][c] == randchar_array[r][c+1])

Here, and:

for (r = 0; r < ROW ; r++)
{
    for (c = 0; c < COLUMN -1; c++)
    {
        if(randchar_array[r][c] == randchar_array[r+1][c])

You're for looping though all elements of the char array, where c and r will increment right up to COLUMN -1 and ROW -1 respectively. But in the aforementioned loops, you increment either c or r by 1 in the right hand operand of the if statement.
The last time your loop runs, this means you're actually going out of bounds, and accessing (or at leastr try to access):

randchar_array[r][COLUMN]
//and
randchar_array[ROW][c]

This is wrong. Either change your loop to:

for(c = 0;c < COLUMN -1;++c)

Or, for a more micro-optimized way

for(c = COLUMN - 1;c > 0; --c)

The latter loop is iterating the chars back to front, of course, so you'll have to take that into account.
Note that in the last loop (for(c = COLUMN - 1;c > 0; --c)), c won't ever hit 0, but That's because it needn't, since, when c = 1, you're comparing it to c-1 anyway...

Your code could end up looking something like:

int function2(char randchar_array[ROW][COLUMN])
{
    int r,c, horpairs = 0;
    for (r = 0; r < ROW ; ++r)
    {
        for (c = COLUMN -1; c > 0 ; --c)
        {
            if(randchar_array[r][c] == randchar_array[r][c-1]) horpairs++;
        }
    }
    return horpairs;
}
//and
int function3(char randchar_array[ROW][COLUMN])
{
    int r,c, vertpairs = 0;
    for (r = ROW - 1; r > 0 ; --r)
    {
        for (c = 0; c < COLUMN; ++c)
        {
            if(randchar_array[r][c] == randchar_array[r-1][c]) vertpairs++;
        }
    }
    return vertpairs;
}

Upvotes: 2

Constantin
Constantin

Reputation: 8961

for (r = 0; r < ROW -1; r++)
{
  for (c = 0; c < COLUMN ; c++)
    {
      if(randchar_array[r][c] == randchar_array[r][c+1])
      horpairs++;
    }
}

ROW-1 in the loop condition and c+1 in the Loop body will produce an Array-out-of-bounds Exception. Either you want COLUMN-1 instead of ROW-1 or you want r+1 instead of c+1.

Both function2() and function3() sharing that Problem. I guess you want:

char function2 (char randchar_array[ROW] [COLUMN]) {
  int r = 0 ;
  int c = 0 ;
  int horpairs = 0;
  for (r = 0; r < ROW ; r++){
    for (c = 0; c < COLUMN-1 ; c++){
      if(randchar_array[r][c] == randchar_array[r][c+1])
      horpairs++;
    }
  }
  return (horpairs);
}

char function3 (char randchar_array[ROW] [COLUMN]) {
  int r = 0 ;
  int c = 0 ;
  int vertpairs = 0;
  for (r = 0; r < ROW -1; r++){
    for (c = 0; c < COLUMN ; c++){
      if(randchar_array[r][c] == randchar_array[r+1][c])
        vertpairs++;
    }
  }
  return (vertpairs);
}

Upvotes: 2

Related Questions