Lab Program 1: Sort a given list of strings
Aim: Sort a given list of strings using c program
Algorithm
Step 01: Start
Step 02: Declare integer variables i,j,count
Step 03: Declare array str[25][25], temp[25]
Step 04: Display "How many strings u are going to enter?:
Step 05: Read count
Step 06: Print "Enter Strings one by one: "
Step 07: for(i=0;i<=count;i++)
Step 08: read str[i]
Step 09: for(i=0;i<=count;i++)
{
for(j=i+1;j<=count;j++)
{
if(strcmp(str[i],str[j])>0)
{
1. strcpy(temp,str[i]);
2. strcpy(str[i],str[j]);
3. strcpy(str[j],temp);
}
}
}
Step 10: Print “Order of Sorted Strings:"
Step 11: for(i=0;i<=count;i++)
display str[i]
Step 12: End
Program
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
puts("How many strings u are going to enter?: ");
scanf("%d",&count);
puts("Enter Strings one by one: ");
for(i=0;i<=count;i++)
gets(str[i]);
/* Sorting logic*/
for(i=0;i<=count;i++)
{
for(j=i+1;j<=count;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("Order of Sorted Strings:");
for(i=0;i<=count;i++)
puts(str[i]);
getch();
return 0;
}
Output:
Lab Program 2: Merge two sorted array
Aim: Merge two sorted arrays into one sorted array
Algorithm
Step 01: Declare array1[50], array2[50], array3[100], m, n, i, j, k = 0
Step 02: Print " Enter size of array Array 1: "
Step 03: Read m
Step 04: Print “Enter sorted elements of array 1:”
Step 05: for (i = 0; i < m; i++)
read array1[i])
Step 06: Print " Enter size of array Array 2: "
Step 07: Read n
Step 08: Print “Enter sorted elements of array 2:”
Step 09: for (i = 0; i < n; i++)
read array2[i])
Step 10: i = 0
Step 11: j = 0
Step 12: while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
Step 13: if (i >= m) /* copy remaining elements in array 2*/
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
Step 14: if (j >= n) /* copy remaining elements in array 1*/
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
Step 15: Print " After merging: "
Step 16: for (i = 0; i < m + n; i++)
Printf array3[i]
Step 17: End
Program
#include <stdio.h>
int main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
printf("\n Enter size of array Array 1: ");
scanf("%d", &m);
printf("\n Enter sorted elements of array 1: \n");
for (i = 0; i < m; i++)
scanf("%d", &array1[i]);
printf("\n Enter size of array 2: ");
scanf("%d", &n);
printf("\n Enter sorted elements of array 2: \n");
for (i = 0; i < n; i++)
scanf("%d", &array2[i]);
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("\n After merging: \n");
for (i = 0; i < m + n; i++)
{
printf("\n%d", array3[i]);
}
getch();
return(0);
}
Output:
Enter size of array Array 1: 3
Enter sorted elements of array 1:
12
18
40
Enter size of array 2: 3
Enter sorted elements of array 2:
47
56
89
After merging:
12
18
40
56
60
89
Lab Program 3: Linear search
Aim: Implement linear search algorithm using c program
Algorithm
Step 1: Declare array a[20]
Step 2: Declare integer variable i,x,n
Step 3: print "Enter size of the array”
Step 4: Read n
Step 5: print "Enter array elements:"
Step 6: Read n elements into array using a loop
Step 7: Print "Enter element to search:"
Step 8: Read x
Step 9: Set i=0
Step 10: Repeat for Step 11 and 12 until (i<n)
Step 11: if(a[i]==x)then goto 13
Step 12: i=i+1 goto Step10
Step 13: if(i<n)then Print "Element found at index i”
else print "Element not found"
Step 14: End
Program
#include<stdio.h>
int main()
{
int a[20];
int i,x,n;
printf("Enter size of the array:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
break;
}
if(i<n)
printf(“Element found at index %d”,i);
else
printf(“Element not found”);
return 0;
}
Output:
Enter size of the array:5
Enter array elements:
20
10
30
50
40
Enter element to search:30
Element found at index 2
Lab Program 4: Binary search
Aim: Search an element in one dimensional array using binary search
Algorithm
Step 01: Start
Step 02: Declare integer variable first, last, middle, size, i, sElement
Step 03: Declare array named list[100]
Step 04: Print “Enter the size of the array: "
Step 05: Read size
Step 06: Print "Enterinteger values in Ascending order"
Step 07: for (i = 0; i < size; i++)
Read list[i]
Step 08: Print "Enter value to be search: "
Step 09: Read sElement
Step 10: first = 0;
Step 11: last = size - 1;
Step 12: middle = (first+last)/2;
Step 13: while (first <= last)
{
if (list[middle] == sElement)
{
Print "Element found at index middle”
goto END
}
else if (list[middle] < sElement)
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
Step 14: if (first > last)
print "Element Not found in the list."
Step 15: END
Program
#include<stdio.h>
int main()
{
int first, last, middle, size, i, sElement, list[100];
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending order\n", size);
for (i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last)
{
if (list[middle] == sElement)
{
printf("Element found at index %d\n",middle);
break;
}
else if (list[middle] < sElement)
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Element Not found in the list.");
getch();
return(0);
}
Output:
Enter the size of the array:4
Enter 4 integer values in Ascending order
10
20
30
40
Enter value to be search:30
Element found at index 2
Lab Program 5: Sparse Matrix Implementation
Aim: Implement sparse matrix using triplet representation.
Triplet Representation
Sparse Matrix
Row Index Column Index Element
0 1 2 3
4
0 0 5 0 0 5 4 No of
No of rows No of columns Non-Zero
1 0 0 6 0 Values
2 0 8 0 0 0 1 5
3 0 0 0 0 1 2 6
4 0 2 0 0 2 1 8
4 1 2
Algorithm
Step 01: Start
Step 02: Define a constant MAX with value 20
Step 03: Declare integer variable a[10][10], b[MAX][3], row, column, i,j,k
Step 04: Print "Enter the size of matrix (rows, columns):
Step 05: Read row,column
Step 06: Print “Enter elements of matrix"
Step 07: for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
Step 08: b[0][0] = row;
Step 09: b[0][1] = column;
Step 10: k = 1;
Step 11: for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (a[i][j] != 0)
{
1. b[k][0] = i;
2. b[k][1] = j;
3. b[k][2] = a[i][j];
4. k++;
}
}
}
Step 12: b[0][2] = k-1;
Step 13: printf"Sparse form - list of 3 triples"
Step 14: for (i = 0; i <= b[0][2]; i++)
Print b[i][0], b[i][1], b[i][2]
Step 15: End
Program
#include <stdio.h>
#define MAX 20
int main()
{
int a[10][10], b[MAX][3], row, column, i,j,k ;
printf("\nEnter the size of matrix (rows, columns): ");
scanf("%d%d", &row, &column);
printf("\nEnter elements of matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
b[0][0] = row;
b[0][1] = column;
k = 1;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (a[i][j] != 0)
{
b[k][0] = i;
b[k][1] = j;
b[k][2] = a[i][j];
k++;
}
}
}
b[0][2] = k-1;
printf("\nSparse form - list of 3 triples\n\n");
for (i = 0; i <= b[0][2]; i++)
printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]);
getch();
return 0;
}
Output:
Enter the size of matrix (rows, columns): 3 4
Enter elements of matrix
[0][0]: 6
[0][1]: 0
[0][2]: 0
[0][3]: 0
[1][0]: 0
[1][1]: 1
[1][2]: 0
[1][3]: 0
[2][0]: 0
[2][1]: 0
[2][2]: 0
[2][3]: 5
Sparse form - list of 3 triples
3 4 3
0 0 6
1 1 1
2 3 5