0% found this document useful (0 votes)
5 views

Dsa Practical

The document contains multiple C programs that demonstrate various algorithms and operations on arrays and matrices, including finding the maximum value in an array, displaying arrays, matrix addition and multiplication, insertion and deletion in arrays, linear and binary search, and different sorting algorithms (bubble, insertion, selection, and quick sort). Each program includes user input for array or matrix sizes and elements, and outputs the results accordingly. The programs are structured with functions for specific tasks and utilize standard input/output libraries.

Uploaded by

itsharshkumar775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Dsa Practical

The document contains multiple C programs that demonstrate various algorithms and operations on arrays and matrices, including finding the maximum value in an array, displaying arrays, matrix addition and multiplication, insertion and deletion in arrays, linear and binary search, and different sorting algorithms (bubble, insertion, selection, and quick sort). Each program includes user input for array or matrix sizes and elements, and outputs the results accordingly. The programs are structured with functions for specific tasks and utilize standard input/output libraries.

Uploaded by

itsharshkumar775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Program 1: Find Maximum value in an array

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i, n, a[20], max, loc = 0;

printf("Enter the size of array");


scanf("%d", &n);

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


scanf("%d", &a[i]);

printf("Array is:\n");
for (i = 0; i < n; i++)
printf("a[%d] = %d\n", i, a[i]);

max = a[0];
for (i = 0; i < n; i++)
{
if (max < a[i])
{
max = a[i];
loc = i + 1;
}
}

printf("\nMaximum value in array = %d", max);


printf("\nLocation = %d", loc);

getch();
}
Output:
Program 2: Display the array
**Program to Display the array.**

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i, n, a[20];
clrscr();

printf("Enter the size of array");


scanf("%d", &n);

printf("Enter the elements\n");


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

printf("Array is:\n");
for (i = 0; i < n; i++)
printf("a[%d] = %d\n", i, a[i]);

getch();

}
Program 3: Display the Matrix of two
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i, j, p, q, a[5][5], b[5][5];
clrscr();
printf("\n enter the size of
matrix a”)
scanf("%d%d",& p,&q);
printf("\nEnter the size of matrix b = ");
scanf("%d%d", &p, &q);
printf("\nEnter the elements of first matrix a = ");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the elements of second matrix b = ");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &b[i][j]);
printf("\nMatrix a = \n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", a[i][j]);
printf("\n");
}
printf("\nMatrix b = \n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", b[i][j]);
printf("\n");
}
getch();
}
Program 4: Addition of Two Matrices
**Program of Addition of Two Matrices**

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
int i, j, p, q, a[5][5], b[5][5], c[5][5];
clrscr();

printf("\nEnter the size of matrix a = ");


scanf("%d%d", &p, &q);

printf("\nEnter the size of matrix b = ");


scanf("%d%d", &p, &q);

printf("\nEnter the elements of first matrix a = ");


for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &a[i][j]);

printf("\nEnter the elements of second matrix b = ");


for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &b[i][j]);

printf("\nMatrix a =\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", a[i][j]);
printf("\n");
}

printf("\nMatrix b =\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", b[i][j]);
printf("\n");
}

printf("\nSum of the matrices a and b =\n");


for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", a[i][j] + b[i][j]);
printf("\n");
}

getch();
}
Program 5: Multiplication of Two Matrices
**Program of Multiplication of Two Matrices**

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
int i, j, k, m, n, p, q;
int a[5][5], b[5][5], c[5][5];
clrscr();

printf("\nEnter the size of matrix a = ");


scanf("%d%d", &m, &n);

printf("\nEnter the size of matrix b = ");


scanf("%d%d", &p, &q);

if (n != p) {
printf("\nMultiplication not possible");
} else {
printf("\nEnter the elements of first matrix a = ");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);

printf("\nEnter the elements of second matrix b = ");


for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &b[i][j]);

printf("\nMatrix a =\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("\t%d", a[i][j]);
printf("\n");
}

printf("\nMatrix b =\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++)
printf("\t%d", b[i][j]);
printf("\n");
printf("\nMultiplication of the matrix a and b =\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}
}

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


for (j = 0; j < q; j++)
printf("\t%d", c[i][j]);
printf("\n");
}
}

getch();
}

}
Program 6: Insertion in an Array
**Program of Insertion in an Array**

#include <stdio.h>
#include <conio.h>

void main()
{
int array[100], position, c, n, value;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);


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

