DS Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Q.1 Write a program to perform the matrix addition, multiplication operation.

#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

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


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

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

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}

Multiplication

#include <stdio.h>
#include <stdlib.h>

int main()
{
int r1, r2, c1, c2;

printf("Enter the number of rows for the first matrix:\n");


scanf("%d", &r1);

printf("Enter the number of columns for the first matrix:\n");


scanf("%d", &c1);
printf("Enter the number of rows for the second matrix:\n");
scanf("%d", &r2);

printf("Enter the number of columns for the second matrix:\n");


scanf("%d", &c2);
if (c1 != r2) {
printf("The matrices cannot be multiplied together");
exit(-1);
}
int m1[r1][c1], m2[r2][c2];
printf("Enter the elements of the first matrix\n");

for (int i = 0; i < r1; i++) {


for (int j = 0; j < c1; j++) {
scanf("%d", &m1[i][j]);
}
}

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

for (int i = 0; i < r2; i++) {


for (int j = 0; j < c2; j++) {
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];

for (int i = 0; i < r1; i++) {


for (int j = 0; j < c2; j++) {
mul[i][j] = 0;

for (int k = 0; k < c1; k++) {


mul[i][j] += m1[i][k] * m2[k][j];
}
}
}
printf("The multiplied matrix is: \n");

for (int i = 0; i < r1; i++) {


for (int j = 0; j < c2; j++) {
printf("%d\t", mul[i][j]);
}

printf("\n");
}

return 0;
}
1. Write a program to perform various string operations such as copy, length, reversing,
palindrome, concatenation and to find occurrence of a sub-string using and without using
library functions.

#include
#include
#include
void main()
{
int no,m,ch ,i,j,n,k,flag=0;
char str[20],str2[20],str3[20],str4[20];
char wish;
clrscr();
do
{
menu:
printf(“\n1.length of the string.”);
printf(“\n2.copying string another.”);
printf(“\n3.concatination of two string”);
printf(“\n4.reversing the string”);
printf(“\n5.checking palindrome or not.”);
printf(“\n6.checking sub-string in the string.” );
printf(“\nEnter the choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“Enter the string: “);
flushall();
gets(str);
for(i=1;str[i]!=’\0′;i++); //to find the length of the string
printf(“the length of the string is:%d “,i);
break;
case 2:
printf(“Enter the string: “);
flushall();
gets(str);
for(i=0;str[i]!=’\0′;i++)//TO COPY A STRING IN OTHER
{
str2[i]=str[i];
}
str2[i]=’\0′;
printf(“\n the copied string is: “);
puts(str2);
break;
case 3:
printf(“Enter the string: “);
flushall();
gets(str);
printf(“\n Enter the second string”);
flushall();
gets(str2);
i=0;
while(str[i]!=’\0′)
i++;
for(j=0;str2[j]!=’\0′;j++,i++)//TO CONCATINATE TWO STRING
str[i]=str2[j];
str[i]=’\0′;
printf(“\nthe concatinated string is: “);
puts(str);
break;
case 4:
printf(“Enter the string: “);
flushall();
gets(str);
i=0;
while(str[i]!=’\0′)
i++;
i–;
for(j=0;str[j]!=’\0′,i>=0;j++,i–)//TO REVERSE STRING
str2[j]=str[i];
str2[j]=’\0′;
printf(“\n the reverse string is: “);
puts(str2);
break;
case 5:
printf(“Enter the string: “);
flushall();
gets(str);
i=0;
while(str[i]!=’\0′)
i++;
i–;
flag=1;
for(j=0;i>j;j++,i–)
{
if(str[i]!=str[j])
{
flag=0;
break;
}
}
if(flag==1)
printf(“\nthe string is palindrom.”);
else
printf(“\nthe string is not palindrom.”);
break;
case 6:
printf(“Enter the string: “);
flushall();
gets(str);
printf(“\nEnter the second string: “); //TO FIND WETHER SUBSTRING EXISTS OR NOT
gets(str4);
for(m=0;str[m]!=’\0′;m++);
for(k=0;k<=m-1;k++)
{
for(j=0;str4[j]!=’\0′;j++)
{
if(str[k+j]!=str4[j])
{
flag=0;
break;
}
}
if(str4[j]==’\0′)
{
flag=1;break;}
}
if(flag==1)
printf(“\nsubstring found at position:%d “,j+k);
else
printf(“\n substring donot exists”);
break;
default:
printf(“\n wrong choice!!!!”);
break;
}
printf(“\nwant to continue(y/n)”);
scanf(“%c”,&wish);
}
while(wish!=’n’);
getch();
}
2.Write a program to search an element using Linear Search.
Answer:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}

3. Write a program to search an element using Binary Search

Binary Search Working

Binary Search Algorithm can be implemented in two ways which are discussed below.

1. Iterative Method

2. Recursive Method
The recursive method follows the divide and conquer approach.
The general steps for both methods are discussed below.

1. The array in which searching is to be performed is:

Initial array
Let x = 4 be the element to be searched.
2. Set two pointers low and high at the lowest and the highest positions respectively.

Setting pointers

3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.

Mid element
4. If x == mid, then return mid.Else, compare the element to be searched with m.

5. If x > mid, compare x with the middle element of the elements on the right side of mid. This
is done by setting low to low = mid + 1.
6. Else, compare x with the middle element of the elements on the left side of mid. This is done
by setting high to high = mid - 1.

Finding mid element


7. Repeat steps 3 to 6 until low meets high.

Mid element

7. x = 4 is found.

Found
Binary Search Algorithm

Iteration Method

do until the pointers low and high meet each other.

mid = (low + high)/2

if (x == arr[mid])

return mid

else if (x > arr[mid]) // x is on the right side

low = mid + 1

else // x is on the left side

high = mid - 1

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

if (array[mid] == x)
return mid;

if (array[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}

int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

Recursive Method

binarySearch(arr, x, low, high)

if low > high

return False

else

mid = (low + high) / 2

if x == arr[mid]

return mid

else if x > arr[mid] // x is on the right side

return binarySearch(arr, x, mid + 1, high)

else // x is on the left side

return binarySearch(arr, x, low, mid - 1)

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {


if (high >= low) {
int mid = low + (high - low) / 2;

// If found at mid, then return it


if (array[mid] == x)
return mid;
// Search the left half
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1);

// Search the right half


return binarySearch(array, x, mid + 1, high);
}

return -1;
}

int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
}

4.Write a program to sort the given array using Bubble Sort.


Answer
/* Bubble sort code */
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

5.Write a program to sort the given array using Selection Sort


Answer:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}

6. Write a program to sort the given array using Insertion Sort


Answer:
#include<stdio.h>
int main(){
/* Here i & j for loop counters, temp for swapping,
* count for total number of elements, number[] to
* store the input numbers in array. You can increase
* or decrease the size of number array as per requirement
*/
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);
// Implementation of insertion sort algorithm
for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
7.Write a program to sort the given array using QuickSort
Answer:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
8.Write a program to sort the given array using MergeSort.
Answer:
/* C program for Merge Sort */
#include <stdio.h>
#include <stdlib.h>

// Merges two subarrays of arr[].


// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);


printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}

You might also like