Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
106 views

C Programming Multidimensional Arrays

C programming allows for multidimensional arrays, or arrays of arrays. For example, a 2D array can store elements in rows and columns. Multidimensional arrays can be initialized in different ways, such as providing all values inline or row by row. The document includes an example of adding two 2x2 matrices using a multidimensional array to store the values, taking input from the user, and outputting the sum.

Uploaded by

John Mark Carpio
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

C Programming Multidimensional Arrays

C programming allows for multidimensional arrays, or arrays of arrays. For example, a 2D array can store elements in rows and columns. Multidimensional arrays can be initialized in different ways, such as providing all values inline or row by row. The document includes an example of adding two 2x2 matrices using a multidimensional array to store the values, taking input from the user, and outputting the sum.

Uploaded by

John Mark Carpio
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming Multidimensional Arrays

C programming language allows programmer to create arrays of arrays known as multidimensional


arrays. For example:

float a[2][6];

Here, a is an array of two dimension, which is an example of multidimensional array.

For better understanding of multidimensional arrays, array elements of above example can be
thinked of as below:

Initialization of Multidimensional Arrays


In C, multidimensional arrays can be initialized in different number of ways.

int c[2][3]={{1,3,0}, {-1,5,9}};

OR

int c[][3]={{1,3,0}, {-1,5,9}};

OR

int c[2][3]={1,3,0,-1,5,9};

Initialization Of three-dimensional Array

double cprogram[3][2][4]={

{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},

{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},

{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}


};

Suppose there is a multidimensional array  arr[i][j][k][m] . Then this array can hold i*j*k*m
numbers of data.

Similarly, the array of any dimension can be initialized in C programming.

Example of Multidimensional Array In C


Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays
where, elements of matrix are entered by user.

#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n'
dimension, 'n' numbers of loops are needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
return 0;
}

Ouput

Enter the elements of 1st matrix

Enter a11: 2;

Enter a12: 0.5;


Enter a21: -1.1;

Enter a22: 2;

Enter the elements of 2nd matrix

Enter b11: 0.2;

Enter b12: 0;

Enter b21: 0.23;

Enter b22: 23;

Sum Of Matrix:

2.2 0.5

-0.9 25.0

You might also like