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

Lecture-6 Data Structures CSE242

The document discusses multidimensional arrays. It defines multidimensional arrays as collections of linear arrays organized in multiple dimensions. It provides examples of 2D and 3D arrays and demonstrates how to initialize, access, and process elements within multidimensional arrays using for loops. The document also includes an example C program that stores two 2D matrices in arrays, calculates their sum, and prints the result.

Uploaded by

JANGLI GAMERS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture-6 Data Structures CSE242

The document discusses multidimensional arrays. It defines multidimensional arrays as collections of linear arrays organized in multiple dimensions. It provides examples of 2D and 3D arrays and demonstrates how to initialize, access, and process elements within multidimensional arrays using for loops. The document also includes an example C program that stores two 2D matrices in arrays, calculates their sum, and prints the result.

Uploaded by

JANGLI GAMERS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE-242

Arrays
Data Structures
c t ure
Le
6 By
Dr. Sudeshna Chakraborty

1
Department of CSE, School of Engineering and Technology
Lecture -6

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

2
Department of CSE, School of Engineering and Technology
Lecture -6
Arra
Array y
Arrays
• Collection of similar types of data items/elements
Multi
• Contiguous memory locations One Dimensiona
Dimensional l
• Linear or one dimensional

• Multi dimensional

3
Department of CSE, School of Engineering and Technology
Lecture -6

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

111 112 113

4
Department of CSE, School of Engineering and Technology
Lecture -6

Multidimensional Array Elements


111 112 113 Arrays
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}
};

5
Department of CSE, School of Engineering and Technology
0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements 6
Array
7
s
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

6
Department of CSE, School of Engineering and Technology
0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements
int x[2][3][4] =
6 7
12 Array
8
13 s 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]
                     ;
            }
        }
    }
7
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
Arrays
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]);
}
}
8
Department of CSE, School of Engineering and Technology
Lecture -6

Multidimensional Array storing, accessing and processing


Arrays

9
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");    
Arrays
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;  
}     }  

10
Department of CSE, School of Engineering and Technology
Lecture -6

Assignment
Arrays
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.

11
Department of CSE, School of Engineering and Technology

You might also like