1. Apply the mathematical logic and generate the following
series,
1/5! + 2/4! + 3/3! + 4/2! + 5/1!
(!- represents Factorial)
Also compute the value of the series.
CODE:
#include<stdio.h>
int fact(int n)
{
if(n==1)
return
1;
else
{
return
n*fact(n-1);
}
}
int main()
{
int i;
float
sum=0.0;
printf("\n%The
series is :\n");
for(i=1;i<=5;i++)
{
printf("%f
",i/(float)fact(6-i));
sum+=i/fact(6-i);
}
printf("\nThe
sum is:%f\n",sum);
return
0;
}
OUTPUT:
2.
Write code to perform Linear Search
CODE:
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is
found");
return 0;
}
OUTPUT
3.
Write code to perform Binary Search
CODE:
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not
found.");
else
printf("The number is
found.");
return 0;
}
1. Develop a C
program to create two other stacks stack2 and stack3 by removing odd
positioned and even positioned
elements from the stack1 .
CODE:
#include<iostream>
using namespace std;
int top=-1;
int a[5];
int top2=-1;
int a2[5];
int top3=-1;
int a3[5];
void push(int a[],int& t,int key)
{
a[++t]=key;
}
int pop(int a[],int &top)
{
int
temp=a[top];
top--;
return
temp;
}
int main()
{
push(a,top,0);
push(a,top,2);
push(a,top,3);
push(a,top,4);
push(a,top,5);
int i;
for(i=0;i<top+4;i++)
{
if(i%2==0)
push(a2,top2,pop(a,top));
else
push(a3,top3,pop(a,top));
}
printf("STACK
ODD :\n");
for(i=0;i<top2+2;i++)
printf("%d
",a2[i]);
printf("\n");
printf("STACK
EVEN :\n");
for(i=0;i<top3+1;i++)
printf("%d
",a3[i]);
return
0;
}
OUTPUT:
Q) Develop a
Queue1. Find the odd elements and even elements in it. Prepare two other queues
such that Queue2 is square of odd elements in Queue1 and Queue3 contains even
elements from Queue1.
CODE:
#include<stdio.h>
#include<stdlib.h>
typedef
struct Queue
{
int capacity;
int size;
int front;
int rear;
int *elements;
}Queue;
Queue *
createQueue(int maxElements)
{
Queue *Q;
Q = (Queue *)malloc(sizeof(Queue));
Q->elements = (int
*)malloc(sizeof(int)*maxElements);
Q->size = 0;
Q->capacity = maxElements;
Q->front = 0;
Q->rear = -1;
return Q;
}
int
Dequeue(Queue *Q)
{
int item;
if(Q->size==0)
{
printf("Queue is
Empty\n");
return;
}
else
{
Q->size--;
item
=Q->elements[Q->front];
Q->front++;
if(Q->front==Q->capacity)
{
Q->front=0;
}
}
return item;
}
int
front(Queue *Q)
{
if(Q->size==0)
{
printf("Queue is
Empty\n");
exit(0);
}
return Q->elements[Q->front];
}
void
Enqueue(Queue *Q,int element)
{
if(Q->size == Q->capacity)
{
printf("Queue is
Full\n");
}
else
{
Q->size++;
Q->rear = Q->rear + 1;
if(Q->rear == Q->capacity)
{
Q->rear = 0;
}
Q->elements[Q->rear] =
element;
}
return;
}
int main()
{
Queue *Q1 = createQueue(5);
Queue *Q2 = createQueue(5);
Queue *Q3 = createQueue(5);
Enqueue(Q1,1);
Enqueue(Q1,2);
Enqueue(Q1,3);
Enqueue(Q1,4);
int i;
Enqueue(Q1,5);
printf("\n");
printf("Queue
1:\n");
for(i=0;i<Q1->size;i++)
{
printf("%d
",Q1->elements[i]);
}
for(i=0;i<Q1->size+4;i++)
{
if(i%2==0)
{int
item=Dequeue(Q1);
Enqueue(Q2,item*item);
}
else
{int
item=Dequeue(Q1);
Enqueue(Q3,item);
}
}
printf("\n");
printf("Queue
2:\n");
for(i=0;i<Q2->size;i++)
{
printf("%d
",Q2->elements[i]);
}
printf("\n");
printf("Queue
3:\n");
for(i=0;i<Q3->size;i++)
{
printf("%d
",Q3->elements[i]);
}
}
OUTPUT
SCREEN:
1. Take the details of 5 Employees such as
Employee name, Employee ID, Department,Designation, Blood group, address and
phone number. Ensure that the Employee IDs are unique. Sort the Employee
records based on length of Employee names. Use Linked List
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node
{
int id;
char name[20];
char dep[20];
char des[20];
char bg[20];
char phno[20];
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList()
{
struct node *ptr =
head;
printf("\n[
");
//start from the
beginning
while(ptr != NULL)
{
printf("%d ,
",ptr->id);
printf("%s
->",ptr->name);
ptr =
ptr->next;
}
printf("
]");
}
//insert link at the first location
void insertFirst(char name[], int id)
{
//create a link
struct node *link =
(struct node*) malloc(sizeof(struct node));
strcpy(link->name,name);
link->id = id;
//point it to old
first node
link->next =
head;
//point first to
new first node
head = link;
}
//delete first item
struct node* deleteFirst()
{
//save reference to
first link
struct node
*tempLink = head;
//mark next to
first link as first
head =
head->next;
//return the
deleted link
return tempLink;
}
//is list empty
bool isEmpty()
{
return head ==
NULL;
}
int length()
{
int length = 0;
struct node
*current;
for(current = head;
current != NULL; current = current->next)
{
length++;
}
return length;
}
void sort(){
int i, j, k,
tempKey;
char tempData[20];
struct node
*current;
struct node *next;
int size =
length();
k = size ;
for ( i = 0 ; i
< size - 1 ; i++, k-- ) {
current = head ;
next =
head->next ;
for ( j = 1 ; j
< k ; j++ ) {
if
(strcmp(current->name,next->name)>0 ) {
strcpy(tempData,current->name);
strcpy(current->name,next->name);
strcpy(next->name,tempData);
tempKey =
current->id;
current->id = next->id;
next->id = tempKey;
}
current =
current->next;
next =
next->next;
}
}
}
main() {
insertFirst("a",10);
insertFirst("ab",20);
insertFirst("abc",30);
insertFirst("abcd",1);
printf("Original List: ");
//print list
printList();
printf("\n");
sort();
printf("List
after sorting the id: ");
printList();
}
OUTPUT:
2.Design a C program for Book Database using
Single linked list and perform search,Insert and delete and update operation
for a particular register number.
CODE:
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<stdbool.h>
struct node
{
int price;
char name[20];
int id;
struct node *next;
};
struct node
*head = NULL;
struct node
*current = NULL;
//display the
list
void printList()
{
struct node *ptr = head;
printf("\n[ ");
//start from the beginning
while(ptr != NULL)
{
printf("(%d,%s,%d)
",ptr->id,ptr->name,ptr->price);
ptr = ptr->next;
}
printf(" ]");
}
//insert link at
the first location
void
insertFirst(int id, char name[],int price)
{
//create a link
struct node *link = (struct node*)
malloc(sizeof(struct node));
link->id = id;
strcpy(link->name,name);
link->price = price;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
//is list empty
bool isEmpty()
{
return head == NULL;
}
int length()
{
int length = 0;
struct node *current;
for(current = head; current != NULL; current
= current->next)
{
length++;
}
return length;
}
//delete a link
with given id
struct node*
delete(int id){
//start from the first link
struct node* current = head;
struct node* previous = NULL;
//if list is empty
if(head == NULL){
return NULL;
}
//navigate through list
while(current->id != id){
//if it is last node
if(current->next == NULL){
return NULL;
}else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == head) {
//change first to point to next link
head = head->next;
}else {
//bypass the current link
previous->next = current->next;
}
return current;
}
//delete a link
with given id
struct node*
update(int id,char name[],int price){
//start from the first link
struct node* current = head;
struct node* previous = NULL;
//if list is empty
if(head == NULL){
return NULL;
}
//navigate through list
while(current->id != id){
//if it is last node
if(current->next == NULL){
return NULL;
}else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
strcpy(current->name,name);
current->price=price;
return current;
}
main() {
insertFirst(1,"BOOK1",10);
insertFirst(2,"BOOK2",100);
insertFirst(3,"BOOK3",110);
printf("Original List: ");
//print list
printList();
update(2,"B2",500);
printf("\nList after updating an item:
");
printList();
delete(2);
printf("\nList after deleting an item:
");
printList();
printf("\n");
}
OUTPUT:
Q1)
Print nodes at k distance from root
Given a root
of a tree, and an integer k. Print all the nodes which are at k distance from
root. For example, in the below tree, 4, 5, 8 are at distance 2 from root and
6,7 are at distance 3 from root.
CODE:
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to
right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
void printKDistant(struct node *root , int k)
{
if(root == NULL)
return;
if( k == 0 )
{
printf( "%d
", root->data );
return ;
}
else
{
printKDistant(
root->left, k-1 ) ;
printKDistant(
root->right, k-1 ) ;
}
}
/* Helper function that allocates a new node with the
given data and NULL
left and right pointers. */
struct node* newNode(int data)
{
struct node* node =
(struct node*)
malloc(sizeof(struct node));
node->data =
data;
node->left =
NULL;
node->right =
NULL;
return(node);
}
/* Driver program to test above functions*/
int main()
{
/* Constructed
binary tree is
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
*/
struct node *root =
newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left =
newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(8);
root->right->right->left
= newNode(6);
root->right->right->right = newNode(7);
int i;
for(i=0;i<4;i++)
{
printf("Nodes at
a dist of %d :\n",i);
printKDistant(root,
i);
printf("\n");
}
return 0;
}
OUTPUT:
Q1.In
Binary Search Tree, Inorder successor of a node is the next node in its Inorder
traversal . Inorder Successor is NULL for the last node in Inoorder traversal.
In Binary Search Tree, Inorder Successor of an input node can also be defined
as the node with the smallest key greater than the key of input node. So, it is
sometimes important to find next node in sorted order. For Example consider the
BST as in diagram, inorder successor of 8 is 10.
Perform the following:
1.
Creation
2.
Insertion
3.
Deletion
4. Print
CODE:
/*
* C Program to
Construct a Binary Search Tree and perform deletion, inorder traversal on it
*/
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\nOPERATIONS ---");
printf("\n1 -
Insert an element into tree\n");
printf("2 -
Delete an element from the tree\n");
printf("3 -
Inorder Traversal\n");
printf("4 -
Preorder Traversal\n");
printf("5 -
Postorder Traversal\n");
printf("6 -
Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create()
{
int data;
printf("Enter
data of node to be inserted : ");
scanf("%d", &data);
temp = (struct
btnode *)malloc(1*sizeof(struct btnode));
temp->value =
data;
temp->l =
temp->r = NULL;
}
/* Function to search the appropriate position to insert the
new node */
void search(struct btnode *t)
{
if ((temp->value
> t->value) && (t->r != NULL)) /* value more than root node value insert
at right */
search(t->r);
else if
((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if
((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert
at left */
search(t->l);
else if
((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No
elements in a tree to display");
return;
}
if (t->l !=
NULL)
inorder(t->l);
printf("%d
-> ", t->value);
if (t->r !=
NULL)
inorder(t->r);
}
/* To check for the deleted node */
void delete()
{
int data;
if (root == NULL)
{
printf("No
elements in a tree to delete");
return;
}
printf("Enter
the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root,
data);
}
/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No
elements in a tree to display");
return;
}
printf("%d
-> ", t->value);
if (t->l !=
NULL)
preorder(t->l);
if (t->r !=
NULL)
preorder(t->r);
}
/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No
elements in a tree to display ");
return;
}
if (t->l !=
NULL)
postorder(t->l);
if (t->r !=
NULL)
postorder(t->r);
printf("%d
-> ", t->value);
}
/* Search for the appropriate position to insert the new node
*/
void search1(struct btnode *t, int data)
{
if
((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data <
t->value))
{
t1 = t;
search1(t->l, data);
}
else if
((data==t->value))
{
delete1(t);
}
}
/* To delete a node */
void delete1(struct btnode *t)
{
int k;
/* To delete leaf
node */
if ((t->l ==
NULL) && (t->r == NULL))
{
if (t1->l ==
t)
{
t1->l =
NULL;
}
else
{
t1->r =
NULL;
}
t = NULL;
free(t);
return;
}
/* To delete node
having one left hand child */
else if ((t->r
== NULL))
{
if (t1 == t)
{
root =
t->l;
t1 = root;
}
else if
(t1->l == t)
{
t1->l =
t->l;
}
else
{
t1->r =
t->l;
}
t = NULL;
free(t);
return;
}
/* To delete node
having right hand child */
else if (t->l ==
NULL)
{
if (t1 == t)
{
root =
t->r;
t1 = root;
}
else if
(t1->r == t)
t1->r =
t->r;
else
t1->l =
t->r;
t == NULL;
free(t);
return;
}
/* To delete node
having two child */
else if ((t->l
!= NULL) && (t->r != NULL))
{
t2 = root;
if (t->r !=
NULL)
{
k =
smallest(t->r);
flag = 1;
}
else
{
k
=largest(t->l);
flag = 2;
}
search1(root,
k);
t->value =
k;
}
}
/* To find the smallest element in the right sub tree */
int smallest(struct btnode *t)
{
t2 = t;
if (t->l !=
NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return
(t->value);
}
/* To find the largest element in the left sub tree */
int largest(struct btnode *t)
{
if (t->r !=
NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}
OUTPUT:
Develop
the following:
1. Sorting using Insertion sort
CODE:
#include<stdio.h>
int main(){
int i,j,s,temp;
int
a[10]={3,4,2,1,5,6,9,8,7,10};
printf("\nINSERTION SORT \n");
printf("\nBefore sorting: \n");
for(i=0;i<10;i++)
printf("
%d",a[i]);
for(i=1;i<10;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nAfter
sorting: \n");
for(i=0;i<10;i++)
printf("
%d",a[i]);
return 0;
}
OUTPUT:
2. Sorting using bubble sort
CODE:
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
printf("Enter total numbers of elements:
");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
//Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
OUTPUT:
3.
Sorting using Selection Sort
CODE:
#include<iostream>
int
A[10]={3,4,2,1,5,6,9,8,7,10};
using namespace
std;
int main()
{
int i,j;
for(i=0;i<10;i++)
printf("%d ",A[i]);
printf("\nSELECTION SORT
:\n");
for(i=0;i<10;i++)
{
int min=i;
for(j=i+1;j<10;j++)
{
if(A[min]>A[j])
{min=j;}
}
int
t=A[min];
A[min]=A[i];
A[i]=t;
}
for(i=0;i<10;i++)
printf("%d ",A[i]);
return 0;
}
OUTPUT:
No comments:
Post a Comment