An example how to code this is shown below.
Please note that there are many different (and better) ways, how to do this. Anyway, it works and it should give you an idea, about what you can do.
COMMENT (personal preference):
- I would consider to store the matrix as a vector in C, like the
vector includes first column 0, followed by column 1, followed by
column 2, ... (in the case below with m=5 and n=12, this means vector[60]).
- Then, you access [row, column]-entry, just by vector[row+m*column].
- Next, I would consider just to define the vector (which will hold the matrix) as a pointer, and dynamically allocate its size (using malloc).
#include <stdio.h>
void print_row_or_col(int m, int n, float matrix[m][n], int row, int col, int choose){
int i;
if(choose==0) // print row
{
if(!(row<m) || row==-1) return; // return if row exceeds size
for(i=0;i<n;i++) printf("%.1f ",matrix[row][i]);
}
if(choose==1) // print column
{
if(!(col<n) || col==-1) return; // return if col exceeds size
for(i=0;i<m;i++) printf("%.1f ",matrix[i][col]);
}
printf("\n");
}
int main ()
{
float Rainfall_amounts[5][12] = {
{7.5, 7.0, 6.3, 0.8, 0.5, 1.2, 2.3, 3.5, 4.3, 5.0, 5.5, 6.7},
{8.3, 7.2, 6.5, 1.5, 0.5, 1.6, 2.0, 3, 4.38, 4, 5.3, 6.0},
{7.7, 7.3, 6.2, 0.9, 1.8, 1.3, 2.8, 3.8, 5, 5.5, 5.8, 5.9},
{7.0, 7.0, 5.8, 0.8, 1.1, 1.8, 3.4, 4.0, 5.0, 5.2, 5.8, 6.7},
{8.2, 6.1, 5.0, 1.2, 0.5, 2.3, 4.5, 3.5, 4.0, 5.0, 5.5, 6.7}};
print_row_or_col(5, 12, Rainfall_amounts, 0, -1, 0);
print_row_or_col(5, 12, Rainfall_amounts, -1, 2, 1);
return 0;
}
Output:
7.5 7.0 6.3 0.8 0.5 1.2 2.3 3.5 4.3 5.0 5.5 6.7
6.3 6.5 6.2 5.8 5.0
printf("%d ", Rainfall_amounts[row][0]); printf("%d ", Rainfall_amounts[row][1]); printf("%d ", Rainfall_amounts[row][2]);etc. But that would be awful code. Or do you mean use a different loop type such aswhile?