tony selcuk
tony selcuk

Reputation: 687

Accessing the rows and columns of a multidimensional array

How can I access the rows and columns of a multidimensional array. I want to make a function that reads the rows and columns of an multi dimensional array. For example row = 0 output would be {7.5, 7.0, 6.3, 0.8, 0.5, 1.2, 2.3, 3.5, 4.3, 5.0, 5.5, 6.7} and column 2 output would be {6.3, 6.5, 6.2, 5.8, 5.0}.

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}
    return 0;
}

Upvotes: 0

Views: 285

Answers (2)

Neil
Neil

Reputation: 1922

Since it is initialised, the [5] from Rainfall_amounts[5][12] can be omitted; [] and the compiler will figure it out. This is good practice if you ever want to add more years without changing the constant. Later, one can use a static sizeof to programmatically determine the size in bytes. cdecl may be helpful to figure out how to use sizeof to compute the size of the individual elements.

This code uses the use-case that functions or structures are discouraged; one just wants to print it once. There are many other equally-valid considerations.

#include <stdio.h>

int main(void) {
    const float rainfall_amounts[][3] = {
        {7.5, 7.0, 6.3},
        {8.3, 7.2, 6.5}
    };
    const size_t years = sizeof rainfall_amounts / sizeof *rainfall_amounts,
        months = sizeof *rainfall_amounts / sizeof **rainfall_amounts;
    size_t i;

    /* Row 1. */
    for(i = 0; i < months; i++) {
        printf("%s%.1f", i ? ", " : "", rainfall_amounts[1][i]);
    }
    printf(".\n");

    /* Column 1. */
    for(i = 0; i < years; i++) {
        printf("%s%.1f", i ? ", " : "", rainfall_amounts[i][1]);
    }
    printf(".\n");

    return 0;
}

The fprintf man page says ".#" is "the number of digits to appear after the radix character." "%s" inserts a ", " before every one except the first.

Upvotes: 1

sidcoder
sidcoder

Reputation: 460

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

Upvotes: 0

Related Questions