0

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;
}
6
  • 2
    without a for loop. Why?? You can by writing the same line of code 12 times. 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 as while? Commented Feb 9, 2021 at 22:10
  • I am new to C so I guess I will just delete the last section without the for loop. Its ok if a loop is used. Commented Feb 9, 2021 at 22:20
  • Do you know how to use a loop to iterate a one dimensional array? Commented Feb 9, 2021 at 22:21
  • That I dont really know. Commented Feb 9, 2021 at 22:24
  • 1
    Well then you need to hit a text book or tutorial. That's basic C that is taught in any of those resources. We can spoon feed you an answer but it will be more beneficial for your own long term prospects to learn how to learn effectively. Commented Feb 9, 2021 at 22:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.