PDS-1 LAB MANUEL
PDS-1 LAB MANUEL
MANAPPARAI
Bachelor of Engineering
First Year
II SEMESTER
LAB MANUAL
Prepared By:
Mr. D. YOBU, Assistant professor
LTPC0032
OBJECTIVES:
TOTAL: 45 PERIODS
Algorithm:
1. Start the program.
2. Declare the variables and read the coefficients and constants.
3. Find the determinant.
4. If it is greater than zero, print the roots are real and distinct and find the roots.
5. If it is less than zero, print the roots are imaginary.
6. If it is equal to zero, print the roots are real and equal and find the roots.
7. Stop the program.
Output:
Enter coefficients a, b and
c: 2.3 4 5.6
To write a C program for finding whether a given number is Armstrong number or not
using loop control statements.
Algorithm:
Output:
Algorithm:
1. Start the program.
2. To multiply two matrixes sufficient and necessary condition is "number of columns in
matrix A = number of rows in matrix B".
3. Loop for each row in matrix A.
4. Loop for each columns in matrix B and initialize output matrix C to 0.
5. This loop will run for each rows of matrix A.
6. Loop for each columns in matrix A.
7. Multiply A[i,k] to B[k,j] and add this value to C[i,j]
8. Return output matrix C.
9. Stop the program.
20 5
20 5
Algorithm:
1. Start the program.
2. Get two strings as input.
3. Use the string compare function for checking the two strings are equal.
4. Use the string concatenation function for joining first string with second string.
5. Find the length of the strings using the strlen function.
6. Print the reverse of a string using strrev function.
7. Show the upper case and lower case of a string using strupr and strlwr functions.
8. Stop the program.
Output:
Algorithm:
1. Create an initial array with some elements in it.
2. Allocate memory for the elements using calloc function.
3. Check first element is greater than the second element of the array using pointers
and so on.
4. Print the largest element.
5. Stop the program.
Output:
Algorithm:
7. Calculate sum using the above formula, calculated sum is nothing but the decimal
representation of the given binary number.
8. Stop the program.
/*Number Conversion*/
#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\
n"); printf("2. Enter alphabet 'b' to convert decimal to
binary.\n"); scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number:
"); scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}
int decimal_binary(int n)
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
int binary_decimal(int n)
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
Kurinji College of Engineering & Technology Page 16
n/=10;
decimal +=
rem*pow(2,i); ++i;
}
return decimal;
}
Output:
Instructions:
1. Enter alphabet 'd' to convert binary to decimal.
2. Enter alphabet 'b' to convert decimal to binary.
b
Enter a decimal number: 8
8 in decimal = 1000 in binary
To write a C program to read data from keyboard, write it to a file named student
again read the same data from student file and write it into data file.
Algorithm:
1. Start the program.
2. Declare the file pointers.
3. Allocate the memory dynamically.
4. Store the roll number and name in a file.
5. Display them from the file.
6. Stop the program.
Algorithm:
1. Start the program.
2. Create a structure details and declare its members.
3. Get the values for the structure members as input.
4. Display them in output.
5. Stop the program.
Output:
Enter name:
Sachein
Enter age:
21
Enter Address:
Perambalur
Enter Salary:
28000
Algorithm:
A) Insertion:
1. Declare a new node and allocate memory for that node.
2. Get the data to be inserted and make the reference field of the node to NULL.
3. If first node is NULL, assign new node to first node; else, assign first to old node and
make the reference field of old node to point to new node.
B) Deletion:
1. Get the element to be deleted and assign first node to old node.
2. If old node is NULL, then return element not found; else, assign the value of
reference field of old to the reference field of temporary node and free the old node.
}
}
}
Output:
<-----Linked List Implementation----->
1. Insert
Kurinji College of Engineering & Technology Page 26
2. Delete
3. Print
4. Find
5. Exit
Enter Your Choice:
1
Enter Data to Insert:
10
1. Insert
2. Delete
3. Print
4. Find
5. Exit
Enter Your Choice:
1
Enter Data to Insert:
20
1. Insert
2. Delete
3. Print
4. Find
5. Exit
Enter Your Choice:
3
The list is 10 20
1. Insert
2. Delete
3. Print
4. Find
5. Exit
Enter Your Choice:
2
Enter Data to Delete:
20
1. Insert
2. Delete
3. Print
4. Find
5. Exit
Enter Your Choice:
4
Enter Data to
Find: 20
Element Not Found
1. Insert
2. Delete
3. Print
Kurinji College of Engineering & Technology Page 27
4. Find
5. Exit
Enter Your Choice:
5
Algorithm:
A) Push Operation:
1. To push an element into the stack, check whether the top of the stack is greater than
or equal to the maximum size of the stack.
2. If so, then return stack is full and element cannot be pushed into the stack.
3. Else, increment the top by one and push the new element in the new position of top.
B) Pop Operation:
1. To pop an element from the stack, check whether the top of the stack is equal to -1.
2. If so, then return stack is empty and element cannot be popped from the stack.
3. Else, decrement the top by one.
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice:
1
Enter The item to be pushed:
10
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice:
1
Enter The item to be pushed:
20
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice:
3
20
10
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice:
2
The popped element is 20
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice:
4
Algorithm:
A) Push Operation:
1. To push an element into the stack, copy the element to be inserted in the data field of
the new node.
2. Assign the reference field of the new node as NULL.
3. Check if top is not NULL, if so, then assign the value of top in the reference field of
new node.
4. Assign the address of the new node to the top.
B) Pop Operation:
1. To pop an element from the stack, check whether the top of the stack is NULL.
2. If so, then return stack is empty and element cannot be popped from the stack.
3. Else, assign the top value to a temporary node.
4. Now assign the value in the reference field of the node pointed by top to the top value.
5. Return the value in the data field of the temporary node as the element deleted
and delete the temporary node.
void push()
{
int item;
n *temp;
printf("Enter the item\n");
Kurinji College of Engineering & Technology Page 34
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item; temp-
>link=top;
top=temp;
}
void pop()
{
n *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data);
free(temp);
top=top->link;
}
}
void display()
{
n *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack
are :"); while(save!=NULL)
{
printf("%d\t",save->data);
save=save->link;
}
printf("\nTopmost element = %d\n",top->data);
}
}
Enter your
choice 1
Enter the item
10
Enter your
choice 1
Enter the item
20
Enter your
choice 3
The elements of the stack are :20 10
Topmost element = 20
Enter your
choice 2
The element deleted = 20
Enter your
choice 4
Algorithm:
1. Define a stack
2. Go through each character in the string
3. If it is between 0 to 9, append it to output string.
4. If it is left brace push to stack
5. If it is operator *+-/ then
a. If the stack is empty push it to the stack
b. If the stack is not empty then start a loop:
i. If the top of the stack has higher precedence
ii. Then pop and append to output string
iii. Else break
iv. Push to the stack
6. If it is right brace then
a. While stack not empty and top not equal to left brace
b. Pop from stack and append to output string
c. Finally pop out the left brace.
7. If there is any input in the stack pop and append to the output string.
main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("<-----Stack Application: Infix to Postfix Conversion----->\
n"); printf("\n\nRead the Infix Expression ? ");
scanf("%s",
infx); push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch; else
if (ch == ' )' )
{
Kurinji College of Engineering & Technology Page 38
while (s[top] != ' ( ' )
pofx[k++] = pop();
elem = pop();
}
else
{
while (pr(s[top]) >=
pr(ch)) pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#')
pofx[k++] = pop();
pofx[k] = '\0';
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}
Output:
Algorithm:
1. Start the program.
2. Scan the Postfix string from left to right.
3. Initialise an empty stack.
4. If the scannned character is an operand, add it to the stack. If the scanned character is
an operator, there will be atleast two operands in the stack.
5. If the scanned character is an Operator, then we store the top most element of the
stack(topStack) in a variable temp. Pop the stack. Now evaluate
topStack(Operator)temp. Pop the stack and Push result into the stack.
7. After all characters are scanned, we will have only one element in the stack. Return
topStack.
8. Stop the program.
int pop()
{
return(s[top--]);
}
main()
{
char pofx[50],ch;
int i=0,op1,op2;
printf("<-----Stack Application: Evaluating Postfix Expression----->\n");
printf("\n\nRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch))
push(ch-'0'); else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
printf("\n Given Postfix Expn: %s\n",pofx);
printf("\n Result after Evaluation: %d\n",s[top]);
}
Algorithm:
Enqueue:
1. Check if queue is not full.
2. If it is full then return queue overflow and item cannot be inserted.
3. If not, check if rear value is -1, if so then increment rear and by 1; if not increment
front by 1.
4. Store the item in the new value of front.
Dequeue:
1. Check if queue is not empty.
2. If it is empty, return queue underflow and dequeue operation cannot be done.
3. If not, check if rear and front are equal, if so assign -1 to front and rear; if
not decrement front by 1.
}
void display_queue()
{
int i;
if(front==-1)
printf("\n No elements to
display"); else
{
printf("\n The queue elements are:\n
"); for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}
}
1.Enqueue an element
2.Dequeue an element
3.Display queue
4.Exit
Enter your choice: 1
1.Enqueue an element
2.Dequeue an element
3.Display queue
4.Exit
Enter your choice: 1
1.Enqueue an element
2.Dequeue an element
3.Display queue
4.Exit
Enter your choice: 3
1.Enqueue an element
2.Dequeue an element
3.Display queue
4.Exit
Enter your choice: 2
1.Enqueue an element
2.Dequeue an element
3.Display queue
4.Exit
Enter your choice: 4
Algorithm:
Enqueue:
1. Create a new node and allocate memory space for the new node.
2. Assign the element to be inserted in the data field of the new node.
3. Assign NULL to the address field of the new node.
4. Check if rear and front pointers are NULL.
5. If so, then make the front and rear pointers to point to new node.
6. If not, then assign address of the new node as the rear pointer value.
Dequeue:
1. Check if queue is not empty.
2. If it is empty, return queue underflow and dequeue operation cannot be done.
3. If not, assign the front->next value as the new front pointer and free the deleted node.
void dequeue()
{
struct node *temp,
*var=rear; if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void display()
{
struct node *var=rear;
if(var!=NULL)
Kurinji College of Engineering & Technology Page 48
{
printf("\nElements in Queue: ");
while(var!=NULL)
{
printf("\t%d",var-
>data); var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int ch;
clrscr();
front=NULL;
printf("<-----Queue using Linked List----->\n");
printf(" \n1. Enqueue an element");
printf(" \n2. Dequeue an
element"); printf(" \n3. Display
Queue"); printf(" \n4. Exit\n");
while(1)
{
printf(" \nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int value;
printf("\nEnter a value to Enqueue:
"); scanf("%d",&value);
enqueue(value);
display();
break;
}
case 2:
{
dequeue();
display();
break;
}
case 3:
{
display();
Kurinji College of Engineering & Technology Page 49
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
1. Enqueue an element
2. Dequeue an element
3. Display Queue
4. Exit
Elements in Queue: 10
Elements in Queue: 10 20
Elements in Queue: 10 20 30
Elements in Queue: 20 30
Aim:
To Write a C Program to perform linear search and binary search.
Algorithm:
Linear Search
Binary Search
3. If search value is less than middle element then search left half of list with the same
method.
4. Else search right half of list with the same method.
Enter Elements:
24351
1.Linear Search
2.Binary
Search 3.Exit
Enter your choice:
1
<-----LINEAR SEARCH----->
Algorithm:
Insertion Sort
1. Get the n elements to be sorted.
2. The ith element is compared from (i-1)th to 0th element and placed in proper
position according to ascending value.
3. Repeat the above step until the last element.
Quick Sort
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements which are less than the pivot come before the
pivot and so that all elements greater than the pivot come after it.
3. After this partitioning, the pivot is in its final position. This is called the partition
operation.
4. Recursively sort the sub-list of lesser elements and the sub-list of greater elements
Bubble Sort
1. Get the n elements to be sorted.
2. Compare the first two elements of the array and swap if necessary.
3. Then, again second and third elements are compared and swapped if it is necessary
and continue this process until last and second last element is compared and swapped.
4. Repeat the above two steps n-1 times and print the result.
Enter Elements
24135
1.Insertion sort
2.Quick sort
3.Bubble sort
4.Exit
Enter your choice:
1
<-----Insertion SORT----->
Sorted List
1 2 3 4 5
1.Insertion sort
2.Quick sort
3.Bubble sort
4.Exit
Enter your choice:
2
<-----Quick SORT----->
Sorted List
1 2 3 4 5
1.Insertion sort
2.Quick sort
3.Bubble sort
4.Exit
Enter your choice:
3
<-----Bubble SORT----->
Sorted List
1 2 3 4 5