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

Manual Final

Uploaded by

Harshit T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Manual Final

Uploaded by

Harshit T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1. Program1a.

:
Write a C program to solve a quadric equation using the switch statement
#include <stdio.h>
#include <math.h>

int main() {
float a, b, c;
float root1, root2, imaginary;
float d;
int x; // Variable to store the case number

printf("Enter values of a, b, c of quadratic equation (ax^2 + bx


+ c)\n");
scanf("%f%f%f", &a, &b, &c);

d = (b * b) - (4 * a * c);

if (d > 0) {
x = 1;
} else if (d < 0) {
x = 2;
} else {
x = 3;
}

switch (x) {
case 1:
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
printf("Two distinct and real roots exist: %.2f and
%.2f", root1, root2);
break;

case 2:
printf("Roots are imaginary");
break;

case 3:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exist: %.2f and %.2f",
root1, root2);
break;
}
return 0;

“Perform ADDITIONAL program during Lab”


3x^2+12x+9=0
Program 1b:
Write a program to generate the pattern given below for required
number of lines
*****
*****
*****
*****
*****
*****

#include <stdio.h>

int main() {
int i, j;

for (j = 1; j <= 6; j++) {


for (i = 1; i <= 5; i++) {
printf("*");
}
printf("\n");
}

return 0;
}
“Perform ADDITIONAL program during Lab”
Write a program to generate the pattern given below for required
number of lines
*
**
***
****
*****

2. Program 2a:
WAP to sort an array of numbers in ascending order

#include <stdio.h>

int main()
{
int a[10], n, temp, i, j, k;

printf("Enter the size of the array (max 10):\n");


scanf("%d", &n);

if (n > 10) {
printf("Size should not exceed 10.\n");

return 1;
}
printf("Enter %d elements one by one:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

printf("The unsorted array is:\n\n");


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

// Bubble Sort
for (j = 0; j < n - 1; j++)
{
for (k = 0; k < n - 1 - j; k++)
{
if (a[k] > a[k + 1])
{
temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
}

printf("\nThe sorted array after bubble sort is:\n\n");


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

return 0;
}

“Perform ADDITIONAL program during Lab”


WAP to search an element in a array
Program 2b:
Create a structure called employee having members for accepting name of employee, his employment
number & salary

a)Search an employee in the created database using function

b)Sort the created database in increasing order of their salary using function
#include <stdio.h>

// Define the structure for Employee


typedef struct {
char name[100];
int emp_number;
float salary;
} Employee;

// Function to search for an employee by employment number


Employee* searchEmployee(Employee employees[], int size, int
emp_number) {
int i;
for (i = 0; i < size; i++) {
if (employees[i].emp_number == emp_number) {
return &employees[i];
}
}
return NULL;
}

// Function to sort employees by salary in increasing order


void sortEmployeesBySalary(Employee employees[], int size) {
Employee temp;
int i, j;
for (i = 0; i < size - 1; i++) {
for (j = i + 1; j < size; j++) {
if (employees[i].salary > employees[j].salary) {
// Swap the employees
temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
}

// Function to print employee details


void printEmployees(Employee employees[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("Name: %s, Employment Number: %d, Salary: %.2f\n",
employees[i].name, employees[i].emp_number,
employees[i].salary);
}
}

int main() {
int size = 3; // Number of employees
int i;
int emp_number_to_search = 1002; // Variable to search for an
employee
Employee employees[3]; // Declare an array of Employee structures

// Input employee data


printf("Enter data for %d employees:\n", size);
for (i = 0; i < size; i++) {
printf("Enter name for employee %d: ", i + 1);
scanf("%s", employees[i].name);
printf("Enter employment number for employee %d: ", i + 1);
scanf("%d", &employees[i].emp_number);
printf("Enter salary for employee %d: ", i + 1);
scanf("%f", &employees[i].salary);
}

// Print unsorted employee list


printf("\nUnsorted employee list:\n");
printEmployees(employees, size);

// Sort employees by salary


sortEmployeesBySalary(employees, size);

// Print sorted employee list


printf("\nSorted employee list by salary:\n");
printEmployees(employees, size);

// Search for an employee


Employee* found_employee = searchEmployee(employees, size,
emp_number_to_search);
if (found_employee != NULL) {
printf("\nEmployee found: %s, %d, %.2f\n",
found_employee->name, found_employee->emp_number,
found_employee->salary);
} else {
printf("\nEmployee with employment number %d not found.\n",
emp_number_to_search);
}

return 0;
}

“Perform ADDITIONAL program during Lab”


Displaying the average salary of all employees.
Program 3
Write a program in C to simulate the working of a stack of integers using an array. Provide the
following operations.

a.Insert b.Delete c. Display


#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int stack[100], ch, top, x,i;
void push();
void pop();
void display();
int main()
{
top=-1;
printf("1:push 2:pop 3:display \n");
for(;;)
{
printf("enter your choice: \n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default: printf("Invalid choice \n");
exit(0);
}
}

}
void push()
{
if(top==SIZE-1)
{
printf("stack overflow \n");
}
else
{
printf("enter the element to be pushed:\n");
scanf("%d",&x);
top=top+1;
stack[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("stack underflow\n");
}
else
{
printf("the poped element is %d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("the element are:\n");
for(i=top; i>=0; i--)
{
printf("%d\n", stack[i]);
}
}
else{
printf("stack is empty");
}
}

“Perform ADDITIONAL program during Lab”


Write a program in C to simulate the working of a stack of characters using an array. Provide the
following operations.
a.peek operation

Program 4
Given a valid parenthesized infix arithmetic expression convert it to postfix expression. The expression
consists of single character operands and the binary operators + (plus), -(minus), * (multiply) and /
(divide).
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}
return 0;
}

“Perform ADDITIONAL program during Lab”


Given a valid parenthesized postfix expression convert it to infix expression. The expression consists of
single character operands and the binary operators + (plus), -(minus), * (multiply) and / (divide).

Program 5
Design, develop and execute a program in C to simulate the working
of a queue of integers using an array. Provide the following
operations.
a. Insert
b. Delete
c. Display
#include<stdio.h>
#define maxsize 3
int q[maxsize], front=0,rear=-1;
void insert()
{
int n;
if(rear==maxsize-1)
printf("\nQueue full\n");
else
{
printf("\nEnter the data to be added\n");
scanf("%d", &n);
q[++rear]=n;
}
}
void delete()
{
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\n%d is deleted\n",q[front++]);
if(front>rear && rear==maxsize-1)
{
printf("\nReinit\n");
front=0; rear=-1;
}
}
}
void display()
{
int i;
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\nQueue status is\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
}
int main()
{
int ch;
while(1)
{
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
puts("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2:delete(); break;
case 3:display(); break;
case 4: return 0;
default :printf("\nInvalid choice\n");
}
}
}
“Perform ADDITIONAL program during Lab”
Design, develop and execute a program in C to simulate the working of a queue of charaters an array.
Provide the following operations.
Peek and search

Program 6
Simulate the working of Messaging system in which a message is placed in a
circular Queue by a message sender, a message is removed from the circular
queue by a message Receiver, which can also display the contents of the queue.
#include <stdio.h>
#define sz 5
int count =0;
int f=0,r=-1;
int q[sz];
void push(){
int item;
if(count == sz){
printf("Queue overflow");
return;
}
printf("Enter the element ");
scanf("%d",&item);
r = (r+1)%sz;
q[r]=item;
count++;
}
void pop(){
if(count == 0){
printf("queue underflow");
return;
}
printf("Item deleted is %d \n",q[f]);
f = (f+1)%sz;
count--;
}
void display(){
if(count == 0){
printf("Queue is empty \n");
return;
}
int j = f;
for(int i=0;i<count;i++){
printf("%d \t",q[j]);
j = (j+1)%sz;
}
printf("\n");
}
int main()
{
int option;
while(1){
printf("1.Push 2.Pop 3.Display 4. Exit \n");
printf("Enter the option ");
scanf("%d",&option);
switch(option){
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return 0;
}
}
return 0;
}
“Perform ADDITIONAL program during Lab”
WAP for ticket booking system where booking requests from customers are placed in a circular
queue. When a ticket agent becomes available, the next request is processed. The operations
should include:

 Adding a booking request to the queue


 Processing a booking request (removing it from the queue)
 Displaying all pending booking requests.

Program 7
Design, develop and execute a program in C to perform the following operation on Singly
linked list
a.Insert and Delete a node at the beginning of the list b. Display the list

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

struct link
{
int data;
struct link *next;
};

typedef struct link node;


void addfirst();
void delfirst();
void display();
node *head=NULL;

void main()
{
int ch;
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.DeleteFirst\n3.Display\n4.Exit\n");
do
{
printf("\nEnter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
break;
case 2:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
delfirst();
break;
case 3:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=4);
getch();
}

void addfirst()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....\t");
scanf("%d",&temp->data);
temp->next=head;
head=temp;
}

void delfirst()
{
node *temp=head;
head=head->next;
printf("\nDeleted item is %d\n",temp->data);
free(temp);
}

void display()
{
node *cur=head;
printf("\nLINKED LIST : ");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->next;
}
}
Program 8:
Design, develop and execute a program in C to perform the following operation on Singly
linked list
a.Insert and Delete a node at the end of the list b. Display the list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct link
{
int data;
struct link *next;
};

typedef struct link node;


void addlast();
void dellast();
void display();
node *head=NULL;

void main()
{
int ch;
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addlast\n2.Deletelast\n3.Display\n4.Exit\n");
do
{
printf("\nEnter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addlast();
break;
case 2:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
dellast();
break;
case 3:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=4);
getch();
}

void addlast()
{
node *temp,*cur;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....");
scanf("%d",&temp->data);
temp->next=NULL;
if(head == NULL)
head =temp;
else
{
cur=head;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
}
}

void dellast()
{
node *temp,*cur=head;
if(cur->next==NULL)
{
printf("Deleted item is %d\n",cur->data);
free(cur);
head =NULL;
}
else
{
while(cur->next->next!=NULL)
cur=cur->next;
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->data);
free(temp);
}
}

void display()
{
node *cur=head;
printf("\nLINKED LIST : ");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->next;
}

Additional Program
WAP to insert and delete a node at the opposite end of a singly linked list and
display the same
Program 9:
Design, develop and execute a program in C to perform the following operation on doubly
linked list
a.Insert and Delete a node at the beginning of the list b. Display the list

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

struct link {
struct link *prev;
int item;
struct link *next;
};

typedef struct link node;


void addfirst();
void delfirst();
void display();
node *head = NULL;

void main() {
int ch;
printf("\nDOUBLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.DeleteFirst\n3.Display\n4.Exit\n");

do {
printf("\nEnter your option:\t");
scanf("%d", &ch);
switch (ch) {
case 1:
addfirst();
break;
case 2:
if (head == NULL)
printf("\nLIST IS EMPTY.....\n");
else
delfirst();
break;
case 3:
if (head == NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
} while (ch != 4);
}

void addfirst() {
node *temp;
temp = (node *)malloc(sizeof(node));
printf("\nEnter the data....\t");
scanf("%d", &temp->item);
temp->prev = NULL;
temp->next = head;

if (head != NULL) // Update the previous head's prev pointer


only if the list is not empty
head->prev = temp;

head = temp; // Update head to point to the new node


}

void delfirst() {
node *temp = head;
head = head->next;
if (head != NULL) // Check if the list is not becoming empty
after deletion
head->prev = NULL;

printf("Deleted item is %d\n", temp->item);


free(temp);
}

void display() {
node *cur = head;
printf("\nLINKED LIST :");
while (cur != NULL) {
printf("\t%d", cur->item);
cur = cur->next;
}
}
Additional WAP to insert and delete at the end
Program 10:
/*WAP to implement a circularly linked list by inserting elements at beginning of list
and removing from end of the list*/

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

struct num
{
int data;
struct num *link;
};
typedef struct num node;
node *head,*tail;// pointer to head node

Delete(int *x);
Insert(const int x) ;
void display();

//removes elements from end of list


Delete( int *x)
{
node *q;
if (head == NULL)
printf("list is empty\n");
else
{
if(head == tail)
{
*x=head->data;
free (head);
head = tail= NULL;
}
else
{
q= head;
while(q->link !=tail)
q= q->link;
*x=tail->data;
free(tail);
tail =q;
tail -> link = head;
}

}
//inserts elements at the beginning of the list
Insert(const int x)
{
node *q;
q= (node *)malloc(sizeof(node));
q->data = x;
if(head == NULL)
{
head=q;
tail = q;
}
else
{ q->link =head;
head = q;
}
tail->link = head;
}

void display()
{ node *p,*q;
p = head;
q=NULL;

while (p!=q)
{ printf("%d\t",p->data);
p = p->link;
q = head;
}

main()
{
int z;
Insert(2);
Insert(6);
Insert(5);
Insert(7);
printf("\nList is =" );
display();
Delete(&z);
printf("\nDeleted element is %d\n", z);
printf("\nList is =" );
display();
Delete(&z);
printf("\nDeleted element is %d\n", z);
printf("List is =" );
display();
}
Program 11:
/* WAP to create a binary tree and display the contents of the tree using
preorder, post order and inorder traversal techniques*/
#include <stdio.h>
#include <stdlib.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};

// Function declarations
struct tree* newNode(int data);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);

int main(void) {
struct tree *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

printf("\nINORDER TRAVERSAL: ");


inorder(root);
printf("\nPREORDER TRAVERSAL: ");
preorder(root);

printf("\nPOSTORDER TRAVERSAL: ");


postorder(root);

return 0;
}

// Function to create a new node


struct tree* newNode(int data) {
struct tree *node = (struct tree*) malloc(sizeof(struct tree));
node->info = data;
node->left = NULL;
node->right = NULL;
return node;
}

// Function to display contents of the tree using inorder traversal (LNR)


void inorder(struct tree *r) {
if (r != NULL) {
inorder(r->left);
printf(" %d", r->info);
inorder(r->right);
}
}

// Function to display contents of the tree using postorder traversal (LRN)


void postorder(struct tree *r) {
if (r != NULL) {
postorder(r->left);
postorder(r->right);
printf(" %d", r->info);
}
}

// Function to display contents of the tree using preorder traversal (NLR)


void preorder(struct tree *r) {
if (r != NULL) {
printf(" %d", r->info);
preorder(r->left);
preorder(r->right);
}
}

Program 12

// WAP to create a Binary search tree and search a given data in the tree
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* A binary tree node has data, pointer to left child and a pointer to right child */
struct tree
{
int data;
struct tree* left;
struct tree* right;
};
struct tree *root = NULL;

//function to traverse the tree inorder


void inorder(struct tree *r)
{
if(r != NULL)
{
inorder(r->left);
printf(" %d",r->data);
inorder(r->right);
}
return;
}

//function to insert a node to a binary search tree


void insert(int d)
{
struct tree *t = (struct tree *)malloc(sizeof(struct tree));
t->data = d;
t->left = NULL;
t->right = NULL;
if(root == NULL)
root =t;
else
{
struct tree *cur = root, *parent;
while(cur != NULL)
{
parent = cur;
if(t->data > cur->data)
cur = cur->right;
else
cur = cur->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}

//function to search a given data in a binary search tree


int search( int d)
{
struct tree *p = root;
while(p != NULL)
if(d < p->data)
p = p -> left;
else if(d > p->data)
p = p -> right;
else if(d == p->data)
return 1;
return 0;
}

main()
{
int num,info,i,s,found;
printf("\nenter the number of nodes required in the tree");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\nenter the data");
scanf("%d",&info);
insert(info);
}
printf(" \ncontents of the tree using inorder traversal technique is");
inorder(root);
printf("\nenter the data to be searched");
scanf("%d",&s);
found = search(s);
if( found == 1)
printf( "\nelement being searched is found in the tree");
else
printf( "\nelement being searched is not found in the tree");
}

You might also like