printf("Enter the location where you wish to insert an element\n");


scanf("%d", &position);

printf("Enter the value to insert\n");


scanf("%d", &value);

for (c = n; c >= position - 1; c--)


array[c + 1] = array[c];

array[position - 1] = value;

printf("Resultant array is:\n");


for (c = 0; c <= n; c++)
printf("%d\n", array[c]);

getch();
}
Program 7: Deletion in an Array
**Program of Deletion of an Element in Array**

#include <stdio.h>
#include <conio.h>

void main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);


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

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if (position >= n + 1)
printf("Deletion not possible.\n");
else {
for (c = position - 1; c < n - 1; c++)
array[c] = array[c + 1];

printf("Resultant array is:\n");


for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}

getch();
}
Program 8: Linear Search
**Program for Linear Search in an Array**

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
int a[100], s, c, n;
clrscr();

printf("Enter the number of elements in array:\t");


scanf("%d", &n);

printf("\nEnter %d integer(s):\n", n);


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

printf("\nEnter the number to search\t");


scanf("%d", &s);

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


if (a[c] == s) {
printf("\n\n\t%d is present at location %d\n", s, c + 1);
break;
}
}

if (c == n)
printf("\n\n\t%d is not present in array.", s);

getch();
}
Program 9: Binary Search
**Program for Binary Search in an Array**

#include <stdio.h>
#include <conio.h>

void main()
{
int c, first, last, middle, n, search, array[100];

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]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first + last) / 2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d\n", search, middle + 1);
break;
} else
last = middle - 1;
middle = (first + last) / 2;
}

if (first > last)


printf("Not found! %d is not present in the list", search);
getch();
}
Program 10: Bubble Sort
**Program for Bubble Sort**

#include <stdio.h>
#include <conio.h>

void carray(int[10]);
void darray(int[10]);
void bsort(int[10]);
int n;

void main()
{
int a[10];
clrscr();

printf("\nEnter the number of elements:");


scanf("%d", &n);

carray(a);

printf("\nThe array is:");


darray(a);

bsort(a);

printf("\nSorted array:");
darray(a);

getch();
}

void carray(int x[10])


{
int i;
printf("\nEnter elements in array\n");
for (i = 0; i < n; i++)
scanf("%d", &x[i]);
}

void darray(int x[10])


{
int i;
for (i = 0; i < n; i++)
printf("\na[%d] = %d", i, x[i]);
}
void bsort(int x[10])
{
int i, j, temp;
for (i = 0; i <= n - 1; i++) {
for (j = 0; j < n - 1; j++) {

if (x[j] > x[j + 1]) {


temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
}
Program 11: Insertion Sort
**Program for Insertion Sort**

#include <stdio.h>
#include <conio.h>

void carray(int[10]);
void darray(int[10]);
void isort(int[10]);
int n;

void main()
{
int a[10];
clrscr();

printf("\nEnter the number of elements in an array:");


scanf("%d", &n);

carray(a);

printf("\nThe array is:");


darray(a);

isort(a);

printf("\nSorted array:");
darray(a);

getch();
}

void carray(int x[10])


{
int i;
printf("\nEnter elements in array:");
for (i = 0; i < n; i++)
scanf("%d", &x[i]);
}

void darray(int x[10])


{
int i;
for (i = 0; i < n; i++)
printf("\na[%d] = %d", i, x[i]);
}
for (j = i - 1; j >= k; j--)
{
x[j + 1] = x[j];
}
x[k] = temp;

}
}
}
}
Program 12: Selection Sort
**Program for Selection Sort**

#include <stdio.h>
#include <conio.h>

void carray(int[10]);
void darray(int[10]);
void ssort(int[10]);
int min(int[10], int);

int n;

void main()
{
int a[10];
clrscr();

printf("\nEnter the number of elements in an array: ");


scanf("%d", &n);

carray(a);

printf("\nThe array is:");


darray(a);

ssort(a);

printf("\nSorted array:");
darray(a);

getch();
}

void carray(int x[10])


{
int i;
printf("\nEnter elements in array:\n");
for (i = 0; i < n; i++)
scanf("%d", &x[i]);
}

void darray(int x[10])


{
int i;
for (i = 0; i < n; i++)
printf("\na[%d] = %d", i, x[i]);
}

void ssort(int x[10])


