data science

1. Write a C program to construct a stack of integers and to perform the following operations on it: Push b. Pop c. Display
The program should print appropriate messages for stack overflow and stack underflow.

#include<stdio.h>
int stack[10],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=10]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty"); }}



2. Write a recursive c programs for he following:
a. Searching an element on a given list of integers using the binary search method.
b. Solving the Towers of Hanoi Problem.

a.searching an element on a given list of integers using the binary search method

#include <stdio.h>
int bsearch(int num[], int left, int right, int key) {
int middle = 0;
while (left <= right) {
middle = left + (right -left) / 2;
// If the key is present in the middle of the array
if (num[middle] == key){
return middle;
}
// If the key is Smaller than the element in the middle check the left subarray.
if (num[middle] > key){
right = middle - 1;
}
// If the key is greater than the element in the middle, check the right subarray.
else{
left = middle + 1;
}
}
// If the element is not found
return -1;
}
int main() {
int size = 0, key = 0, found = 0;
printf("Enter the size of the array: ");
scanf("%d", & size);
int num[size];
printf("Enter the elements of the array in ascending order: ");
for (int i = 0; i < size; i++) {
scanf("%d", & num[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", & key);
found = bsearch(num, 0, size - 1, key);
if (found == -1) {
printf("Element is not present in the array");
}
else {
printf("Element found at index %d", found);
}
return 0;}

b.Solving the Towers of Hanoi Problem. 

#include <stdio.h>
 void towers(int, char, char, char);
 int main()
 {
 int num; 
printf("Enter the number of disks : ");
 scanf("%d", &num);
 printf("The sequence of moves involved in the Tower of Hanoi are :\n");
 towers(num, 'A', 'C', 'B');
 return 0;
 }
 void towers(int num, char frompeg, char topeg, char auxpeg) 
// Base Condition if no of disks are if (num == 1) 
{
 printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); 
return; 
}
 towers(num - 1, frompeg, auxpeg, topeg); 
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
 towers(num - 1, auxpeg, topeg, frompeg); 
}

3. Write a C program to convert and print a given valid parenthesized infix expression consists of single character operands and the binary operators + (plus),-(minus),* (multiply),/(divide) to suffix/Postfix expression

#include<stdio.h>

#include<conio.h>

int inputpre(char sym) //Function for input precedence

{

switch(sym)

{

case '+' :

case '-' : return 1;

case '*' :

case '/' : return 3;

case '^' :

case '$' : return 6;

case '(' : return 9;

case ')' : return 0;

default : return 7;

}

}

int stackpre(char sym) //Function for stack precedence

{

switch(sym)

{

case '+' :

case '-' : return 2; case '*' :

case '/' : return 4; case '^' :

case '$' : return 5; case '(' : return 0; case '#' : return -1; default : return 8;

}

}

void push (char item, int *top, char s[])

{

s[++(*top)] = item;

}

char pop(int *top, char s[])

{

return s[(*top)--];

}

void infix_to_postfix (char ifix[], char pfix[])

{

int top = -1, i, j = 0; char s[30] , sym;

push('#',&top,s);

for(i=0;i < strlen(ifix);i++)

{

sym = ifix[i];

while (stackpre(s[top]) > inputpre(sym)) pfix[j++] = pop(&top,s);

if(stackpre(s[top]) != inputpre(sym)) push(sym,&top,s);

else

pop(&top,s);

}

while(s[top] != '#')

pfix[j++] = pop(&top,s);

pfix[j] = '\0';

}

void main()

{

char ifix[20], pfix[20];

clrscr();

printf("Enter valid infix expression\n");

scanf("%s", ifix);

infix_to_postfix (ifix,pfix);

printf("The postfix expression is = %s", pfix);

}


Comments

Popular posts from this blog

final

Usp