You are on page 1of 322

EST102

PROGRAMMING IN C

MODULE III
SYLLABUS
Arrays and strings
Arrays, Declaration and Initialization, 1-
Dimensional Array, 2-Dimensional Array

String processing: In built String handling


functions (strlen, strcpy, strcat and strcmp,
puts, gets)

Linear search program, bubble sort program


Important!!
➔ 1D Array - Declaration & Initialization
(Program to read and print elements in 1D array)
➔ C Programs - Linear Search, Bubble Sort
➔ 2D Array - Declaration & Initialization
(Program to read and print elements in 2D array /
Matrix)
➔ C Programs - Transpose, Matrix Addition, Matrix
Multiplication
➔ String - Declaration & Initialization
➔ String Handling functions in C
➔ C Programs - using string handling functions, without
using string handling functions
➔ C program - String palindrome, Count number of
vowels...
Arrays
Arrays
• An array is a variable that can store multiple
values of same data type.
• It is defined as the collection of similar type of
data items stored at contiguous memory
locations.
• Arrays are the derived data type which can store
the primitive type of data such as int, char, float
etc.
• Array is the simplest data structure where each
data element can be randomly accessed by using
its index number.
How to declare an Array?
data-type variable-name[size];

• Arrays can be single or multidimensional.


• The number of subscript or index determines the
dimensions of the array.
One Dimensional Arrays
• An array of one dimension is known as a one
dimensional array or 1-D array.
• One dimensional array can be represented as a
single row, where elements are stored one after
another.
data-type variable-name[size];
Example
If we want to store the marks of a student in
6 subjects then we don’t need to define
different variables for the marks in different
subjects. Instead of that, we can define an array
which can store the marks in each subject .
Example
If we want to store the marks of a student in
6 subjects then we don’t need to define
different variables for the marks in different
subjects. Instead of that, we can define an array
which can store the marks in each subject .

float m[6];
Example
If we want to store the marks of a student in
6 subjects then we don’t need to define
different variables for the marks in different
subjects. Instead of that, we can define an array
which can store the marks in each subject .
float m[6];
Index 0 1 2 3 4 5
Values 20.5 30.5 45 50 23 35.5
Element m[0] m[1] m[2] m[3] m[4] m[5]
Initialization of 1-D Arrays
1) Array declaration by specifying size
Eg: int A[10];

2) Array declaration by initializing


