Thread: Remove column of a 2D Array in C by overwriting its elements(shift another column)

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    3

    Lightbulb Remove column of a 2D Array in C by overwriting its elements(shift another column)

    Hey all! So I am watching online tutorials about C. I found an exercise which asked me to write a code in C which "removes" all rows of a matrix whose first element is divisible by 2. I came up with this.


    Code:
                      for (i = 0 ; i < V ; i++) {  /*V = ''height'' of matrix*/
        if (matrica[i][0] % 2 == 0) {       
            for (k = i ; k < V - 1 ; k++) {
                for (j = 0 ; j < S ; j++) {     /* ''S'' width of matrix*/
                    matrica[k][j] = matrica[k+1][j];
                }
            }
           i--;
           V--;
        }
    }
    So if I had a matrix that looked like this:

    1 1 1
    2 2 2
    3 3 3
    4 4 4

    After using this code it would look like this:

    1 1 1
    3 3 3

    However, I cannot seem to figure out if I can do something similar to this which works for columns. Can anyone help out?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what about swapping the row and column indexes isn't working for you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtracting one column in a 2d array from another?
    By zaihed13 in forum C Programming
    Replies: 6
    Last Post: 10-19-2013, 09:29 AM
  2. Adding a new column in array
    By Tomislav Pti in forum C Programming
    Replies: 3
    Last Post: 11-22-2012, 04:48 AM
  3. Sum of elements of row/column of a matrix
    By Pole in forum C Programming
    Replies: 14
    Last Post: 12-31-2011, 02:50 PM
  4. sort elements of 2D matrix by 1st column reference
    By cfdprogrammer in forum C Programming
    Replies: 12
    Last Post: 05-04-2009, 03:26 PM
  5. Replies: 2
    Last Post: 11-19-2006, 05:01 AM

Tags for this Thread