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

1. ## 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. 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