elements
Eg: int A[] = { 10, 20, 30,
40 }
3) Array declaration by specifying size and
initializing elements
Initialization at Compile Time
Question No. 1
Write a C program to read and display an
array.
Method 1
#include<stdio.h>
int main()
{
int a[4];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
printf(“a[0]=%d”,a[0]);
printf(“a[1]=%d”,a[1]);
printf(“a[2]=%d”,a[2]);
printf(“a[3]=%d”,a[3]);
return 0;
}
Method 2
#include<stdio.h>
int main()
{
int a[]={10,20,30,40};
printf(“a[0]=%d”,a[0]);
printf(“a[1]=%d”,a[1]);
printf(“a[2]=%d”,a[2]);
printf(“a[3]=%d”,a[3]);
return 0;
}
Method 3
#include<stdio.h>
int main()
{
int a[4]={10,20,30,40};
printf(“a[0]=%d”,a[0]);
printf(“a[1]=%d”,a[1]);
printf(“a[2]=%d”,a[2]);
printf(“a[3]=%d”,a[3]);
return 0;
}
Initialization at Run Time
Question No. 2
Write a C program to read and display an array of
size n.
Program
#include<stdio.h>
int main()
{
int a[50]; //declare an array of size 50
int n,i;
//read the size of the array
printf(“Enter thesize of the array\n”);
scanf(“%d”,&n);
//read elements of an array
printf(“Enter Elements:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
//display elements of an array
printf(“Elements are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,a[i]);
}
return 0;
}
Two Dimensional Arrays
• An array of arrays is known as two dimensional
array or 2-D array.
• The 2-D arrays in C programming is also known
as matrix.
• It is represented as a table of rows and columns.
• A 2-D array can be declared as,
data-type variable-name[no-of-rows][no-of-cols];
Initialization at Compile Time
Question No. 3
Write a C program to read and display matrix of
elements.
Method 1
#include<stdio.h>
int main()
{
int a[2][3]; //2 rows and 3 columns
a[0][0]=10;
a[0][1]=20;
a[0][2]=30;
a[1][0]=40;
a[1][1]=50;
a[1][2]=60;
printf(“a[0][0]=%d”,a[0][0]);
printf(“a[0][1]=%d”,a[0][1]);
printf(“a[0][2]=%d”,a[0][2]);
printf(“a[1][0]=%d”,a[1][0]);
printf(“a[1][1]=%d”,a[1][1]);
printf(“a[1][2]=%d”,a[1][2]);
return 0;
}
Method 2
#include<stdio.h>
int main()
{
int a[2][3]={{10,20,30},{40,50,60}};
printf(“a[0][0]=%d”,a[0][0]);
printf(“a[0][1]=%d”,a[0][1]);
printf(“a[0][2]=%d”,a[0][2]);
printf(“a[1][0]=%d”,a[1][0]);
printf(“a[1][1]=%d”,a[1][1]);
printf(“a[1][2]=%d”,a[1][2]);
return 0;
}
Initialization
Question No. 4
at Run Time
Write a C program to read and display a matrix of size
m x n.
Program
#include<stdio.h>
int main()
{
int a[50][50]; //declare an matrix of rows 50 and columns 50
int m,n,i,j;
//read the number of rows and columns
printf(“Enter the number of rows\n”);
scanf(“%d”,&m);
printf(“Enter the number of columns\n”);
scanf(“%d”,&n);
//read elements of an array/matrix
printf(“Enter Elements:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
//display elements of an array/matrix
printf(“Elements are:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
return 0;
}
Question No. 5
Write a C program to check whether the given
element is present in an array or not.
Program
#include<stdio.h>
int main()
{
int a[10],n,i,k,f;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n");
scanf("%d",&k);
f=0;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
f=1;
break;
}
}
if(f==1)
{
printf("Element is present\n");
}
else
{
printf("Element is not present\n");
}
return 0;
}
Output 1
Enter the size of the array
3
Enter the elements
1
25
48
Enter the element to be searched
25
Element is present
Output 2
Enter the size of the array
4
Enter the elements
10
20
30
40
Enter the element to be searched
58
Element is not present
Question No. 6
Write a C program to display the number of
occurrences of a given element in an array.
Program
#include<stdio.h>
int main()
{
int a[10],n,i,k,count;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n");
scanf("%d",&k);
count=0;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
count++;
}
}
printf("Number of occurrences of the
element=%d\n",count);
return 0;
}
Output
Enter the size of the array
5
Enter the elements
10
20
54
20
20
Enter the element to be searched
20
Number of occurrences of the element=3
Question No. 7
Write a C program to find transpose of a matrix.

Program
#include<stdio.h>
int main()
{
int a[10][10],t[10][10],i,j,m,n;
printf("Enter the number of rows and
columns of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Transpose Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
t[j][i]=a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows and columns of matrix
2
2
Enter the elements
4
5
8
10
Matrix
4 5
8 10
Transpose Matrix
4 8
5 10
Question No. 8
Write a C program to print row sum, column sum
and diagonal sum of a matrix.

Program
#include<stdio.h>
int main()
{
int a[10][10],i,j,m,n,rsum,csum,dsum;
printf("Enter the number of rows and
columns of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
rsum=0;
for(j=0;j<n;j++)
{
rsum=rsum+a[i][j];
}
printf("Sum of elements of %d row = %d\
n",i,rsum);
}
for(i=0;i<n;i++)
{
csum=0;
for(j=0;j<m;j++)
{
csum=csum+a[j][i];
}
printf("Sum of elements of %d column =
%d\n",i,csum);
}
dsum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
dsum=dsum+a[i][j];
}
}
}
printf("Sum of diagonal elements = %d\n",
dsum);
return 0;
}

