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

Lecture-6 Data Structures CSE242

The document discusses multidimensional arrays. It defines multidimensional arrays as collections of linear arrays of similar data types stored in contiguous memory locations. It provides examples of 2D and 3D arrays and how to initialize and access elements within these arrays using indexing. The document also demonstrates how to store, access, and process elements of multidimensional arrays through examples calculating the sum and product of matrices. It concludes with an assignment to write a program to calculate various sums from elements of a 2D array.

Uploaded by

JANGLI GAMERS
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture-6 Data Structures CSE242

The document discusses multidimensional arrays. It defines multidimensional arrays as collections of linear arrays of similar data types stored in contiguous memory locations. It provides examples of 2D and 3D arrays and how to initialize and access elements within these arrays using indexing. The document also demonstrates how to store, access, and process elements of multidimensional arrays through examples calculating the sum and product of matrices. It concludes with an assignment to write a program to calculate various sums from elements of a 2D array.

Uploaded by

JANGLI GAMERS
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE-242

Data Structures
c t ure
Le
6 By
Dr Sudeshna Chakraborty

Department of CSE, School of Engineering and Technology


Lecture -6

Contents
• Array – Definition
• Multidimensional Array
• Multidimensional Array elements
• Multidimensional Array storing and accessing

Department of CSE, School of Engineering and Technology


Lecture -6

Array
Array
• Collection of similar types of data items/elements
Multi
One
• Contiguous memory locations
Dimensional
Dimensional

• Linear or one dimensional

• Multi dimensional

Department of CSE, School of Engineering and Technology


Lecture -6

Multidimensional Array
• Collection of Linear arrays – 2D Arrays
• Collection of 2D - Arrays – 3D Arrays

111 112 113

Department of CSE, School of Engineering and Technology


Lecture -6

Multidimensional Array Elements


111 112 113
int A[3][3][3] = {111, 112, 113, 121, 122, 123,
131, 132, 133, ....., 332, 333};

int A[3] = {111, 112, 113};

111 112 113


121 122 123

int A[2][3] = {111, 112, 113, 121, 122, 123};


Or
int A[2][3] = {
{111, 112, 113},
121, 122, 123}
};

Department of CSE, School of Engineering and Technology


0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements 6 7 8

Method 1: 9 10 11
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 12 13 14
22, 23};
15 16 17

Better Method: 18 19 20
int x[2][3][4] =
{ 21 22 23
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, 0 1 2
{20,21,22,23} } 3 412 513 14
}; 6 715 816 17
9 1018 1119 20
21 22 23

Department of CSE, School of Engineering and Technology


0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements 6 7 8
int x[2][3][4] = 12 13 14
9 10 11
{ 15 16 17
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} } 18 19 20
};
21 22 23
for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for (int k = 0; k < 4; ++k)
            {
                printf("Element at x[%d][%d[%d] = %d”, i, j, k, x[i][j][k]
                     ;
            }
        }
    }

Department of CSE, School of Engineering and Technology


Lecture -6

Multidimensional Array storing, accessing and processing


// C program to find the sum of two matrices of
for (int i = 0; i < 2; ++i)
order 2*2 Enter elements of 1st matrix
for (int j = 0; j < 2; ++j)
#include <stdio.h>
{ Enter a11: 2;
int main()
result[i][j] = a[i][j] + b[i][j]; Enter a12: 0.5;
{
} Enter a21: -1.1;
float a[2][2], b[2][2], result[2][2];
printf("Enter elements of 1st matrix\n"); Enter a22: 2;
// Displaying the sum
for (int i = 0; i < 2; ++i)
printf("\nSum Of Matrix:"); Enter elements of 2nd matrix Enter b11: 0.2;
for (int j = 0; j < 2; ++j) Enter b12: 0;
{ Enter b21: 0.23;
for (int i = 0; i < 2; ++i)
printf("Enter a%d%d: ", i + 1, j + 1); Enter b22: 23;
for (int j = 0; j < 2; ++j)
scanf("%f", &a[i][j]);
{ Sum Of Matrix:
}
printf("%.1f\t", result[i][j]); 2.2 0.5
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
if (j == 1) -0.9 25.0
for (int j = 0; j < 2; ++j)
printf("\n");
{
}
printf("Enter b%d%d: ", i + 1, j + 1);
return 0;
scanf("%f", &b[i][j]);
}
}

Department of CSE, School of Engineering and Technology


Lecture -6

Multidimensional Array storing, accessing and processing

Department of CSE, School of Engineering and Technology


Lecture -6

Multidimensional Array storing, accessing and processing


#include<stdio.h>     printf("multiply of the matrix=\n");    
int main(){   for(i=0;i<r;i++)  {    
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;     for(j=0;j<c;j++)  {    
printf("enter the number of row=");     mul[i][j]=0;    
scanf("%d",&r);     for(k=0;k<c;k++) {    
printf("enter the number of column=");     mul[i][j]+=a[i][k]*b[k][j];    
scanf("%d",&c);     }    
printf("enter the first matrix element=\n");     }    
for(i=0;i<r;i++)   {     }    
for(j=0;j<c;j++)  {     //for printing result    
scanf("%d",&a[i][j]);     for(i=0;i<r;i++)    {    
}     for(j=0;j<c;j++)   {    
}     printf("%d\t",mul[i][j]);    
printf("enter the second matrix element=\n"); }    
for(i=0;i<r;i++)   {     printf("\n");    
for(j=0;j<c;j++)  {     }    
scanf("%d",&b[i][j]);     return 0;  
}     }  

Department of CSE, School of Engineering and Technology


Lecture -6

Assignment
Write a C program to find sum of specific elements of given 2D Array of nxn where n is odd:
1. Sum of both diagonals such that the central element is considered only once.
2. Sum of middle row and middle column elements such that the central element is considered only once.
3. Sum of corner elements.

Department of CSE, School of Engineering and Technology

You might also like