{
int k, loc, temp;
for (k = 0; k < n; k++)
{
loc = min(x, k);
temp = x[k];
x[k] = x[loc];
x[loc] = temp;
}
}
int min(int x[10], int k)
{
int j, m, loc;
m = x[k];
loc = k;

for (j = k + 1; j < n; j++)


{
if (m > x[j])
{
m = x[j];
loc = j;
}
}
return loc;
}
Program 13: Quick Sort
**Program for Quick Sort**

#include <stdio.h>
#include <conio.h>

void carray(int[10]);
void darray(int[10]);
void qsort(int[15], int, int);
int partition(int[15], int, int);

int n;

void main()
{
int a[10], lb, ub;
clrscr();

printf("\nEnter the number of elements: ");


scanf("%d", &n);

carray(a);

lb = 0;
ub = n - 1;
qsort(a, lb, ub);
printf("\nThe array is:\n");
darray(a);

printf("\nSorted array:\n");
darray(a);

getch();
}
void carray(int x[10])
{
int i;
printf("\nEnter elements in array:\n");
for (i = 0; i < n; i++)
scanf("%d", &x[i]);
}

void darray(int x[10])


{
int i;
for (i = 0; i < n; i++)
printf("\na[%d] = %d", i, x[i]);
}
Void qsort(int x[0],int ib,int ub)
{
Int split;
If(ib,ub)
{
Split=partition(x,ib,ub);
Qsort(x,ib,ub,split-1);
Qsort(x,split+1,ub);
}
}
int partition(int x[10], int lb, int ub)
{
int first, last, temp, key;
first = lb;
last = ub;
key = x[lb];

while (first < last)


{
while (x[first] < key)
first++;

while (x[last] > key)


last--;

if (first < last)


{
temp = x[first];
x[first] = x[last];
x[last] = temp;
}
}

if (first > last)


{
temp = x[lb];
x[lb] = x[last];
x[last] = temp;
}

return last;
}
Program 14: Stack using Array
**Program for Stack (Using Array)**

#include <stdio.h>
#include <conio.h>

void push(int [10], int);


void pop(int [10]);
void display(int [10]);
int n = 10, top = -1;
void main()
{
int a[10], ch, item;

do {
clrscr();
printf("Menu\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter element to push: ");
scanf("%d", &item);
push(a, item);
break;

case 2:
pop(a);
break;

case 3:
display(a);
break;

case 4:
printf("Thanks for using my window");
getch();
exit(0);

default:
printf("Wrong choice");
}
} while (ch <= 4);

getch();
}
void push(int x[10], int item)
{
if (top == n - 1)
printf("Overflow");
else {
top++;
x[top] = item;
}

getch();
}

void pop(int x[10])


{
int item;

if (top == -1)
printf("Underflow");
else {
item = x[top];
top--;
printf("Deleted item = %d", item);
}

getch();
}

void display(int x[10])


{
int i;

if (top == -1)
printf("Stack is empty");
else {
printf("Stack is:");
for (i = 0; i <= top; i++)
printf("x[%d] = %d\n", i, x[i]);
}

getch();
}
Program 15: Queue using Array
**Program for Queue (Using Array)**

#include <stdio.h>
#include <conio.h>

void insert(int[10], int);


void delete(int[10]);
void display(int[10]);
int n = 10, front = -1, rear = -1;
void main()
{
int a[10], ch, item;

do {
clrscr();
printf("Menu\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\nEnter element in queue: ");
scanf("%d", &item);
insert(a, item);
break;

case 2:
delete(a);
break;

case 3:
display(a);
break;

case 4:
printf("\nThanks for using my window");
getch();
exit(0);

default:
printf("Wrong choice");
}
} while (ch <= 4);

getch();
}
void insert(int x[10], int item)
{
if (rear == n - 1)
printf("\nOverflow");
else {
if (front == -1)
front++;

rear++;
x[rear] = item;
}

getch();
}

void delete(int x[10])


{
int item;

if (front == -1)
printf("\nUnderflow");
else {
item = x[front];
if (front == rear) {
front--;
rear--;
} else {
front++;
}
printf("Deleted item = %d", item);
}

getch();
}

void display(int x[10])


{
int i;

if (front == -1)
printf("\nQueue is empty");
else {
printf("Queue is:\n");
for (i = front; i <= rear; i++)
printf("q[%d] = %d\n", i, x[i]);
}
Getch():
}
Program 16: Circular Queue
#include <stdio.h>
#include <conio.h>