Output 1
Enter the number of rows and columns of
matrix
3
3
Enter the elements
2
4
6
8
10
12
14
16
18
Matrix:
2 4 6
8 10 12
14 16 18
Sum of elements of 0 row = 12
Sum of elements of 1 row = 30
Sum of elements of 2 row = 48
Sum of elements of 0 column = 24
Sum of elements of 1 column = 30
Sum of elements of 2 column = 36
Sum of diagonal elements = 30
Output 2
Enter the number of rows and columns
2
3
Enter the elements
1
2
3
4
5
6
Matrix:
1 2 3
4 5 6
Sum of elements of 0 row = 6
Sum of elements of 1 row = 15
Sum of elements of 0 column = 5
Sum of elements of 1 column = 7
Sum of elements of 2 column = 9
Sum of diagonal elements=6
Question No. 9
Write a C program to find product of two matrices.

Product of two matrices


• The product of two matrices can be computed
by multiplying elements of the first row of the
first matrix with the first column of the second
matrix then, add all the product of elements.
• Continue this process until each row of the first
matrix is multiplied with each column of the
second matrix.
2⨯ 2 2⨯ 3

2⨯ 3
m1⨯ n1 m2⨯ n2

m1⨯ n2
Program
#include<stdio.h>
int main()
{
int a[10][10], b[10][10], p[10][10], m1, n1,
m2, n2, i, j, k;
printf("Enter the number of rows and
columns of first matrix\n");
scanf("%d%d",&m1,&n1);
printf("Enter the elements\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("First Matrix:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the number of rows and
columns of second matrix\n");
scanf("%d%d",&m2,&n2);
printf("Enter the elements\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Second Matrix:\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
p[i][j]=0;
for(k=0;k<m2;k++)
{
p[i][j]=p[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("Product:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",p[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows and columns of first
matrix
2
2
Enter the elements
1
2
3
4
First Matrix:
1 2
Enter the number of rows and columns of second matrix
2
3
Enter the elements
5
6
7
8
9
10
Second Matrix:
5 6 7
8 9 10
Product:
21 24 27
47 54 61
Question No. 10
Write a C program to find sum of two matrices.
a[i][j] b[i][j]
Sum[i][j]
a[0][1]=2 b[0][1]=2
Sum[0][1]= a[0][1]+b[0][1]=2+2=4
Program
#include<stdio.h>
int main()
{
int a[10][10], b[10][10], Sum[10][10], m1,
n1, m2,n2,i,j;
printf("Enter number of rows and columns
of first matrix\n");
scanf("%d%d",&m1,&n1);
printf("Enter the elements\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("First Matrix:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter number of rows and columns
of second matrix\n");
scanf("%d%d",&m2,&n2);
printf("Enter the elements\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Second Matrix:\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
if((m1==m2) && (n1==n2))
{
printf("Sum of two matrices:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
Sum[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",Sum[i][j]);
}
printf("\n");
}
}
else
{
printf("Addition is not possible\n");
}
return 0;
}
Output 1
Enter number of rows and columns of first
matrix
2
2
Enter the elements
1
2
3
4
First Matrix:
1 2
3 4
Enter number of rows and columns of second
matrix
2
2
Enter the elements
5
6
0
1
Second Matrix:
5 6
0 1
Sum of two matrices:
6 8
3 5
Output 2
Enter number of rows and columns of first
matrix
2
2
Enter the elements
1
0
3
8
First Matrix:
1 0
3 8
Enter number of rows and columns of second matrix
2
3
Enter the elements
10
5
0
6
8
2
Second Matrix:
10 5 0
6 8 2
Addition is not possible
Bubble Sort
• Bubble sort, also referred to as
Comparison sort, is a simple sorting
algorithm.
• It repeatedly goes through the list,
compares adjacent elements and swaps
them if they are in the wrong order.
• Bubble sort is the easiest sorting
algorithm to implement.
• It is an in-place sorting algorithm.
In real life, bubble sort can be
visualised when people in a queue
wanting to be standing in a height wise
sorted manner swap their positions
among themselves until everyone is
standing based on increasing order of
heights.
How Bubble Sort works?
• Bubble sort uses multiple passes (scans)
through an array.
• In each pass, bubble sort compares the
adjacent elements of the array.
• It then swaps the two elements if they are
in the wrong order.
• In each pass, bubble sort places the next
largest element to its proper position.
• In short, it bubbles down the largest
element to its correct position.
Example
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
We need to sort this array in ascending order
using bubble sort.
Expected Output:
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Input :
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
First Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 33
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 33 > 27
=> TRUE
So, we swap A[1] and A[2].
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the third and fourth element.
ie, A[2] and A[3].
Check if A[2] > A[3]
=> 33 > 35
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the fourth and fifth element.
ie, A[3] and A[4].
Check if A[3] > A[4]
=> 35 > 10
=> TRUE
So, we swap A[3] and A[4].
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

Thus, marks the end of the first pass,


where the Largest element reaches its
final(last) position.
Second Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 27
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 27 > 33
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the third and fourth element.
ie, A[2] and A[3].
Check if A[2] > A[3]
=> 33 > 10
=> TRUE
So, we swap A[2] and A[3].
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of second pass,


where the second largest element in the
array has occupied its correct position.
Third Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 27
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 27 > 10
=> TRUE
So, we swap A[1] and A[2].
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of third pass, where


the next largest element in the array has
occupied its correct position.
Fourth Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 10
=> TRUE
So, we swap A[0] and A[1].
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of fourth pass, where


the next largest element in the array has
occupied its correct position.
Fifth Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 10 > 14
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of fifth pass, where


the next largest element in the array has
occupied its correct position.
After the nth pass, the nth largest
element(smallest element) will be at nth last
position (1st position) in the array, where ‘n’ is
the size of the array.

After doing all the passes, the array will be


sorted.

Thus, the sorted array will look like this:


Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Assessment Question
Index 0 1 2 3 4
A 8 22 31 11 5
Value A[0] A[1] A[2] A[3] A[4]
Sort this array in ascending order using bubble
sort.
Expected Output:
Index 0 1 2 3 4
A 5 8 11 22 31
Value A[0] A[1] A[2] A[3] A[4]
Write an algorithm to sort an array in
ascending order using bubble sort.

Problem Statement: To sort an array in


ascending order
using bubble sort.
• Identify the inputs and outputs
Input : - Size of the array, n
- Elements of the
array,
a[0],a[1],a[2],
……….,a[n-1]

Output : Print Sorted Array


• Identify any other data and constants
required to solve the problem
Read the size of the array ‘a’ from user.
Store it in some variable n.
Read all the elements of the array from user.
Starting from index value, i = 0.
Check whether i < n, if it is true, then,
read a[i]
i = i + 1, point to the next index value.
Process is repeated until n elements are
entered by the user.
Perform n - 1 passes in order to sort all elements in the
array.
Start from the value, j = 1
Check whether j < n, if it is true, then start from the
index value, k = 0
Check whether k < n - j, if it is true, then,
If a[k] > a[k + 1], if it is true, then
swap a[k] and a[k + 1]
k=k+1
Otherwise, j = j + 1
Repeat the process until largest element is placed in
correct position.
Repeat the entire process until all elements are sorted.
Print Sorted Array.
Starting from index value, l = 0.
Check whether l < n, if it is true, then,
print a[l]
l = l + 1, point to the next index value.
Process is repeated until n elements are
printed on the screen.
Here, no other data to be computed.
• Identify what needs to be computed
Set j = 1
Check whether j < n, if it is true
Set k = 0
Check whether k < n - j, if it is
true
If a[k] > a[k + 1], then
temp = a[k]
a[k] = a[k + 1]
a[k + 1] =
temp
Set l = 0
Check whether l < n, if it is true
print a[l]
l=l+1
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step _
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step _.
Otherwise, go to Step _.
Step 11 : If a[k] > a[k + 1], then go to
Step _. Otherwise, go to Step _.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step _.
Step 16 : j = j + 1 go to Step _.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step _. Otherwise, go to
Step _.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step _.
Step 21 : Stop
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
How Bubble Sort works?
• Bubble sort uses multiple passes (scans)
through an array.
• In each pass, bubble sort compares the
adjacent elements of the array.
• It then swaps the two elements if they are
in the wrong order.
• In each pass, bubble sort places the next
largest element to its proper position.
• In short, it bubbles down the largest
element to its correct position.
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Draw the flow chart to sort an array in
ascending order using bubble sort.
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n


Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
k=0
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
k=0

Check whether False


k<n-j
True
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
}
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True j=j+1
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False
k<n-j
True j=j+1
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True
Print a[l]
False temp = a[k]
k=k+1 a[k] = a[k+1] l=l+1
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True
Print a[l]
False temp = a[k]
k=k+1 a[k] = a[k+1] l=l+1
a[k+1] = temp

Stop
Program
#include<stdio.h>
int main()
{
int a[20],n,i,j,k,temp,l;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=1;j<n;j++)
{
for(k=0;k<n-j;k++)
{
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
}
printf("Sorted Array\n");
for(l=0;l<n;l++)
{
printf("%d\n",a[l]);
}
return 0;
}
Output
Enter the size of the array
4
Enter the elements
10
50
1
85
Sorted Array
1
10
50
85
Properties of Arrays
• Each element of an array is of same data type
and carries the same size.
• Index value always starts with zero.
• Elements of the array are stored at contiguous
memory locations where the first element is
stored at the smallest memory locations.
• Elements of the array can be randomly accessed.
Advantages of Arrays
• Random access of elements using array
index.
• Use of less line of code as it creates a
single array of multiple elements.
• Easy access to all the elements.
• Traversal through the array becomes easy
using a single loop.
• Sorting becomes easy as it can be
accomplished by writing less line of code.
Disadvantages of Arrays
1) It allows us to enter only fixed number of elements into
it. We cannot alter the size of the array once array is
declared. Hence if we need to insert more number of
records than declared then it is not possible. We should
know array size at the compile time itself.
2) Inserting and deleting the records from the array would
be costly since we add / delete the elements from the
array, we need to manage memory space too.
3) It does not verify the indexes while compiling the array.
In case there is any indexes pointed which is more than
the dimension specified, then we will get run time
errors rather than identifying them at compile time.
Questions
1) Explain the different ways in which you can declare & initialize a single dimensional
array. (3 Marks)
2) Draw a flow chart to find the position of an element in a given sequence, using linear
searching technique. With an example explain how the flowchart finds the position
of a given element. (10 Marks)
3) Write a pseudo code representing the flowchart for linear searching. (4 Marks)
4) Write a C program to find the transpose of a given matrix.(6 Marks)
5) How do you initialize a two dimensional array during declaration? (2 Marks)
6) With the help of a flow chart, explain the bubble sort operation. Illustrate with an
example. (10 Marks)
7) Write a C program to accept a 2-D integer matrix and check whether it symmetric or
not.(3 Marks)
8) Write an algorithm representing the flowchart for bubble sort. (4 Marks)
9) Write a C program to check whether a given matrix is a diagonal matrix. (8 Marks)
10) Write a C program to find the second largest element of an unsorted array. (6 Marks)
11) Write a C program to perform bubble sort. (6 Marks)
12) Write a C program to subtract two matrices. (3 Marks)
13) Write a C program to accept a two dimensional matrix and display the row sum,
column sum and diagonal sum of elements.(5 Marks)
String Processing
String
• Strings are defined as an array of
characters.
• The difference between a character array
and a string is the string is terminated with
a special character ‘\0’.
Declaration of Strings
• Basic syntax for declaring a string.
char variable-name[size];
• variable-name is any name given to the
string variable and size is used define the
length of the string, i.e. the number of
characters strings will store.
• There is an extra terminating character
which is the Null character (‘\0’) used to
indicate termination of string which differs
strings from normal character arrays.
Initializing a String
A string can be initialized in different ways:

