You are on page 1of 57

Ques 1: Write a program to insert an element in an array.

CODE:
#include <stdio.h>

int main()
{
int arr[10], size, i, num, pos;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be inserted: ");


scanf("%d", &num);

printf("Enter the position where the element should be inserted (1 to %d): ", size+1);
scanf("%d", &pos);

for(i=size; i>=pos; i--)


{
arr[i] = arr[i-1];
}

arr[pos-1] = num;
size++;

printf("The array after insertion is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

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;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be deleted: ");


scanf("%d", &num);

for(i=0; i<size; i++)


{
if(arr[i] == num)
{
pos = i;
break;
}
}
for(i=pos; i<size-1; i++)
{
arr[i] = arr[i+1];
}

size--;

printf("The array after deletion is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

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;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be searched: ");


scanf("%d", &num);

for(i=0; i<size; i++)


{
if(arr[i] == num)
{
pos = i;
break;
}
}
if(pos == -1)
{
printf("Element not found in the array");
}
else
{
printf("The element %d was found at position %d in the array", 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;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be searched: ");


scanf("%d", &num);

printf("The element %d was found at positions: ", num);


for(i=0; i<size; i++)
{
if(arr[i] == num)
{
printf("%d ", i+1);
count++;
}
}
if(count == 0)
{
printf("Element not found in the array");
}
else
{
printf("\nTotal occurrences: %d", count);
}

return 0;
}
OUTPUT:
Ques 4: Write a program to implement binary search.
(i) With recursion
CODE:
#include <stdio.h>

int binarySearch(int arr[], int lb, int ub, int num);

int main()
{
int arr[100], size, i, num, index;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array in ascending order: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be searched: ");


scanf("%d", &num);

index = binarySearch(arr, 0, size-1, num);

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;
}

int binarySearch(int arr[], int lb, int ub, int num)


{
if(lb > ub)
{
return -1;
}

int mid = (lb + ub) / 2;

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 binarySearch(int arr[], int size, int num);

int main()
{
int arr[100], size, i, num, index;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array in ascending order: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to be searched: ");


scanf("%d", &num);

index = binarySearch(arr, size, num);

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;
}

int binarySearch(int arr[], int size, int num)


{
int lb = 0, ub = size-1;

while(lb <= ub)


{
int mid = (lb + ub) / 2;

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 findMax(int arr[], int size);

int main()
{
int arr[100], size, i, max;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

max = findMax(arr, size);

printf("The maximum element in the array is %d", max);

return 0;
}

int findMax(int arr[], int size)


{
int max = arr[0], i;
for(i=1; i<size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}

return max;
}

OUTPUT:
Ques 6: Write a program to find the second largest element .
CODE:
#include <stdio.h>

void findSecondLargest(int arr[], int size);

int main()
{
int arr[100], size, i;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

findSecondLargest(arr, size);

return 0;
}

void findSecondLargest(int arr[], int size)


{
int firstLargest = arr[0], secondLargest = arr[0], i;

for(i=1; i<size; i++)


{
if(arr[i] > firstLargest)
{
secondLargest = firstLargest;
firstLargest = arr[i];
}
else if(arr[i] > secondLargest && arr[i] != firstLargest)
{
secondLargest = arr[i];
}
}

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>

void selectionSort(int arr[], int size);

int main()
{
int arr[100], size, i;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

selectionSort(arr, size);

printf("The sorted array is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
void selectionSort(int arr[], int size)
{
int i, j, minIndex, temp;

for(i=0; i<size-1; i++)


{
minIndex = i;

for(j=i+1; j<size; j++)


{
if(arr[j] < arr[minIndex])
{
minIndex = j;
}
}

temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

OUTPUT:
Ques 8: Write a program to implement insertion sort.
CODE:
#include <stdio.h>

void insertionSort(int arr[], int size);

int main()
{
int arr[100], size, i;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

insertionSort(arr, size);

printf("The sorted array is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
void insertionSort(int arr[], int size)
{
int i, j, temp;

for(i=1; i<size; i++)


{
temp = arr[i];

for(j=i-1; j>=0 && arr[j]>temp; j--)


{
arr[j+1] = arr[j];
}

arr[j+1] = temp;
}
}

OUTPUT:
Ques 9: Write a program to implement bubble sort.
CODE:
#include <stdio.h>

void bubbleSort(int arr[], int size);

int main()
{
int arr[100], size, i;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

bubbleSort(arr, size);

printf("The sorted array is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
void bubbleSort(int arr[], int size)
{
int i, j, temp;

for(i=0; i<size-1; i++)


{
for(j=0; j<size-i-1; j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

OUTPUT:
Ques 10: Write a program to implement counting sort.
CODE:
#include <stdio.h>
#include <stdlib.h>

void countingSort(int arr[], int size);

int main()
{
int arr[100], size, i;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

countingSort(arr, size);

printf("The sorted array is: ");


for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

void countingSort(int arr[], int size)


{
int max = arr[0], i, j;

for(i=1; i<size; i++)


{
if(arr[i]>max)
{
max = arr[i];
}
}

int count[max+1];
for(i=0; i<=max; i++)
{
count[i] = 0;
}

for(i=0; i<size; i++)


{
count[arr[i]]++;
}

for(i=1; i<=max; i++)


{
count[i] += count[i-1];
}
int temp[size];

for(i=size-1; i>=0; i--)


{
temp[count[arr[i]]-1] = arr[i];
count[arr[i]]--;
}

for(i=0; i<size; i++)


{
arr[i] = temp[i];
}
}

OUTPUT:
Ques 11: Write a program to implement radix sort.
CODE:
#include <stdio.h>

// Function to find the maximum element in the array


int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

// Function to perform counting sort


void countSort(int arr[], int n, int pos) {
int output[n];
int count[10] = {0};

// Store count of occurrences in count[]


for (int i = 0; i < n; i++) {
count[(arr[i] / pos) % 10]++;
}

// 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]--;
}

// Copy the output array to arr[]


for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}

// Function to perform radix sort


void radixSort(int arr[], int n) {
int max = getMax(arr, n);

for (int pos = 1; max / pos > 0; pos *= 10) {


countSort(arr, n, pos);
}
}

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("\nEnter the elements of the first matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix1[i][j]);
}
}

printf("\nEnter the elements of the second matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix2[i][j]);
}
}

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");
}

// Display the menu


while(1){
printf("\nMatrix operations menu:\n");
printf("1. Add matrices\n");
printf("2. Subtract matrices\n");
printf("3. Multiply matrices\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

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;

// Input for first matrix


printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &rows1, &cols1);

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");
}

// Input for second matrix


printf("Enter the number of rows and columns of the second matrix: ");
scanf("%d %d", &rows2, &cols2);

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");
}

// Check if the two matrices are identical


int is_identical = 1;
if (rows1 != rows2 || cols1 != cols2) {
is_identical = 0;
} else {
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
if (matrix1[i][j] != matrix2[i][j]) {
is_identical = 0;
break;
}
}
if (!is_identical) {
break;
}
}
}

// Print the result


if (is_identical) {
printf("The two matrices are identical.\n");
} else {
printf("The two matrices are not identical.\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;

printf("Enter the number of rows and columns of the matrix: ");


scanf("%d %d", &rows, &cols);

int matrix[rows][cols];

printf("Enter the elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("The matrix is:\n");


for (i = 0; i < rows; i++) {
printf("\n");
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

printf("The diagonal elements of the matrix are:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (i == j) {
printf("%d ", matrix[i][j]);
}
}
}

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;

printf("Enter the number of rows in the matrix: ");


scanf("%d", &rows);
printf("Enter the number of columns in the matrix: ");
scanf("%d", &columns);

int matrix[rows][columns];

printf("Enter the elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix[i][j]);
}
}

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;

printf("Enter the number of rows in the matrix: ");


scanf("%d", &rows);
printf("Enter the number of columns in the matrix: ");
scanf("%d", &columns);

int matrix[rows][columns];

printf("Enter the elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix[i][j]);
}
}

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;
}
}
}
}

printf("\nMatrix with all rows arranged in ascending order:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

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;

printf("Enter the number of rows in the matrix: ");


scanf("%d", &rows);
printf("Enter the number of columns in the matrix: ");
scanf("%d", &columns);

int matrix[rows][columns];

printf("Enter the elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0) {
count++;
}
}
}

printf("\nEntered matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

if (count > (rows * columns) / 2) {


printf("\nThe matrix is sparse.");
} else {
printf("\nThe matrix is not sparse.");
}

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;

printf("Enter the number of rows in the matrix: ");


scanf("%d", &rows);
printf("Enter the number of columns in the matrix: ");
scanf("%d", &columns);

int matrix[rows][columns];

printf("Enter the elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0) {
count++;
}
}
}

printf("\nEntered matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

// Create a row triplet form array from the sparse matrix


int row_triplet[(rows*columns - count) + 1][3];
row_triplet[0][0] = rows;
row_triplet[0][1] = columns;
row_triplet[0][2] = (rows*columns - count);

for (i = 0; i < rows; i++) {


for (j = 0; j < columns; j++) {
if (matrix[i][j] != 0) {
k++;
row_triplet[k][0] = i;
row_triplet[k][1] = j;
row_triplet[k][2] = matrix[i][j];
}
}
}

// Print the row triplet form array


printf("\nRow triplet form:\n");
for (i = 0; i <= k; i++) {
printf("%d %d %d\n", row_triplet[i][0], row_triplet[i][1], row_triplet[i][2]);
}

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;

printf("Enter the size of the array: ");


scanf("%d", &n);

printf("Enter the elements of the array: ");


for(i=0; i<n-1; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}

expected_sum = (n*(n+1))/2; //sum of n natural numbers

printf("The missing element is: %d", expected_sum-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;

int len = sizeof(arr)/sizeof(arr[0]);

for( i = 0; i < len; i++) {


for(j = i + 1; j < len; j++) {
if(arr[i] == arr[j])
printf("%d\n", arr[i]);
}
}
return 0;
}

You might also like