Help with 2d arrays

This is a discussion on Help with 2d arrays within the C Programming forums, part of the General Programming Boards category; Hi there i need some help manipulating a 2d array. For example say i have an 2d array Code: int ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    19

    Help with 2d arrays

    Hi there
    i need some help manipulating a 2d array.
    For example
    say i have an 2d array
    Code:
    int array[20][21]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  //Crop this
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Crop this
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Crop this
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Crop this
                                   {0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
                                   {0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
                                   {1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                                   {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                                   {1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
                                   {0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
                                   {0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                                   {1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
                                   {0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
                                   {1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                                   {1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
                                   {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Crop this
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // Crop this
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; // Crop this
    I want to crop any 2d array like the one above matrix i get so that the matrix that start with with a column that has 1's in it. and end with a column that has one int it. ie if it has a column with all zeros at the begining or end it should crop it out..
    to provide an 2d array like the following:
    Code:
                                 {{0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
                                   {0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
                                   {1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                                   {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                                   {1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
                                   {0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
                                   {0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                                   {1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
                                   {0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
                                   {1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                                   {1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
                                   {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1}};
    help me please...
    thanks cheer
    Last edited by Salem; 05-24-2008 at 06:55 AM. Reason: Code tags in the right places

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Please do NOT put comments inside the code tags - ONLY put the code inside the code tags.
    And why not just copy data into a new array then?
    Last edited by Elysia; 05-24-2008 at 06:03 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    19

    sorry about the comments

    [/code]
    Hi well technically i can't because i don't know the size of the array i'm using.. i will jst have to open the file they give me and the program will have to do that for me.
    [code]

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's good you listened to Elysia about the code tags ... er not.

    A simple loop that traverses the array, and if you didn't find a non-zero don't add it to the 2nd array...

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    19
    Quote Originally Posted by zacs7 View Post
    It's good you listened to Elysia about the code tags ... er not.

    A simple loop that traverses the array, and if you didn't find a non-zero don't add it to the 2nd array...
    Hi the not following about traverse, i'm just a beginner so can you go though it with me...
    please.

    thanks in advance

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Oh but it is, how are you going to 'see' what's in the array without traversing it?

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    By traversing the array he basically means going through each element reading the content.

    You could then have four variables: x_start, x_end, y_start, and y_end. When reading the array you would want to find these four points.

    When you are finished reading you could then crop the array to everything between these co-ordinates.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I read his post a little differently. I thought he wanted to save just the rows that contained at least one value of 1, in them.


    Code:
    for(row = 0, j = 0; row < MaxRowsInOriginalArray; row++)   {
       goodrow = 0;
    
       for(col = 0; col < MaxColsInOriginalArray; col++)   {
          if(OriginalArray[row][col] == 1)
            goodrow = 1;                 //mark this row to be saved
            break;      
       }
       if(goodrow == 1)   {
          for each col - as above.
              NoZeroArray[j++][col] = OriginalArray[row][col];
    
       }
    }
    To work like this, the NoZeroArray must have the same number of columns as the OriginalArray has. It may need the same number of rows, IF the OriginalArray has no rows that are all zero's.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    19

    thank bro you got the idea i was impleamenting

    Quote Originally Posted by Adak View Post
    I read his post a little differently. I thought he wanted to save just the rows that contained at least one value of 1, in them.


    Code:
    for(row = 0, j = 0; row < MaxRowsInOriginalArray; row++)   {
       goodrow = 0;
    
       for(col = 0; col < MaxColsInOriginalArray; col++)   {
          if(OriginalArray[row][col] == 1)
            goodrow = 1;                 //mark this row to be saved
            break;      
       }
       if(goodrow == 1)   {
          for each col - as above.
              NoZeroArray[j++][col] = OriginalArray[row][col];
    
       }
    }
    To work like this, the NoZeroArray must have the same number of columns as the OriginalArray has. It may need the same number of rows, IF the OriginalArray has no rows that are all zero's.
    what is wrong with my code
    it is print out junk values for some reason
    Code:
    #include <stdio.h>
    
    int main(int argc,char *argv[]){
    int row;
    int col;
    int goodrow=0;
    int j;
    int array[20][21]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
                      {0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
                      {1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                      {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                      {1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
                      {0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
                      {0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                      {1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
                      {0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
                      {1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                      {1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
                      {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
        int resultarray[14][20]={};
    
    for(row = 0, j = 0; row < 20; row++)   {
       goodrow = 0;
    
       for(col = 0; col < 21; col++)   {
          if(array[row][col] == 1)
            goodrow = 1;                 //mark this row to be saved
            break;
       }
       if(goodrow == 1)   {
          for (col=0;col<21;col++) {
              resultarray[j++][col] = array[row][col];
          }
       }
    }
    for(row=0;row<j;row++){
        for(col=0;col<21;col++){
            printf("%d",resultarray[row][col]);
        }
    printf("\n");
    }
    return 0;
    }
    cheer bro

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You students nowadays, you expect code I post to be close to actually working!

    Code:
    #include <stdio.h>
    
    int main(int argc,char *argv[])   {
       int row, col, j;
       int goodrow=0;
       int array[20][21]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
                         {0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
                         {1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                         {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                         {1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
                         {0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
                         {0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                         {1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
                         {0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
                         {1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                         {1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
                         {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    
       int resultarray[14][21] = {{ 0 }};
    
       for(row = 0, j = 0; row < 20; row++)   {
          goodrow = 0;
          for(col = 0; col < 21; col++)   {
             if(array[row][col] == 1)  {
                goodrow = 1;                 //mark this row to be saved
                break;
             }
          }
          if(goodrow == 1)   {
             for (col=0;col<21;col++) {
                resultarray[j][col] = array[row][col];
             }
             j++;
          }
       }
       
       for(row=0;row<j;row++)  {
          for(col=0;col<21;col++)  {
             printf("%d",resultarray[row][col]);
          }
          printf("\n");
       }
       return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    19
    Quote Originally Posted by Adak View Post
    You students nowadays, you expect code I post to be close to actually working!

    Code:
    #include <stdio.h>
    
    int main(int argc,char *argv[])   {
       int row, col, j;
       int goodrow=0;
       int array[20][21]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
                         {0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
                         {1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                         {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
                         {1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
                         {0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
                         {0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                         {1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
                         {0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
                         {1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
                         {1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
                         {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    
       int resultarray[14][21] = {{ 0 }};
    
       for(row = 0, j = 0; row < 20; row++)   {
          goodrow = 0;
          for(col = 0; col < 21; col++)   {
             if(array[row][col] == 1)  {
                goodrow = 1;                 //mark this row to be saved
                break;
             }
          }
          if(goodrow == 1)   {
             for (col=0;col<21;col++) {
                resultarray[j][col] = array[row][col];
             }
             j++;
          }
       }
       
       for(row=0;row<j;row++)  {
          for(col=0;col<21;col++)  {
             printf("%d",resultarray[row][col]);
          }
          printf("\n");
       }
       return 0;
    }
    thanks boss

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    19

    Hi: how to Traverse

    Quote Originally Posted by mike_g View Post
    By traversing the array he basically means going through each element reading the content.

    You could then have four variables: x_start, x_end, y_start, and y_end. When reading the array you would want to find these four points.

    When you are finished reading you could then crop the array to everything between these co-ordinates.
    How would i go on about traversing the the 2d array..
    Thanks in Advance

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You are already traversing the array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    19

    Hi

    Quote Originally Posted by Elysia View Post
    You are already traversing the array.
    Hi there oh ok
    but there is only one problem, my code is too genearlised.
    the width and height of the array will change according to the file i open. so any idea, on how i can manipulate the width and height given that the maximum width is 50 and max height is 80

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You know how much data you're reading, aren't you? Then you know the width and height of your array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 08:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21