0% found this document useful (0 votes)
11 views

Multidimentional Array

Multidimensional arrays allow the storage of arrays within arrays. For example, a 2D array can store rows and columns of data. A multidimensional array is declared with the data type, array name, and size of each dimension enclosed in brackets. Elements can be accessed using indices for each dimension. Multidimensional arrays are useful for problems that can be represented using a matrix structure, such as storing inventory data for multiple products across multiple stores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Multidimentional Array

Multidimensional arrays allow the storage of arrays within arrays. For example, a 2D array can store rows and columns of data. A multidimensional array is declared with the data type, array name, and size of each dimension enclosed in brackets. Elements can be accessed using indices for each dimension. Multidimensional arrays are useful for problems that can be represented using a matrix structure, such as storing inventory data for multiple products across multiple stores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Multidimentional array :

Multidimentional array is termed as arrays of


array or combination of multiple arrays .
Syntax of multidimentional array::
Multidimentional array is generalized by declaring N dimensional arrays as
following :-
Data_types name_of_array[size1][size2][size3]……..[sizeN];
Example can be written as:

Int arr[4][5]; //two dimensional Array


Here , data_types is integar and the name of the array is “arr” having total
size of (4*5) =20 which are defined by multiplying the size of the all
dimensions. This array can store (20*4)=80 bytes at a time as 1 integar size
is 4 bytes .

Int array_3D[3][4][5]; //three dimensional Array


Here , data_types is integar and the name of the array is “array_3D” having
total size of (3*4*5) =60 which are defined by multiplying the size of the all
dimensions. This array can store (60*4)=240 bytes at a time as 1 integar size
is 4 bytes .
Initialization of multidimentional array::
//for 2D array :
Row 1 Row 2 colm 1 colm 2 colm 3

int arr[2][3]={{1, 2, 3},{ 4, 5, 6}}; 1 2 3


4 5 6

fig:2D array as
matrix
indicating row and column.

// expecting input from user


int arr[4][5]; //declaring 2D array;
for(int i=0;i<4;i++) //i is indicating the row size and starts from index 0;
{
for(j=0;j<5;j++) //j is indicating the column size and starts from index 0;
{
scanf("%d",&arr[i][j]);
}
}
//initializing 2D array by dynamically allocation;
int r = 4, c = 5;
int* ptr = malloc((r * c) * sizeof(int));
/* Accessing the array values as if it was a 2D array */
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", ptr[i * c + j]);
printf("\n");
}

In such way , 3D array can be initialized .


Accessing multidimentional array :
Using row index and column index 2D array and also 3D array
can be accessed .
Printing multidimentional array :
For printing elements of the 2D array ,two nested for loops are
used and for 3D array three nested for loops are required .

Problem : A problem of 2D array (multidimentional array )


are given below:
In a book shop of Dhaka and Rajshahi ,there are 3 types of pen is used .In
the shop of Dhaka ,the amount of (A,B,C) three pens are sequentially
10 ,15,12 and in the shop of Rajshahi ,the amount of three pens are
13,10,14 .
In every type of pen the both shopkeepers get profit
1.5, 2.0, 1.25 .

The data is figured as :


A type pen B type pen C type pen
Dhaka 10 15 12
Rajshahi 13 10 14
Profit 1.50 2.00 1.25

Now ,calculate the total profit …


/* for solving the problem we need to use 2D array multiplication .*/
Solution of the pronlem given avobe :
/*For solving the problem three 2D arrays are needed as first one is in integar datatypes and
the second and third one is in float datatypes . The name of the array are shop[2][3] where 2 is
the row indicating Dhaka and Rajshahi and 3 is indicating the amount of pens. Also prophit[3][1]
is indicating the profit percentage in one column .By multiplying the arrays ,we will get the
solution with the third array named result[2][1].then,the elements will be added to give the
total result .*/

//the code is given below .


#include<stdio.h>

int main()

int i,j,k;

float sum=0;

int shop[2][3];

float profit[3][1],result[2][1];

for(i=0; i<2; i++)

for(j=0; j<3; j++)

scanf("%d",&shop[i][j]);

for(i=0; i<3; i++)

for(j=0; j<1; j++)

scanf("%f",&profit[i][j]);

for(i=0; i<2; i++)


{

for(j=0; j<1; j++)

sum=0;

for(k=0; k<3; k++)

sum+=shop[i][k]*profit[k][j];

result[i][j]=sum;

printf("Result :\n");

for(i=0; i<2; i++)

for(j=0; j<1; j++)

printf("%f ",result[i][j]);

printf("\n");

float total ;

total=result[0][0]+result[1][0];

printf("Total profit is : %f\n",total);

return 0;

--THE END--

You might also like