Thread: how to compare 2d array rows seperately

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    7

    how to compare 2d array rows seperately

    hey guys i need a little help here, I am trying to write a program that prints a 2d array to the screen and then compares each row and column to see if there is any pairs of letters. basically something that looks like this:
    ---------------------------
    V T A Y U C B
    D F W Q Q C R
    D A L Y M F G
    O A S S D T I

    Number of horizontal pairs: 2
    Number of vertical pairs: 3
    ---------------------------

    so far this is my code below, but i dont know how to approach the comparisons .. please help!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main( void )
    {  
    
    int x = 0;
    int y = 0;
    int countX = 0;
    int countY = 0;
    srandom( (unsigned) time(NULL) );
    char array[x][y]; 
    
    
    for (x=0;x<20;x++)
        {
         for (y=0; y<30; y++)
          {
           array[x][y] = random() % 26 + 65;
           printf("%c ",array[x][y]);
          }
    printf("\n");
        }
    printf ("\n\n");
    
    return ( 0 ) ;
    
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You aren't allocating any memory for your 2d array. The values of x and y are zero.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    how should i go about doing that? just:

    Code:
    char array[x][y]=0;
    ?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    The values in your brackets are what determine the size of the array.

    Code:
    char array[row_length][column_length];
    Right now x = 0 and y = 0 so you are basically saying this

    Code:
    char array[0][0];

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    yes i understand up to that point, and then when it runs through the for statement it ups the x and y values every time.. all of that works flawlessly. its just the comparison of the output that i dont know how to do

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You are declaring a static array, meaning once you declare your array you cannot change the size of it. Your for loop has nothing to do with the memory of your array. You need to specify the size of your array and then initialize the values using your nested for loop. Once you get that taken care of, then you can worry about comparisons.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    how do i do that?

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    how is what i did not the same as what the lesson is teaching? it looks basically the same..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sal, heads up!

    You are making an array with no size! Static array's do NOT expand or grow, in C. array[10][10] will ALWAYS have 10 rows, with 10 elements across each row. Neither more nor less. Camel man is giving you the straight scoop.

    Without a size able to hold your values, the program will simply crash "segmentation fault", after a small amount of scrounged up free memory is used up.

    The comparisons have to be two ways. First, in a nested pair of for loops, go row by row, comparing each value to it's neighbor. Then in another nested pair of for loops, repeat what you did for the row checking, but put the columns index on the outer for loop, and the row index on the inside (so the rows are being treated like the columns were on the first nested pair, and vice-versa).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP 2d array sort rows by its sum value
    By Fromar123 in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2012, 06:50 AM
  2. counting rows in a 2D array
    By Kyoukoku in forum C Programming
    Replies: 12
    Last Post: 09-22-2010, 10:26 AM
  3. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM
  4. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  5. Specify number of rows in an array??
    By niponki in forum C Programming
    Replies: 5
    Last Post: 07-31-2005, 05:41 AM

Tags for this Thread