Skip to main content

Entering elements into the stack from user and transfering into two stack with equal height.

Solution:
#include<stdio.h>
#include<conio.h>
#define maxsize 50
struct stack
{
 int stk[maxsize];
 int top;
};
typedef struct stack STACK;
STACK s1,s2;
int i,n;
int mid;
void array();
void push1();
void push2();
void traverse1();
void traverse2();
void main()
{
 s1.top=-1;
 s2.top=-1;
 clrscr();

     array();
     push1();
     push2();
     traverse1();
     traverse2();

}
void array()
{
int arr[50];
  printf("\n enter the no of array ele to be stored ::");
  scanf("%d",&n);
  mid=n/2;
  printf("\n enter the array elements ::\n");
 for(i=0;i<n;i++)
 {
    printf("\n enter arr[%d] ::\t",i);
    scanf("%d",&arr[i]);
 }

}
void push1()
{
   int arr[50];
   if(s1.top== (maxsize-1))
   {
    printf("\n stack is full ");
   }
   else
   {
    for(i=0;i<mid;i++)
   {
       s1.top=s1.top+1;
       s1.stk[s1.top]=arr[i];
   }
   }
}
void push2()
{  int arr[50];
   if(s2.top== (maxsize-1))
   {
    printf("\n stack is full ");
   }
   else
   {
    for(i=mid;i<n;i++)
    {
       s2.top=s2.top+1;
       s2.stk[s2.top] =arr[i];
    }
   }
}

void traverse1()
{
 int i;
 if(s1.top==-1)
  {
   printf("\n nothing to display , stack is empty ::");
  }
 else
  {
   printf("\n");
   printf("\n stack 1 :");
   for(i=s1.top;i>=0;i--)
    {
    printf("\n %d",s1.stk[i]);
    }
  }
}
void traverse2()
{
  if(s2.top==-1)
  {
   printf("\n nothing to display , stack is empty ::");
  }
 else
  {
   printf("\n");
   printf("\n stack 2 :");
   for(i=s2.top;i>=0;i--)
    {
    printf("\n %d",s2.stk[i]);
    }
  }

}

Comments

Popular posts from this blog

2D transformation for reflection in C program (Computer Graphics).

Program: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void refx(int x1,int x2,int x3,int y1,int y2,int y3){ line(320,0,320,430); line(0,240,640,240); x1=(320-x1)+320; x2=(320-x2)+320; x3=(320-x3)+320; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void refy(int x1,int x2,int x3,int y1,int y2,int y3){ line(320,0,320,430); line(0,240,640,240); y1=(240-y1)+240; y2=(240-y2)+240; y3=(240-y3)+240; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void main() { int gd=DETECT,gm; int x1,y1,x2,y2,x3,y3; clrscr(); initgraph(&gd,&gm,"c://turboc3//bgi"); line(320,0,320,430); line(0,240,640,240); x1=150;y1=100; x2=220;y2=220; x3=220;y3=110; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); refx(x1,x2,x3,y1,y2,y3); getch(); refy(x1,x2,x3,y1,y2,y3); getch(); closegraph(); }

2D transformation for Rotation in C programming (Computer Graphics).

Program: #include <math.h> #include<stdio.h> #include <conio.h> #include <graphics.h> void main() { int gd=DETECT,gm,x1,x2,y1,y2,x4,y4; float angle=0.0,ang; initgraph(&gd,&gm,"C:\\turboc3\\BGI"); printf("Enter Co-ordinates:"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); line(x1,y1,x2,y2); printf("Enter the angle:"); scanf("%f",&ang); angle=(ang*3.14)/180; x4=x2-(((x2-x1)*cos(angle))-((y2-y1)*sin(angle))); y4=y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle))); line(x2,y2,x4,y4); getch(); closegraph(); }

Write a C program for binary search in sorted array.

Solution: #include <stdio.h> int main() {    int c, first, last, middle, n, search, array[100];    printf("Enter number of elements\n");    scanf("%d",&n);    printf("Enter %d integers\n", n);    for (c = 0; c < n; c++)    {       scanf("%d",&array[c]);    }    printf("Enter value to find\n");    scanf("%d", &search);    first = 0;    last = n - 1;    middle = (first+last)/2;    while (first <= last)    {       if (array[middle] < search)       { first = middle + 1;       }       else if (array[middle] == search)       { printf("%d found at location %d.\n", search, middle); break;       }       else last = middle - 1; //       printf("%d\n",middle);       middle = (first + last)/2;   //     printf("%d\n",middle);    }    if (first > last)       printf("Not found! %d is not present in the list.\n", search);