Professional Documents
Culture Documents
DFS File
DFS File
CODE:
#include <stdio.h>
int main()
{
int arr[10], size, i, num, pos;
printf("Enter the position where the element should be inserted (1 to %d): ", size+1);
scanf("%d", &pos);
arr[pos-1] = num;
size++;
return 0;
}
OUTPUT:
Ques 2: Write a program to delete an element from an array.
CODE:
#include <stdio.h>
int main()
{
int arr[10], size, i, num, pos;
size--;
return 0;
}
OUTPUT:
Ques 3: Write a program to search an element using linear search.
(i) Print its 1st occurrence
CODE:
#include <stdio.h>
int main()
{
int arr[10], size, i, num, pos = -1;
return 0;
}
OUTPUT:
Ques 3: Write a program to search an element using linear search.
(ii) Print its all occurrences and count
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i, num, count = 0;
return 0;
}
OUTPUT:
Ques 4: Write a program to implement binary search.
(i) With recursion
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i, num, index;
if(index == -1)
{
printf("Element not found in the array");
}
else
{
printf("The element %d was found at position %d in the array", num, index+1);
}
return 0;
}
if(arr[mid] == num)
{
return mid;
}
else if(arr[mid] > num)
{
return binarySearch(arr, lb, mid-1, num);
}
else
{
return binarySearch(arr, mid+1, ub, num);
}
}
OUTPUT:
Ques 4: Write a program to implement binary search.
(ii) Without recursion
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i, num, index;
if(index == -1)
{
printf("Element not found in the array");
}
else
{
printf("The element %d was found at position %d in the array", num, index+1);
}
return 0;
}
if(arr[mid] == num)
{
return mid;
}
else if(arr[mid] > num)
{
ub = mid-1;
}
else
{
lb = mid+1;
}
}
return -1;
}
OUTPUT:
Ques 5: Write a program to find the maximum element .
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i, max;
return 0;
}
return max;
}
OUTPUT:
Ques 6: Write a program to find the second largest element .
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i;
findSecondLargest(arr, size);
return 0;
}
if(secondLargest == firstLargest)
{
printf("There is no second largest element");
}
else
{
printf("The second largest element in the array is %d", secondLargest);
}
}
OUTPUT:
Ques 7: Write a program to implement selection sort.
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i;
selectionSort(arr, size);
return 0;
}
void selectionSort(int arr[], int size)
{
int i, j, minIndex, temp;
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
OUTPUT:
Ques 8: Write a program to implement insertion sort.
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i;
insertionSort(arr, size);
return 0;
}
void insertionSort(int arr[], int size)
{
int i, j, temp;
arr[j+1] = temp;
}
}
OUTPUT:
Ques 9: Write a program to implement bubble sort.
CODE:
#include <stdio.h>
int main()
{
int arr[100], size, i;
bubbleSort(arr, size);
return 0;
}
void bubbleSort(int arr[], int size)
{
int i, j, temp;
OUTPUT:
Ques 10: Write a program to implement counting sort.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[100], size, i;
countingSort(arr, size);
return 0;
}
int count[max+1];
for(i=0; i<=max; i++)
{
count[i] = 0;
}
OUTPUT:
Ques 11: Write a program to implement radix sort.
CODE:
#include <stdio.h>
// Cumalative count
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / pos) % 10] - 1] = arr[i];
count[(arr[i] / pos) % 10]--;
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
radixSort(arr, n);
printf("The sorted array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
Ques 12: Write a menu driven program to add, subtract and multiply two matrices.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice, i, j, rows, columns;
printf("Enter the number of rows in the matrices: ");
scanf("%d", &rows);
printf("Enter the number of columns in the matrices: ");
scanf("%d", &columns);
int matrix1[rows][columns], matrix2[rows][columns], result[rows][columns];
printf("\nEntered matrices:\n");
printf("\nMatrix 1:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
printf("\nMatrix 2:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
switch (choice) {
case 1:
printf("\nMatrix sum:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
break;
case 2:
printf("\nMatrix difference:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
break;
case 3:
printf("\nMatrix product:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
result[i][j] = 0;
for (int k = 0; k < rows; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
printf("%d ", result[i][j]);
}
printf("\n");
}
break;
case 4:
exit(1);
default:
printf("\nInvalid choice.\n");
}
return 0;
}
OUTPUT:
Ques 13: Write a program to check whether a matrix is identity or not.
CODE:
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the size of the matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("Enter the elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int is_identity = 1;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == j && matrix[i][j] != 1) {
is_identity = 0;
break;
} else if (i != j && matrix[i][j] != 0) {
is_identity = 0;
break;
}
}
if (!is_identity) {
break;
}
}
if (is_identity) {
printf("The matrix is an identity matrix.\n");
} else {
printf("The matrix is not an identity matrix.\n");
}
return 0;
}
OUTPUT:
Ques 14: Write a program to check whether two matrices are identical or not.
CODE:
#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2, i, j;
int matrix1[rows1][cols1];
printf("Enter the elements of the first matrix:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("The first matrix is:\n");
for (i = 0; i < rows1; i++) {
printf("\n");
for (j = 0; j < cols1; j++) {
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
int matrix2[rows2][cols2];
printf("Enter the elements of the second matrix:\n");
for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
printf("The second matrix is:\n");
for (i = 0; i < rows2; i++) {
printf("\n");
for (j = 0; j < cols2; j++) {
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Ques 15: Write a program to print the diagonal elements of a matrix.
CODE:
#include <stdio.h>
int main() {
int rows, cols, i, j;
int matrix[rows][cols];
return 0;
}
OUTPUT:
Ques 16: Write a program to print sum of all rows.
CODE:
#include <stdio.h>
int main() {
int rows, columns, i, j, sum;
int matrix[rows][columns];
printf("\nMatrix:\n");
for (i = 0; i < rows; i++) {
printf("\n");
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Print the sum of each row
printf("\nSum of each row:\n");
for (i = 0; i < rows; i++) {
sum = 0;
for (j = 0; j < columns; j++) {
sum += matrix[i][j];
}
printf("Row %d: %d\n", i + 1, sum);
}
return 0;
}
OUTPUT:
Ques 17: Write a program to arrange all rows of a matrix in ascending order.
CODE:
#include <stdio.h>
int main() {
int rows, columns, i, j, k, temp;
int matrix[rows][columns];
printf("\nOriginal matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns - 1; j++) {
for (k = j + 1; k < columns; k++) {
if (matrix[i][j] > matrix[i][k]) {
temp = matrix[i][j];
matrix[i][j] = matrix[i][k];
matrix[i][k] = temp;
}
}
}
}
return 0;
}
OUTPUT:
Ques 18: Write a program to check if a matrix is sparse or not.
CODE:
#include <stdio.h>
int main() {
int rows, columns, i, j, count = 0;
int matrix[rows][columns];
printf("\nEntered matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Ques 19: Write a program that converts sparse matrix into row triplet form.
CODE:
#include <stdio.h>
int main() {
int rows, columns, i, j, count = 0, k = 0;
int matrix[rows][columns];
printf("\nEntered matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Ques 20: Write a program to find missing element in an array.
CODE:
#include <stdio.h>
int main()
{
int arr[100], n, i, sum=0, expected_sum;
return 0;
}
OUTPUT:
Ques 21: Write a program to find duplicate arrays.
CODE:
#include <stdio.h>
int main() {
int arr[20]={1,2,3,5,7,7,8,8};
int i,j;