Algorithm Design Lab Manual
Algorithm Design Lab Manual
Practical File
On
Algorithm Design Lab
(16CSE22CL1)
#include<conio.h>
void main ()
clrscr ();
if(item==a[i][j])
1
{
found=1;
row=i;
col=j;
break;
if(found==1)
else
getch ();
OUTPUT: -
2
EXPERIMENT-2
Using iteration & recursion concepts write programs for finding the element
in the array Using Binary Search Method.
Using Iteration: -
#include<stdio.h>
#include<conio.h>
int iterativeBsearch(int A[], int size, int element);
void main() {
int A[] = {0,12,6,12,12,18,34,45,55,99};
int n=55;
clrscr();
printf("%d is found at Index %d \n",n,iterativeBsearch(A,10,n));
getch();
}
int iterativeBsearch(int A[], int size, int element) {
int start = 0;
int end = size-1;
while(start<=end) {
int mid = (start+end)/2;
if( A[mid] == element) {
return mid;
} else if( element< A[mid] ) {
end = mid-1;
} else {
start = mid+1;
}
}
return -1;
}
OUTPUT: -
3
Using Recursion: -
#include<stdio.h>
#include<stdio.h>
int 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.\n", search);
return 0;
}
OUTPUT: -
4
EXPERIMENT-3
Write a program to perform following operations Addition, Subtraction,
Multiplication and Transpose on tables using functions only.
#include<stdio.h>
#include<conio.h>
// main function
int main()
{
// table
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
clrscr();
// menu-driven
do
{
// menu to choose the operation
6
printf("\nChoose the table operation,\n");
printf(" \n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf(" \n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of table: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of table: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of table: \n");
display(c);
break;
case 4:
printf("Transpose of the first table: \n");
transpose(a, c);
display(c);
printf("Transpose of the second table: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}
OUTPUT: -
7
EXPERIMENT: - 4
Using iteration & recursion concepts write the programs for Quick Sort
Technique.
Using Iteration: -
#include <stdio.h>
8
}
9
int i;
for (i = 0; i < n; ++i)
printf("%d ", arr[i]);
}
OUTPUT: -
Using Recursion: -
#include <stdio.h>
int main()
{
int list[50];
int size, i;
10
printf("\nAfter implementing Quick sort, Sorted List is :: \n\n");
printf("\n");
return 0;
}
11
EXPERIMENT: -5
Write a program to implement the various operations on string such as length
of string concatenation, reverse of a string & copy of a string to another.
Length of String: -
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
length = strlen(a);
return 0;
}
OUTPUT: -
12
Copy of a string to another: -
#include <stdio.h>
#include <string.h>
int main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
strcpy(destination, source);
return 0;
}
OUTPUT: -
13
String Concatenation: -
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
strcat(a, b);
return 0;
}
OUTPUT: -
14
Reverse Of a String: -
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
strrev(s);
return 0;
}
OUTPUT: -
15
EXPERIMENT: -6
16
Write a program for swapping of two numbers using call by value and call by
reference strategies.
#include <stdio.h>
int temp;
temp = a;
a = b;
b = temp;
int main(void) {
swap(first,second);
printf(" \n After swap function called first number is %d and second number is %d", first
,second);
return 0;
}
OUTPUT: -
17
Swapping two number using call by reference: -
#include <stdio.h>
swap (int *, int *);
int main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
OUTPUT: -
18
EXPERIMENT: -7
19
Write a program to implement binary search tree. (Insertion and Deletion in
Binary search Tree).
Insertion: -
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
20
node->right = insert(node->right, key);
// Driver Code
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
return 0;
}
OUTPUT: -
Deletion: -
21
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
/* A utility function to
insert a new node with given key in
* BST */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);
22
}
return current;
}
23
struct node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
// Driver Code
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
24
printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inorder(root);
return 0;
}
OUTPUT: -
EXPERIMENT: -8
25
Write a program to create a linked list & perform operations such as insert,
delete, update, reverse in the link list.
#include <stdio.h>
// List is empty
if (start == NULL)
printf("\nList is empty\n");
26
// assigned to start
temp->link = start;
start = temp;
}
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
27
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
// If LL is empty
28
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
printf("\nEnter index : ");
// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
}
}
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
int max = temp->info;
29
while (temp != NULL) {
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// Traverse the LL
while (temp != NULL) {
30
// Print the mean value
printf("\nMean is %f ", m);
}
}
// If LL is empty
if (start == NULL) {
return;
}
// Else
else {
// Traverse the LL
while (current != NULL) {
index = current->link;
31
// Function to reverse the linked list
void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;
// If LL is empty
if (start == NULL)
printf("List is empty\n");
// Else
else {
// Traverse the LL
while (start != NULL) {
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}
// Driver Code
int main()
{
int choice;
while (1) {
32
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To find maximum among"
"the elements\n");
printf("\t9 To find mean of "
"the elements\n");
printf("\t10 To sort element\n");
printf("\t11 To reverse the "
"linked list\n");
printf("\t12 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
33
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
OUTPUT: -
34
35
36
EXPERIMENT: -9
#include<stdio.h>
struct student
{
int rollno;
char name[30];
float mark;
}stud;
// FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp = fopen("Record", "a");
printf("Enter the Roll no :");
scanf("%d", &[Link]);
printf("Enter the Name :");
scanf("%s", &[Link]);
printf("Enter the mark :");
scanf("%f", &[Link]);
fwrite(&stud, sizeof(stud), 1, fp);
fclose(fp);
}
// FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 = fopen("Record", "r");
printf("\nRoll Number\tName\tMark\n\n");
while (fread(&stud, sizeof(stud), 1, fp1))
printf(" %d\t\t%s\t%.2f\n", [Link], [Link], [Link]);
fclose(fp1);
}
// FUNCTION TO SEARCH THE GIVEN RECORD
void search()
{
FILE *fp2;
int r, s, avl;
printf("\nEnter the Roll no you want to search :");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
printf("Roll No %d is not available in the file\n",r);
37
else
{
fp2 = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp2))
{
s = [Link];
if (s == r)
{
printf("\nRoll no = %d", [Link]);
printf("\nName = %s", [Link]);
printf("\nMark = %.2f\n", [Link]);
}
}
fclose(fp2);
}
}
// FUNCTION TO DELETE A RECORD
void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the Roll no you want to delete :");
scanf("%d", &r);
if (avlrollno(r) == 0)
printf("Roll no %d is not available in the file\n", r);
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = [Link];
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
fwrite(&stud, sizeof(stud), 1, fpo);
printf("\nRECORD DELETED\n");
fclose(fpo);
38
fclose(fpt);
}
}
// FUNCTION TO UPDATE THE RECORD
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter roll number to update:");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
{
printf("Roll number %d is not Available in the file", r);
}
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = [Link];
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
else
{
printf("\n\t1. Update Name of Roll Number %d", r);
printf("\n\t2. Update Mark of Roll Number %d", r);
printf("\n\t3. Update both Name and Mark of Roll Number %d", r);
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Name:");
scanf("%s", &[Link]);
break;
case 2:
printf("Enter Mark : ");
scanf("%f", &[Link]);
break;
case 3:
printf("Enter Name: ");
39
scanf("%s", &[Link]);
printf("Enter Mark: ");
scanf("%f", &[Link]);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&stud, sizeof(stud), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
{
fwrite(&stud, sizeof(stud), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}
/* FUNCTION TO SORT THE RECORD */
void sort()
{
int a[20], count = 0, i, j, t, c;
FILE *fpo;
fpo = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fpo))
{
a[count] = [Link];
count++;
}
c = count;
for (i = 0; i<count - 1; i++)
{
for (j = i + 1; j<count; j++)
{
if (a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
40
}
}
printf("Roll No.\tName\t\tMark\n\n");
count = c;
for (i = 0; i<count; i++)
{
rewind(fpo);
while (fread(&stud, sizeof(stud), 1, fpo))
{
if (a[i] == [Link])
printf("\n %d\t\t %s \t\t %2f",[Link], [Link], [Link]);
}
}
}
// FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
int avlrollno(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("Record", "r");
while (!feof(fp))
{
fread(&stud, sizeof(stud), 1, fp);
if (rno == [Link])
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//FUNCTION TO CHECK THE FILE IS EMPTY OR NOT
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp))
c = 1;
fclose(fp);
return c;
}
// MAIN PROGRAM
41
void main()
{
int c, emp;
do
{
printf("\n\t---Select your choice---------\n");
printf("\n\t1. INSERT\n\t2. DISPLAY\n\t3. SEARCH");
printf("\n\t4. DELETE\n\t5. UPDATE\n\t6. SORT");
printf("\n\t7. EXIT");
printf("\n\n \n");
printf("\nEnter your choice:");
scanf("%d", &c);
printf("\n");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf("\nThe file is EMPTY\n");
else
disp();
break;
case 3:
search();
break;
case 4:
deletefile();
break;
case 5:
update();
break;
case 6:
emp = empty();
if (emp == 0)
printf("\n The file is EMPTY\n");
else
sort();
break;
case 7:
exit(1);
break;
default:
printf("\nYour choice is wrong\nPlease try again. \n");
42
break;
}
} while (c != 7);
}
OUTPUT: -
43
EXPERIMENT: -10
A) Add a node: -
#include <stdio.h>
#include <stdlib.h>
44
/* 2. allocate new node */
struct Node* new_node=(struct Node*) malloc(sizeof(struct Node));
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
45
// This function prints contents of linked list starting from head
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}
return 0;
}
OUTPUT: -
46
Delete a node: -
#include <stdio.h>
#include <stdlib.h>
47
if (temp == NULL)
return;
// Driver code
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
48