C - Day 5
C - Day 5
6/27/2024
RECAP
6/27/2024
Topics
• Array
• One Dimensional Array
• Two Dimensional Array
• Three Dimensional Array
• Sorting
• Searching
6/27/2024
Arrays
6/27/2024
Arrays Introduction
• Collection of similar type of data
• Derived data type
• Elements of arrays are stored at contiguous
memory locations
6/27/2024
Types of Arrays
• One Dimensional
• Multi Dimensional
• Two Dimensional
• Three Dimensional
6/27/2024
One Dimensional Arrays
• Contains One subscript Array Name
6/27/2024
Memory Representation of 2-D Array
Array Name
a[0][0] -45
a[0][1] 6
a[0][2] 0
a[0][3] 72
a[0][4] 1543
a[1][0] -809
a[1][1] 0
a[1][2] 62
a[1][3] -3
a[1][4] 1
6/27/2024
Sorting
Sorting
• Arranging the elements of array in ascending
or descending order
6/27/2024
An Animated Example
28 23 45 14 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[1]) True
28 23 45 14 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[1]) True
Swap
23 28 45 14 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[2]) False
No Swap
23 28 45 14 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[3]) True
23 28 45 14 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[3]) True
Swap
14 28 45 23 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[4]) True
14 28 45 23 6 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[4]) True
Swap
6 28 45 23 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[0]>a[5]) False
No Swap
6 28 45 23 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[2]) False
No Swap
6 28 45 23 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[3]) True
6 28 45 23 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[3]) True
Swap
6 23 45 28 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[4]) True
6 23 45 28 14 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[4]) True
Swap
6 14 45 28 23 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[1]>a[5]) False
No Swap
6 14 45 28 23 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[2]>a[3]) True
6 14 45 28 23 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[2]>a[3]) True
Swap
6 14 28 45 23 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[2]>a[4]) True
6 14 28 45 23 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[2]>a[4]) True
Swap
6 14 23 45 28 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[2]>a[5]) False
No Swap
6 14 23 45 28 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[3]>a[4]) True
6 14 23 45 28 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[3]>a[4]) True
Swap
6 14 23 28 45 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[3]>a[5]) False
No Swap
6 14 23 28 45 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[4]>a[5]) True
6 14 23 28 45 33
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
if(a[4]>a[5]) True
Swap
6 14 23 28 33 45
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
An Animated Example
Sorted Array:
6 14 23 28 33 45
a[0] a[1] a[2] a[3] a[4] a[5]
6/27/2024
Sorting Logic
for(i=0;i<n; i++)
{
for(j=i+1;j<n; j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
6/27/2024
Program to sort an integer array
#include<stdio.h> if(a[i]>a[j])
#include<conio.h> {
void main() t=a[i];
{ a[i]=a[j];
int a[50],n,i,j,t; a[j]=t;
printf(“Eneter the size of array:\n”); }
scanf(“%d”,&n); }
printf(“Enter %d elements:\n”,n); }
for(i=0;i<n;i++) printf(“Sorted Array:\n”);
{ for(i=0;i<n;i++)
scanf(“%d”,&a[i]); {
} printf(“%d\t”,a[i]);
for(i=0;i<n;i++) }
{ getch();
for(j=i+1;j<n;j++) }
{
6/27/2024
Output
Enter the size of array:
6
Enter 6 elements:
8
6
25
4
15
7
Sorted Array:
4 6 7 8 15 25
6/27/2024
Array Using Pointers
Array Using Pointers
• Declare an array
• Pointer Declaration
6/27/2024
Array Using Pointers
6/27/2024
Searching
Searching
• Process of finding an element in an array.
6/27/2024
Linear Searching Example
6/27/2024
Searching Logic
for(i=0;i<n; i++)
{
if(n==a[i])
{
t=1;
break;
}
}
6/27/2024
Program to search an element from a list of elements
#include<stdio.h> for(i=0;i<n;i++)
#include<conio.h> {
void main() if(m==a[i])
{ {
int a[50],n,i,j,t,m; t=1;
printf(“Enter the size of array:\n”); p=i+1;
scanf(“%d”,&n); break;
printf(“Enter %d elements:\n”,n); }
for(i=0;i<n;i++) }
{ if(t==1)
scanf(“%d”,&a[i]); printf(“Element is found at
} %d position”,p);
printf(“Enter the element that you want else
to search:\n”); printf(“Element not found”);
scanf(“%d”,&m); getch();
}
6/27/2024
Output
Enter the size of array:
6
Enter 6 elements:
8
6
25
4
15
7
Enter the element that you want to search:
15
Element is found at 5 position
6/27/2024
Program to find the addition of two matrices
#include<stdio.h> {
#include<conio.h> scanf(“%d”,&b[i][j]);
}
void main() }
{
int a[5][5],b[5][5],c[5][5],m,n,i,j; for(i=0;i<m;i++)
printf(“Enter the order of matrices:\n”); {
scanf(“%d%d”,&m,&n); for(j=0;j<n;j++)
{
printf(“Enter first matrix:\n”); c[i][j]=a[i][j]+b[i][j];
for(i=0;i<m;i++) }
{ }
for(j=0;j<n;j++) printf(“The result matrix is:\n”);
{ for(i=0;i<m;i++)
{
scanf(“%d”,&a[i][j]); for(j=0;j<n;j++)
} {
} printf(“%d”,c[i][j]);
printf(“Enter second matrix:\n”); }
for(i=0;i<m;i++) printf(“\n”);
}
{ getch();
for(j=0;j<n;j++) }
6/27/2024
Output
Enter the order of matrices: The result matrix is:
2 6 10 7
3 10 6 15
Enter first matrix:
4
7
2
6
5
9
Enter second matrix:
2
3
5
4
1
6
6/27/2024
Program to find the multiplication of two matrices
#include<stdio.h> scanf(“%d”,&a[i][j]);
}
#include<conio.h> }
printf(“Enter second matrix:\n”);
void main()
for(i=0;i<o;i++)
{ {
for(j=0;j<p;j++)
int a[5][5],b[5][5],c[5][5],m,n,o,p,i,j,s; {
printf(“Enter the order of first matrix:\n”); scanf(“%d”,&b[i][j]);
}
scanf(“%d%d”,&m,&n); }
printf(“Enter the order of second matrix:\n”); for(i=0;i<m;i++)
{
scanf(“%d%d”,&o,&p); for(j=0;j<p;j++)
if(n!=o) {
s=0;
{ for(k=0;k<n;k++)
printf(“Multiplication not possible”); {
s=s+a[i][k]*b[k][j];
break; }
} c[i][j]=s;
}
else }
{ printf(“The result matrix is:\n”);
for(i=0;i<m;i++)
printf(“Enter first matrix:\n”); {
for(i=0;i<m;i++) for(j=0;j<p;j++)
{
{ printf(“%d”,c[i][j]);
for(j=0;j<n;j++) }
printf(“\n”);
{ }
getch();
}
6/27/2024
Output
Enter the order of first matrix: Enter second matrix:
2 2
3 3
Enter the order of second matrix: 5
3 4
2 1
Enter first matrix: 6
4
7 The result matrix is:
2 45 52
6 46 92
5
9
6/27/2024
QUIZ
6/27/2024
Quiz
• How many elements will be stored in the array
a[10][5].
(a) 10 (b) 50
(c) 150 (d) 500
Ans: b
• When an array is declared, C automatically
initializes its elements to zero.
a. True
b. False
Ans: b
6/27/2024
Quiz
• The elements of an array are stored in
a. Continuous memory locations
b. Discontinuous memory locations
c. Randomly allocated memory locations
d. None of these
Ans: a
• An array is used for storage of ____________
Homogeneous
data.
0
• The array index in C language starts with ____.
6/27/2024
Programs for Hands-on
Programs
• Write a program in C to input ten integers and
print their average.
• Write a program to find out the second largest
element from a given list of integers.
• Write a program to find the sum, product, and
average and the largest and smallest elements
from a given list of elements.
• Write a C function that reverses the elements
of an array.
]
6/27/2024
Programs
• Develop a program to multiply two matrices.
Input the order of matrices from the user.
Your program should take care of the fact that
no element of either matrix can be negative.
6/27/2024
Programs
• Write a program in C that accepts N × N matrix as
input and prints the transpose of this matrix
• Write a C program to display the index of the
smallest and largest element in a 10 × 20 matrix of
integers.
• Write a program in C to read a 5 × 4 matrix using ar
(ii) Sum of all the elements of the matrix
6/27/2024