[go: up one dir, main page]

0% found this document useful (0 votes)
31 views19 pages

C Language (Day-09)

The document is about the National Institute of Electronics & Information Technology Gorakhpur Center. It provides contact information for the center including its website, social media accounts, and address. It also announces that it will cover topics about matrix (2D array) operations with examples.

Uploaded by

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

C Language (Day-09)

The document is about the National Institute of Electronics & Information Technology Gorakhpur Center. It provides contact information for the center including its website, social media accounts, and address. It also announces that it will cover topics about matrix (2D array) operations with examples.

Uploaded by

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

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Matrix (2D Array) Operation with examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Multidimensional Array

i row
a[0][0] a[0][1] a[0][2]
j column a[i][j]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Notation of Multidimensional Array

Array type Array name

51, 52, 53 Row 1


B= int b[2][3] = {(51, 52, 53),(54, 55, 56)};
54, 55, 56 Row 2
First row second row
Two rows

Col 1 Col 2 Col 3


Three columns
Algebraic notation C notation

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example to add two matrix entered by the user and print it.
#include<stdio.h>
#include<conio.h>
void main() for(i=0 ; i<3 ; i++)
{ {
int a[3][3],b[3][3],c[3][3]; for(j=0 ; j<3 ; j++)
int i,j; {
clrscr(); c[i][j]=a[i][j]+b[i][j];
printf(“enter the elements in both the array:”); printf(“%d”,c[i][j]);
for(i=0 ; i<3 ; i++) }
for(j=0 ; j<3 ; j++) printf(“\n”);
scanf(“%d”,&a[i][j]); }
for(i=0 ; i<3 ; i++) getch();
for(j=0 ; j<3 ; j++) }
scanf(“%d”,&b[i][j]);

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], i,j;
1 2 3
clrscr();
printf(“enter the elements in the array”); 4 5 6
for(i=0 ; i<3 ; i++) 7 8 9
for(j=0 ; j<3 ; j++)
scanf(“%d”,&a[i][j]);
for(j=0 ; i<3 ; i++)
{ 1 4 7
for(i=0 ; j<3 ; j++) 2 5 8
printf(“%2d”,a[j][i]); 3 6 9
printf(“\n”);
}
getch();
} http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
#include<stdio.h>
#include<conio.h>
void main()
{
int x[3][3], i,j;
1 2 3
clrscr();
printf(“enter the elements in the array”); 4 5 6
for(i=0 ; i<3 ; i++) 7 8 9
for(j=0 ; j<3 ; j++)
scanf(“%d”,&x[i][j]);
for(j=0 ; i<3 ; i++)
1 5 9
for(i=0 ; j<3 ; j++)
if(i==j)
printf(“%d”,x[i][j]);
getch();
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


#include<stdio.h> s=0;
#include<conio.h> for(k=0;k<3;k++)
void main() {
{ s=s+a[i][k]*b[k][j];
int a[3][3],b[3][3],k,i,j,s,c[3][3]; c[i][j]=s;
clrscr(); }
printf("enter element in array a"); }
for(i=0;i<3;i++) printf("\nMultiplication of Array A *
for(j=0;j<3;j++) B");
scanf("%d",&a[i][j]); for(i=0;i<3;i++)
printf("enter element in array b"); {
for(i=0;i<3;i++) printf("\n");
for(j=0;j<3;j++) for(j=0;j<3;j++)
scanf("%d",&b[i][j]); printf("\t%d",c[i][j]);
for(i=0;i<3;i++) }
for(j=0;j<3;j++) getch();
{ }
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
 We cannot delete any element from an array.
 If we don't know that how many elements have to be stored in a memory in
advance
 then there will be memory wastage if large array size is specified.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


 It is used to represent multiple data items of same type by using only
single name.
 It can be used to implement other data structures like linked lists, stacks,
queues, trees, graphs etc.
 Multidimensional arrays are used to represent matrices.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


 We must know in advance that how many elements are to be stored in
array.
 Array is static structure. It means that array is of fixed size. The memory
which is allocated to array can not be increased or reduced.
 Since array is of fixed size, if we allocate more memory than requirement
then the memory space will be wasted. And if we allocate less memory
than requirement, then it will create problem.
 The elements of array are stored in consecutive memory locations. So
insertions and deletions are very difficult and time consuming.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. What will be the output of the following C 2. What will be the output of the following C code?
code? #include <stdio.h>
#include <stdio.h> void main()
void main() {
{ int a[2][3] = {1, 2, 3, , 4, 5};
int a[2][3] = {1, 2, 3, 4, 5}; int i = 0, j = 0;
int i = 0, j = 0; for (i = 0; i < 2; i++)
for (i = 0; i < 2; i++) for (j = 0; j < 3; j++)
for (j = 0; j < 3; j++) printf("%d", a[i][j]);
printf("%d", a[i][j]); }
} a) 1 2 3 junk 4 5
a) 1 2 3 4 5 0 b) Compile time error
b) 1 2 3 4 5 junk c) 1 2 3 0 4 5
c) 1 2 3 4 5 5 d) 1 2 3 3 4 5
d) Run time error Answer: b
Answer: a

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

3. What will be the output of the following C code?


#include <stdio.h> 4. What will be the output of the following C code?
void foo(int *ary[]); #include <stdio.h>
int main() int main()
{ {
int ary[2][3];
foo(ary); int ary[2][3][4], j = 20;
} ary[0][0] = &j;
void foo(int *ary[]) printf("%d\n", *ary[0][0]);
{ }
int i = 10, j = 2, k; a) Compile time error b) 20
ary[0] = &i;
ary[1] = &j; c) Address of j d) Undefined behaviour
*ary[0] = 2; Answer: a
for (k = 0;k < 2; k++)
printf("%d\n", *ary[k]); 5. What are the applications of a multidimensional
}
array?
a) 2 2
b) Compile time error a) Matrix-Multiplication
c) Undefined behaviour b) Minimum Spanning Tree
d) 10 2 Answer: a c) Finding connectivity between nodes
d) All of the mentioned Answer: d
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

6. What is the advantage of a multidimensional array over pointer array?


a) Predefined size b) Input can be taken from user
c) Faster Access d) All of the mentioned Answer: d

7. Which is true for a, if a is defined as “int a[10][20];”?


a) a is true two-dimensional array
b) 200 int-sized locations have been set aside
c) The conventional rectangular subscript calculation 20 * row + col is used to find the element a[row, col].
d) All of the mentioned Answer: d

8. Which is true for b, if b is defined as “int *b[10];”?


a) The definition only allocates 10 pointers and does not initialize them
b) Initialization must be done explicitly
c) The definition only allocates 10 pointers and does not initialize them & Initialization must be done
explicitly
d) Error Answer: c

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

9. An array of similar data types which themselves are a collection of dissimilar data type are
___________
a) Linked Lists b) Trees
c) Array of Structure d) All of the mentioned Answer: c

10. Comment on an array of the void data type.


a) It can store any data-type
b) It only stores element of similar data type to first element
c) It acquires the data type with the highest precision in it
d) You cannot have an array of void data type Answer: d

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Assignments
 Write a program to print the anti-main diagonal of 2D array.
 Program to Transpose of Matrix
 Program to check Matrix is a Symmetric Matrix or Not.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like