Programs
Programs
#include<stdio.h>
int i,pos,j,temp;
for (i=0;i<n-1;i++)
{
pos = i;
for(j=i+1;j<n;j++)
if(a[j]<a[pos])
pos =j;
if (i!=pos)
temp =a[i];
a[i]=a[pos];
a[pos]=temp;
}
int main()
int a[100],n,i;
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
printf("after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
Program 2:
#include<stdio.h>
#include<stdlib.h>
int *a;
int max,top=-1;
if(top==max-1)
max=2*max;
a=realloc(a,max*sizeof(int));
}
a[++top]=ele;
int pop()
if(top==-1)
{
printf("stack underflow");
return -999;
else
return(a[top--]);
}
void display()
int i;
if(top==-1)
printf("stack is empty");
else
for(i=top;i>=0;i--)
printf("%d\t",a[i]);
int main()
int choice,ele;
printf("enter the value of max");
scanf("%d",&max);
a=(int *)malloc(max*sizeof(int));
while(1)
{
printf("\nenter your choice\n ");
printf("1 for push\n 2 for pop\n 3 for display\n 4 for exit\n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&ele);
push(ele);
break;
case 2: ele=pop();
if(ele!=-999)
break;
case 3: display();
break;
case 4: free(a);
exit(0);
default:printf("invalid choice");
Program 3:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char istack[20];
int tos=-1;
float stack[20];
int top=-1;
void ipush(char s)
istack[++tos]=s;
char ipop()
{
return istack[tos--];
int precd(char s)
switch(s)
case '*':
case '%':
case '+':
case '(':
case ')':
case '#':return 1;
}
return 0;
{
int i,j=0;
char symbol;
ipush('#');
for(i=0;i<strlen(infix);i++)
symbol=infix[i];
switch(symbol)
postfix[j++]=ipop();
ipop();//pop ( bracket
break;
case '^':
case '*':
case '/':
case '%':
case '+':
postfix[j++]=ipop();
ipush(symbol);
break;
default:postfix[j++]=symbol;
}
while(istack[tos]!='#')
postfix[j++]=ipop();
postfix[j]='\0';
}
void push(float ele)
stack[++top]=ele;
float pop()
return(stack[top--]);
}
void evaluate(char postfix[50])
int i;
char sym;
float op1,op2,result,x;
for(i=0;i<strlen(postfix);i++)
sym=postfix[i];
if(isalpha(sym))
scanf("%f",&x);
push(x);
else
{
op2=pop();
op1=pop();
switch(sym)
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '%':push((int)op1%(int)op2);
break;
case '^':push(pow(op1,op2));
break;
exit(0);
result=pop();
int main()
char infix[20],postfix[20];
convertip(infix,postfix);
evaluate(postfix);
return 0;
}