010-bds record (1)
010-bds record (1)
LAB RECORD
CYCLE 1 & 2
160120734010
WEEK-1 DATE : 29-09-2021
1) Write a C program to print elements in array and calculate sum of all the
elements in an array
AIM: To print elements in an array and calculate sum.
Description: To insert the elements in to Array and transverse (display) them and
then calculate sum.
ALGORITHM :
Transversal:
Step:1 intilaize the array elements ,I,and sum=0
Step:2 reading the array elements using loop
for(i=0;i<n;i++)
step:3 To transverse the array elements
step:4 Addition of the array elements
sum=sum+a[i]
step:5 printing the sum of array elements
CODE:
#include<stdio.h>
int main()
{
int a[5],sum=0,i,count;
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("array elements");
for(i=0;i<5;i++)
160120734010
{
printf("\n%d ",a[i]);
}
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
printf(" sum %d",sum”); }
INPUT : 1 2 3 4 5
OUTPUT : Sum = 15
WriteOVERFLOW
GotoStep7
[END OF IF]
Step 7: EXIT
CODE :
#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
160120734010
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *)
);
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
OUTPUT : Enter the item which you want to insert 12
Node Inserted
Press 0 to insert more
0
Enter the item which you want to insert 23
Node Inserted
Press 0 to insert more
1 .
160
120734010
2)AT THE END
AIM : To Insert an element at the End of the list
DESCRIPTION :Here directly we cannot insert the elements at the
end . We need to create a link using temp variable to the all
elements and insert .
INSERTION At the END : Algorithm
Step1: IFPTR=NULLWriteOVERFLOW
GotoStep1
[END OF IF]
Step8: SETPTR=PTR->NEXT
[END OF LOOP]
CODE :
#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node {
int data;
struct node *next;
}; struct node *head;
void main () { int choice,item;
do {
160120734010
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
lastinsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void lastinsert(int item)
{
struct node *ptr = (struct node*)malloc(sizeof(struct node
));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
} }
160120734010
OUTPUT : Enter the item which you want to insert 12
Node Inserted
Press 0 to insert more?
0
Enter the item which you want to insert 23
Node inserted
Press 0 to insert more
1
WRITEOVERFLOW
GOTOSTEP12
END OF IF
STEP 5: SET I = 0
WRITE"DESIREDNODENOTPRESENT"
GOTOSTEP12
ENDOFIF
END OF LOOP
CODE :
#include<stdio.h>
#include<stdlib.h>
void randominsert(int);
void create(int)
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
if(head == NULL)
{
create(item);
}
else
{
randominsert(item);
}
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
} void create(int item)
160120734010
{
struct node *ptr = (struct node *)malloc(sizeof(struct nod
e *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
} }
void randominsert(int item)
{
struct node *ptr = (struct node *) malloc (sizeof(struct no
de));
struct node *temp;
int i,loc;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else {
printf("Enter the location");
scanf("%d",&loc);
ptr->data = item;
temp=head;
for(i=0;i<loc;i++) {
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
} }
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
160120734010
} }
OUTPUT :
WriteUNDERFLOW
GotoStep5
[END OF IF]
Step 5: EXIT
CODE :
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
160120734010
/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
Else
{
/*
* In data of node from the user
*/
printf("Enter the data of node 1: ");
160120734010
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
/*
* Displays the entire list
*/
void displayList()
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
}
OUTPUT :
Enter the total number of nodes : 5
Enter the data of node 1 : 3
Enter the data of node 2 : 4
Enter the data of node 3 : 2
Enter the data of node 4 : 1
Enter the data of node 5 : 6
Singly LinkedList created Successfully
Data in the list
Data=3
160120734010
Data=4
Data=2
Data=1
Data=6
Press 1 to delete first node : 1
Data deleted = 3
Successfully Deleted first node from the list
Data in the list
Data= 4
Data= 2
Data= 1
Data= 6
STEP 1 START
CODE :
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address 160120734010
}*head;
int main()
{
int n, choice;
/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
/*
* If unable to allocate memory for head node
160120734010
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Input data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
/*
* Delete last node of the linked list
*/
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
if(toDelete == head)
{
head = NULL;
}
else
{
/* Disconnect link of second last node with last node */
secondLastNode->next = NULL;
}
/*
* Display entire list
*/
void displayList()
160120734010
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
}
}
}
OUTPUT :
160120734010
WRITEUNDERFLOW
GOTOSTEP10
END OF IF
STEP 3: SET I = 0
WRITE"DESIREDNODENOTPRESENT"
GOTOSTEP12
END OF IF
STEP 8: I = I+1
END OF LOOP
CODE :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
160120734010
// Data struct node *next; // Address
} *head;
/* Functions used in program */
void createList(int n);
void deleteMiddleNode(int position);
void displayList();
int main()
{
int n, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter the node position you want to delete: ");
scanf("%d", &position);
/* Delete middle node from list */
deleteMiddleNode(position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(toDelete == NULL)
break;
}
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
160120734010
prevNode->next = toDelete->next;
toDelete->next = NULL;
160120734010
160120734010
WEEK-7 Date : 24-11-2021
1)IMPLEMENTATION OF QUEUE
AIM : To perform Insertion, Deletion and Display operation in QUEUE
DESCRIPTION : Implementation of Queue operations in C The queue is
implemented without any functions and directly written with switch case.
Algorithm :
insert( )
1. If rear ≥ size-1
then write (‘overflow’)
2 Read item
3. rear← rear + 1
4. queue[rear]← item
5. If(front==-1)
6. front++;
7. stop
CODE :
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
160120734010
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT : Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter no 2:54
160120734010
Enter the Choice:1
Enter no 3:98
Enter no 4:234
Exit.
160120734010
WEEK 8 DATE: 15/12/2021
(a)LINEAR SEARCH
AIM: To Design, Develop and Implement a program in C for the Search operations
on Given Array of Elements.
DESCRIPTION: Search is a process of finding a value in a list of values. In other
words, searching is the process of locating given value position in a list of values.
Linear search algorithm finds a given element in a list of elements with O(n) time
complexity where n is total number of elements in the list. This search process starts
comparing search element with the first element in the list. If both are matched then
result is element found otherwise search element is compared with the next element in
the list. Repeat the same until search element is compared with the last element in the
list, if that last element also doesn't match, then the result is "Element not found in the
list". That means, the search element is compared with element by element in the list.
ALGORITHM: Linear search is implemented using following steps.
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate
the function.
Step 4 - If both are not matched, then compare search element with the next element
in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in
the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not
found!!!" and terminate the function
CODE
#include<stdio.h>
int main()
{
int i,data,n,k=0;
int found =0;
printf("enter the number of elements in array");
160120734010
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("enter the elements of array");
scanf("%d",&a[i]);
}
printf(" enter the element need to found");
scanf("%d",&data);
for(i=0;i<n;i++)
{
if(a[i]==data)
{
k=1;
i++;
}
if(a[i] != data)
{
k=0;
}
} Output :
if(k=1) Enter the series of the elements :
{ 5 10 15 19 20 22
printf("element is found"); Enter the element : 15
} Element found .
if(k=0)
{
printf("element is not found");
}
}
160120734010
(b) Binary Search
DESCRIPTION: Binary search algorithm finds a given element in a list of elements
with O(log n) time complexity where n is total number of elements in the list. The
binary search algorithm can be used with only a sorted list of elements. That means
the binary search is used only with a list of elements that are already arranged in an
order. This search process starts comparing the search element with the middle
element in the list. If both are matched, then the result is "element found. Otherwise,
we check whether the search element is smaller or larger than the middle element in
the list. If the search element is smaller, then we repeat the same process for the left
sub list of the middle element. If the search element is larger, then we repeat the same
process for the right sub list of the middle element. We repeat this process until we
find the search element in the list or until we left with a sublist of only one element.
And if that element also doesn't match with the search element, then the result is
"Element not found in the list".
ALGORITHM:
Binary search is implemented using following steps.
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list. Step 4
- If both are matched, then display "Given element is found!!!" and terminate func’n
Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and
5 for the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5
for the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.
CODE :
160120734010
#include <stdio.h>
int main(){
int c,first,last,middle,n,search,array[100];
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\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; Output :
} Enter the elements : 2 5 16 18
25
Else Enter the element you want to
search:16
last = middle - 1; 16 is present at the index 2.
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
160120734010
WEEK 9 DATE: 22/12/2021
CODE
#include<stdio.h>
int main()
{
int a[10],i,j,n,flag,temp;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data");
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
160120734010
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
printf("The sorted array :");
for(i=0;i<n;i++)
{
printf("\t%d\t",a[i]);
}
}
Input : enter n value
Enter the array elements 5 4 3 2 1
Output : The sorted list is 1 2 3 4 5
(b) SELECTION SORT
160120734010
ALGORITHM:
CODE
#include<stdio.h>
int main()
{
int a[10],i,n,j,min,temp;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data");
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
160120734010
}
}
if(min!=0)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("The sorted array :");
for(i=0;i<n;i++)
{
printf("\t%d\t",a[i]);
}
}
Input : Enter n value : 5
Enter array elements : 5 6 9 8 7
Output: The sorted list is 5 6 7 8 9
16012073401
0
WEEK 10 DATE:
29/12/2021
QUICK SORT
DESCRIPTION: Quick sort is a fast sorting algorithm used to sort a list of elements.
Quick sort algorithm is invented by C. A. R. Hoare. The quick sort algorithm attempts
to separate the list of elements into two parts and then sort each part recursively. That
means it use divide and conquer strategy. In quick sort, the partition of the list is
performed based on the element called pivot. Here pivot element is one of the
elements in the list. The list is divided into two partitions such that "all elements to the
left of pivot are smaller than the pivot and all elements to the right of pivot are greater
than or equal to the pivot". Complexity of the Quick Sort Algorithm 68 | P a g e To
sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n- 2)+(n-
3)+......+1) = (n (n-1))/2 number of comparisons in the worst case. If the list is already
sorted, then it requires 'n' number of comparisons.
ALGORITHM:
In Quick sort algorithm, partitioning of the list is performed using following steps
. Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in
the list)
. Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
CODE
#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;
160120734010
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
quicksort(number,0,count-1);
return 0;
}
Output : Enter the array size : 7
Enter the array elements : 5 2 6 8 1 9 7
The sorted list is : 1 2 5 6 7 8 9
160120734010
WEEK 11 DATE: 24/01/2022
HEAP SORT
DESCRIPTION: Heap sort is one of the sorting algorithms used to arrange a list of
elements in order. Heap sort algorithm uses one of the tree concepts called Heap Tree.
In this sorting algorithm, we use Max Heap to arrange list of elements in Descending
order and Min Heap to arrange list elements in ascending order.
ALGORITHM:
The Heap sort algorithm to arrange a list of elements in ascending order is performed
using following steps.
Step 3 - Delete the root element from Min Heap using Heapify method.
Step 6 - Display the sorted list. Complexity of the Heap Sort Algorithm To sort
an unsorted list with 'n' number of elements, following are the complexities. Worst
Case : O(n log n)
CODE:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
160120734010
}
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
160120734010
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100] = {15,5,20,1,7,10,30};
int n= size of(a), i;
printf("\n Before sorting the array elements are :");
PrintfArr(“a,n”);
HeapSort(a,n);
Printf(“\n After Sorting the array elements are:” )
printArray(arr, n);
return 0;
}
Output :
Before Sorting the array elements are :
15 5 20 1 7 10 30
After Sorting the array elements are :
1 5 7 10 15 20 30
1601207340
10
Assignment 2
Library Management system in C
Aim : To implement Library Management System in C :
Algorithm :
Step - 1: Declare a structure which holds data members
Step - 2: Declare variables which are used for loop
Step – 3: Use switch case to work on each module
Step – 4: Case 1 : For adding book information
Case 2 : For displaying book information
Case 3 : For finding no.of books in the library
Case 4 : Exit
CODE :
include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct library
{
char book_name[50];
char author[50];
int no.of pages;
float price;
};
int main()
{
struct library lib[100];
char authorname[30], bookname[30];
int i, j, count;
160120734010
i = j= count = 0;
while (j != 5) {
printf("\n\n1. Add book information\n2. Display book information\n");
printf("3. No.of books in the library\n");
printf("4. Exit");
printf("\n\nEnter one of the above: ");
scanf("%d", &j);
switch (j) {
// Add book
case 1:
printf("Enter book name: ");
scanf("%s", lib[i].book_name);
printf("Enter author name = ");
scanf("%s", lib[i].author);
printf("Enter pages = ");
scanf("%d", &lib[i].pages);
printf("Enter price = ");
scanf("%f", &lib[i].price);
count++;
i++;
break;
case 2:
printf("you have entered the following information\n");
for (i = 0; i < count; i++) {
printf("book name = %s “,lib[i].book_name);
printf("\t author name = %s", lib[i].author);
printf("\t pages = %d",lib[i].pages);
printf("\t price = %f", lib[i].price);
160120734010
}
break;
case 3:
printf(”no,of books in the library:%d”);
}
break;
case 4:
exit(0);
}
}
return 0;
}
Output:
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 1
Book name : Harry Potter
Author name : hp
Pages : 300
Price: 355.88
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 2
You have entered the following information
160120734010
Book name : Harry Potter
Author name : hp
Pages : 300
Price: 355
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 3
No. of books in the library : 10
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 4
Exit
160120734010