void insert(int[10], int);


void delete(int[10]);
void display(int[10]);
int n = 10, front = -1, rear = -1;
void main()
{
int a[10], ch, item;

do {
clrscr();
printf("Menu\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\nEnter element in circular queue: ");
scanf("%d", &item);
insert(a, item);
break;

case 2:
delete(a);
break;

case 3:
display(a);
break;

case 4:
printf("\nThanks for using my window");
getch();
exit(0);

default:
printf("Wrong choice");
}

} while (ch <= 4);


getch();
}
void insert(int x[10], int item)
{
if ((front == 0 && rear == n - 1) || (front == rear + 1))
printf("\nOverflow");
else {
if (front == -1)
front = 0;
if (rear == n - 1 && front != 0)
rear = 0;
else
rear++;

x[rear] = item;
}
getch();
}

void delete(int x[10])


{
int item;

if (front == -1)
printf("\nUnderflow");
else {
item = x[front];
if (front == rear) {
front = -1;
rear = -1;
} else if (front == n - 1)
front = 0;
else
front++;

printf("Deleted item = %d", item);


}

getch();
}
void display(int x[10])
{
int i;

if (rear == -1)
printf("\nQueue is empty");
else {
printf("Queue is:\n");
if (front > rear) {
for (i = front; i < n; i++)
printf("x[%d] = %d\n", i, x[i]);
for (i = 0; i <= rear; i++)
printf("x[%d] = %d\n", i, x[i]);
} else {
for (i = front; i <= rear; i++)
printf("x[%d] = %d\n", i, x[i]);
}
}

getch();
}
Program 17: Traversing in Linked List
**Program for Traversing in Linked List**

#include <stdio.h>
#include <conio.h>

int list[20], link[20], start;

void process(int p1)


{
list[p1] = list[p1] / 10;
}

void main()
{
int ptr;
clrscr();

list[1] = 11; list[2] = 22; list[3] = 33; list[4] = 44;


list[5] = 55; list[6] = 66; list[7] = 77; list[8] = 88;

link[1] = 2; link[2] = 3; link[3] = 4; link[4] = 5;


link[5] = 6; link[6] = 7; link[7] = 8; link[8] = 0;

start = 1;
ptr = start;

printf("\nInitial list:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

ptr = start;
while (ptr != 0)
{
process(ptr);
ptr = link[ptr];
}

ptr = start;
printf("\nList after traversal:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

getch();
}
Program 18: Insertion in Linked List
**Program for Insertion in Linked List**

#include <stdio.h>
#include <conio.h>

int list[20], link[20], start;


void process(int, int[], int[]);
void main()
{
int ptr;
clrscr();

list[1] = 11; list[2] = 22; list[3] = 33; list[4] = 44;


list[5] = 55; list[6] = 66; list[7] = 77; list[8] = 88; list[9] = 99;

link[1] = 2; link[2] = 3; link[3] = 4; link[4] = 5;


link[5] = 6; link[6] = 7; link[7] = 8; link[8] = 9; link[9] = 0;

start = 1;
ptr = start;

printf("\nInitial list:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

ptr = start;
process(ptr, list, link);

ptr = start;
printf("\nList after insert a new node:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

getch();
}

void process(int p1, int list[20], int link[20])


{
start = 100;
list[100] = 110;
link[100] = p1;
}
Program 19: Deletion in Linked List
**Program for Deletion in Linked List**

#include <stdio.h>
#include <conio.h>

int list[20];
int link[20];
int start;

void process(int, int[], int[]);

void main()
{
int ptr;
clrscr();

list[1] = 11; list[2] = 22; list[3] = 33; list[4] = 44;


list[5] = 55; list[6] = 66; list[7] = 77; list[8] = 88; list[9] = 99;

link[1] = 2; link[2] = 3; link[3] = 4; link[4] = 5;


link[5] = 6; link[6] = 7; link[7] = 8; link[8] = 9; link[9] = 0;

start = 1;
ptr = start;

printf("\nInitial list:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

ptr = start;
process(ptr, list, link);

ptr = start;
printf("\nList after delete a node:\n");
while (ptr != 0)
{
printf("\t%d", list[ptr]);
ptr = link[ptr];
}

getch();
}

void process(int p1, int list[20], int link[20])


{
start = link[start]; // deletes the first node by skipping it
}

You might also like