0% found this document useful (0 votes)
9 views64 pages

C - Day 5

The document provides an overview of arrays in programming, including one-dimensional, two-dimensional, and three-dimensional arrays, along with their declaration and initialization. It also covers sorting and searching techniques, demonstrating sorting logic through an animated example and providing sample code for sorting and searching elements in an array. Additionally, it discusses the use of pointers with arrays.

Uploaded by

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

C - Day 5

The document provides an overview of arrays in programming, including one-dimensional, two-dimensional, and three-dimensional arrays, along with their declaration and initialization. It also covers sorting and searching techniques, demonstrating sorting logic through an animated example and providing sample code for sorting and searching elements in an array. Additionally, it discusses the use of pointers with arrays.

Uploaded by

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

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

Declaration Syntax: a[0] -45


a[1] 6
datatype array-name[size]; a[2] 0
Declaration Example: a[3] 72
a[4] 1543
int a[10]; a[5] -89
a[6] 0
a[7] 62
a[8] -3
a[9] 1

Position number of the element


within array a
6/27/2024
One Dimensional Arrays Initialization
Array Name
Initialization:
int a[10]={1,2,3,4,5,6,7,8,9,10}; a[0] 1
a[1] 2
a[2] 3
a[3] 4
a[4] 5
a[5] 6
a[6] 7
a[7] 8
a[8] 9
a[9] 10

Position number of the element


within array a
6/27/2024
Program to read and display n numbers
using array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i;
printf(“Enter the number of elements:\n”); Output:
scanf(“%d”,&n); Enter the number of elements
printf(“Enter the elements of array:\n”); 5
for(i=0;i<n;i++) Enter the elements of array:
{ 5
scanf(“%d”,&a[i]); 4
} 6
printf(“The elements of array are:\n”); 27
for(i=0;i<n;i++) 15
{ The elements of array are:
printf(“%d”,a[i]); 5 4 6 27 15
}
getch();
}
6/27/2024
Two Dimensional Arrays
• Contains two subscripts
Declaration Syntax:
datatype Column 0 Column 1 Column 2
name[rows][columns]; Row 0 a[0][0] a[0][1] a[0][2]

Declaration Example: a[1][0] a[1][1] a[1][2]


Row 1
int a[2][3];

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

Position number of the element


within array a
6/27/2024
Two Dimensional Arrays Initialization
Initialization: Array Name

int a[2][5]={{0,1,2,3,4},{5,6,7,8,9}}; a[0][0] 0


a[0][1] 1
a[0][2] 2
a[0][3] 3
a[0][4] 4
a[1][0] 5
a[1][1] 6
a[1][2] 7
a[1][3] 8
a[1][4] 9

Position number of the element


within array a
6/27/2024
Three Dimensional Array
• Contains three subscripts
Declaration:
int a[2][3][4];

6/27/2024
Sorting
Sorting
• Arranging the elements of array in ascending
or descending order

6/27/2024
An Animated Example

int a[6]={28, 23, 45, 14, 6, 33};

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

• Accessing Array Elements Using Pointer:

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

We want to search 33 in the following list

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.

• Write a program for addition of two matrices


using functions.

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

• Write a program to compute the sum of diagonal


element of a square matrix

6/27/2024

You might also like