1. char str[] = "Programming";


2. char str[50] = "Programming";
3. char str[] = {'P','r','o','g','r','a','m','m','i','n','g','\0'};
4. char str[12] = {'P','r','o','g','r','a','m','m','i','n','g','\0'};
Question No. 1
Write a C program which reads and display a
string.
Method 1
#include<stdio.h>
int main()
{
char a[50]; //declare character array of size 50
//read the string
printf(“Enter the string\n”);
scanf(“%s”,a);
//display the string
printf(“The string entered is:%s\n”,a);
return 0;
}
Method 2
#include<stdio.h>
int main()
{
char a[50]; //declare character array of size 50
int n,i;
//read the string
printf(“Enter the string\n”);
gets(a);
//display the string
puts(a);
return 0;
}
String Handling Functions
strlen Finds out the length strlen(s)
of a string
strcat It appends one strcat(s1,s2)
string at the end of
another.
strcpy Use it for copying a strcpy(s1,s2)
string into another.
strcmp It compares two strcmp(s1,s2)
strings
strrev It reverses a string strrev(s)
Question No. 2
Write a C program to find length of a string using
string handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
int n;
printf("Enter the string:\n");
scanf("%s",a);
n=strlen(a);
printf("Length of the string:%d\n",n);
return 0;
}
Output
Enter the string:
hello
Length of the string:5
Question No. 3
Write a C program to concatenate two strings
using string handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[50],b[50];
printf("Enter first string:\n");
scanf("%s",a);
printf("Enter second string:\n");
scanf("%s",b);
strcat(a,b);
printf("Concatenated string:%s",a);
return 0;
}
Output
Enter first string:
Good
Enter second string:
Morning
Concatenated string:GoodMorning
Question No. 4
Write a C program to copy content of one string
into another using string handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[50],b[50];
printf("Enter first string:\n");
scanf("%s",a);
printf("Enter second string:\n");
scanf("%s",b);
printf("Before copying:\na: %s\nb: %s\n",a,b);
strcpy(a,b);
printf("After copying:\na: %s\nb: %s\n",a,b);
return 0;
}
Output
Enter first string:
hello
Enter second string:
world
Before copying:
a: hello
b: world
After copying:
a: world
b: world
Question No. 5
Write a C program to compare two strings using
string handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[50],b[50];
printf("Enter first string:\n");
scanf("%s",a);
printf("Enter second string:\n");
scanf("%s",b);
if(strcmp(a,b)==0)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
return 0;
}
Output 1
Enter first string:
hi
Enter second string:
hi
Same

Output 2
Enter first string:
good
Enter second string:
Good
Different
Question No. 6
Write a C program to print reverse of a string
using string handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[10];
printf("Enter a string\n");
scanf("%s",a);
strrev(a);
printf("Reverse: %s\n",a);
return 0;
}
Output
Enter a string
hello
Reverse: olleh
Question No. 7
Write a C program to check whether the given
string is palindrome or not using string handling
function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[10],b[10];
printf("Enter a string\n");
scanf("%s",a);
strcpy(b,a);
strrev(a);
if(strcmp(b,a)==0)
{
printf("Palindrome\n");
}
else
{
printf("Not Palindrome\n");
}
return 0;
}
Output 1
Enter a string
malayalam
Palindrome

Output 2
Enter a string
good
Not Palindrome
Question No. 8
Write a C program to count number of vowels in a
given string.
Program
#include<stdio.h>
int main()
{
char a[10];
int i,count;
printf("Enter a string\n");
scanf("%s",a);
count=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||
a[i]=='u'|| a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||
a[i]=='U')
{
count++;
}
}
printf("Number of vowels=%d",count);
return 0;
}
Output
Enter a string
education
Number of vowels=5
Question No. 9
Write a C program to count number of spaces in a
given string.
Program
#include<stdio.h>
int main()
{
char a[10];
int i,count;
printf("Enter a string\n");
gets(a);
count=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
{
count++;
}
}
printf("Number of spaces=%d",count);
return 0;
}
Output
Enter a string
Hi Good morning
Number of spaces=2
Question No. 10
Write a C program to count number of consonants
in a given string.
Program
#include<stdio.h>
int main()
{
char a[10];
int i,count;
printf("Enter a string\n");
gets(a);
count=0;
for(i=0;a[i]!='\0';i++)
{
if((a[i]>='a' && a[i]<='z')||(a[i]>='A' &&
a[i]<='Z'))
{
count++;
}
}
printf("Number of consonants=%d",count);
return 0;
}
Output
Enter a string
Good morning
Number of consonants=11
Question No. 11
Write a C program to count number of vowels,
consonants, white spaces, digits and special
characters in a given string.
Program
#include<stdio.h>
int main()
{
char a[50];
int i,v,s,sc,d,c;
printf("Enter a string\n");
gets(a);
v=0;
s=0;
sc=0;
d=0;
c=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||
a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||
a[i]=='U')
{
v++;
}
else if(a[i]==' ')
{
s++;
}
else if((a[i]>='a' && a[i]<='z')||(a[i]>='A' &&
a[i]<='Z'))
{
c++;
}
else if(a[i]>='0' && a[i]<='9')
{
d++;
}
else
{
sc++;
}
}
printf("Number of vowels=%d\n",v);
printf("Number of spaces=%d\n",s);
printf("Number of consonants=%d\n",c);
printf("Number of digits=%d\n",d);
printf("Number of special characters=%d\n",sc);
return 0;
}
Output
Enter a string
Hi, I am Nayana.Date: 02/09/2021
Number of vowels=8
Number of spaces=4
Number of consonants=7
Number of digits=8
Number of special characters=5
Question No. 12
Write a C program to find length of a string
without using string handling function.
Program
#include<stdio.h>
int main()
{
char a[50];
int n,i;
printf("Enter the string:\n");
scanf("%s",a);
n=0;
for(i=0;a[i]!='\0';i++)
{
n++;
}
printf("Length of the string:%d\n",n);
return 0;
}
Output
Enter the string:
education
Length of the string:9
Question No. 13
Write a C program to print reverse of a string
without using string handling function.
Program
#include<stdio.h>
int main()
{
char a[10];
int i,j,n;
printf("Enter a string\n");
scanf("%s",a);
n=0;
for(i=0;a[i]!='\0';i++)
{
n++;
}
printf("Reverse:\n");
for(j=n-1;j>=0;j--)
{
printf("%c",a[j]);
}
return 0;
}
Output
Enter a string
good
Reverse:
doog
Question No. 14
Write a C program to check whether the given
string is palindrome or not without using string
handling function.
Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[10];
int i,n,f;
printf("Enter a string\n");
scanf("%s",a);
n=0;
for(i=0;a[i]!='\0';i++)
{
n++;
}
f=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]!=a[n-1])
{
f=1;
break;
}
n--;
}
if(f==0)
{
printf("Palindrome\n");
}
else
{
printf("Not Palindrome\n");
}
return 0;
}
Output 1
Enter a string
malayalam
Palindrome

Output 2
Enter a string
aluva
Not Palindrome
Question No. 15
Write a C program to concatenate two strings
without using string handling function.
Program
#include<stdio.h>
int main()
{
char a[50],b[50];
int i,n,j;
printf("Enter first string:\n");
scanf("%s",a);
printf("Enter second string:\n");
scanf("%s",b);
n=0;
for(i=0;a[i]!='\0';i++)
{
n++;
}
for(j=0;b[j]!='\0';j++)
{
a[n]=b[j];
n++;
}
a[n]='\0';
printf("Concatenated string:%s\n",a);
return 0;
}
Output
Enter first string:
Good
Enter second string:
Morning
Concatenated string:GoodMorning
Question No. 16
Write a C program to copy content of one string
into another without using string handling
function.
Program
#include<stdio.h>
int main()
{
char a[50],b[50];
int i;
printf("Enter first string:\n");
scanf("%s",a);
printf("Enter second string:\n");
scanf("%s",b);
printf("Before copying:\na: %s\nb: %s\n",a,b);
for(i=0;b[i]!='\0';i++)
{
a[i]=b[i];
}
a[i]='\0';
printf("After copying:\na: %s\nb: %s\n",a,b);
return 0;
}
Output
Enter first string:
hi
Enter second string:
morning
Before copying:
a: hi
b: morning
After copying:
a: morning
b: morning
Questions
1) Write a C program to read a sentence through keyboard and to
display the count of white spaces in the given sentence.
(3 Marks)
2) Without using any builtin string processing function like strlen,
strcat etc., write a program to concatenate two strings.
(8 Marks)
3) Write a C program to concatenate two strings without using
built in function. (3 Marks)
4) Give the syntax of four string handling functions in C. (2
Marks)
5) Write a C program that reads a string from the keyboard and
determines whether the string is a palindrome or not. (6 Marks)
6) Explain how string variables are declared and initialized in C
program. (2 Marks)
Thank you…….

You might also like