Lab Program - First 5
Lab Program - First 5
Program
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
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 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++;
}
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);
i = 0;
j = 0;
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++;
}
}
getch();
return(0);
}
Output:
Enter size of array Array 1: 3
Program
#include<stdio.h>
int main()
{
int a[20];
int i,x,n;
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
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);
first = 0;
last = size - 1;
middle = (first+last)/2;
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
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);
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