Miii
Miii
Miii
PROGRAMMING IN C
MODULE III
SYLLABUS
Arrays and strings
Arrays, Declaration and Initialization, 1-
Dimensional Array, 2-Dimensional Array
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];
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.
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]
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]
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]
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]
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
i=0
i=0
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
i=0
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
i=0
i=i+1
j=1
i=0
i=i+1
j=1
i=0
i=i+1
j=1
i=0
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
i=0
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
i=0
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
i=0
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
i=0
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
i=0
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
i=0
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
i=0
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
i=0
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:
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…….