Thread: For loop through a 2D array - just for select rows

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    For loop through a 2D array - just for select rows

    Hi,

    For my system I am using a 2D array called 'matrix', the dimensions of which are specified by the user (no more than 10 for both the Rows and Columns).

    The following for loop allows me to search through the entire matrix where the usere has input a value of 0 or greater;

    Code:
    		for(i=0; i< m; i++) {
    			for(j=0; j< n; j++) {
    
    				if (matrix[i][j] >= 0){
    				printf("\n[%d][%d] = %d\n", i,j, matrix[i][j]);
    					}
    
    				}
    			}
    The m variable is the columns, and the n variable is the rows.

    Since this is the fisrt time I have used a 2D array I am not quite sure how to do a for loop just for all the rows for the first column.

    For instance, say that the user has defined the matrix at 5 x 3, and has entered a value for the first 2 rows of the first colum, and has left the rest of the matrix blank for some reason;
    [0][0]=1
    [1][0]=1
    [2][0]=0
    [3][0]=0
    [4][0]=0

    Also assume that the system knows of the 2 values in the matrix and has stored the 2 in a variable. Where I am stuck I not sure how to run a for loop for the rows so that the index value for the row and column indexes for the inputs, in this case [0][0]=1 and [1][0]=1, will be got so that the user can the apply their choice of bitwise operator ti it.

    To clarify in this example, the user has inputed a value for [0][0] and [1][0], the for loop should then serach the rows for the first column (column[0]) and then get the index values for the rows and columns where there is a value and then allow the user to selct the bitwise operator to aplly; this will be done through the use of a switch statment.

    If this doesn't make sense I wil be sure to try and re-explain,

    Thankyou, You will only have to bear with me until the weekend!!

    Archie

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Code:
    int i;
    for (i = 0; i < m; i++) {
            if (matrix[i][0] >= 0)
                    printf("\n[%d][0] = %d\n", i, matrix[i][0]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM