C program for Binary search operations

C program for Binary search operations


#define MAX_LEN 10

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}

/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}

void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i scanf("%d",&l[i]);
}

void print_list(int l[],int n)
{
int i;
for(i=0;i printf("%d\t",l[i]);
}

/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;

clrscr();
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);


switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
getch();
break;

case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}


C Program to print Pascal Traingle

C Program to print Pascal Traingle


void main()
{
int num,x,y,a[50][50];
clrscr();
fflush(stdin);

printf("Enter the number of rows: ");
scanf("%d",&num);

printf("\n\t\t Pascal's Triangle of Order %d\n\n",num);

for(x=0;x{
for(y=0;y<=x;y++)
{
if(x==y || y==0)
a[x][y]=1;
else
a[x][y]=a[x-1][y-1]+a[x-1][y];
printf("%4d",a[x][y]);
}
printf("\n\n");
}

getch();
}



VHDL multiplexer using case statements

VHDL multiplexer using case statements


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity multiplexer4_1 is
port (
      i0 : in std_logic;
      i1 : in std_logic;
      i2 : in std_logic;
      i3 : in std_logic;
     sel : in std_logic_vector(1 downto 0);
     bitout : out std_logic
     );
end multiplexer4_1;

architecture Behavioral of multiplexer4_1 is
begin

process(i0,i1,i2,i3,sel)
begin
case sel is
  when "00" => bitout <= i0;
  when "01" => bitout <= i1;
  when "10" => bitout <= i2;
  when others => bitout <= i3;
end case;
end process;

end Behavioral;

The testbench code used for testing the code is given below:

  LIBRARY ieee;
  USE ieee.std_logic_1164.ALL;

  ENTITY testbench IS
  END testbench;

  ARCHITECTURE behavior OF testbench IS
          SIGNAL i0,i1,i2,i3,bitout :  std_logic:='0';
          SIGNAL sel :  std_logic_vector(1 downto 0):="00";
  BEGIN
    UUT : entity work.multiplexer4_1 port map(i0,i1,i2,i3,sel,bitout);

     tb : PROCESS
     BEGIN
            i0<='1';
            i1<='0';
            i2<='1';
            i3<='0';
            sel <="00";
            wait for 2 ns;
            sel <="01";
            wait for 2 ns;
            sel <="10";
             wait for 2 ns;
             sel <="11";
              wait for 2 ns;
            --more input combinations can be given here.
     END PROCESS tb;

  END;


Railway Reservation System Project In C

Railway Reservation System Project In C


#include conio.h
#include stdio.h
#include graphics.h
#include stdlib.h
#include dos.h
#include string.h
#include math.h

/***************Global Variable Declarations****************/

union REGS i,o;
int xmax,ymax;
int gdriver = DETECT, gmode, errorcode,x,y;
int count=0,no_pass=0;
FILE *ptr1;
char d[8][3]={{"S1"},{"S2"},{"S3"},{"S4"},{"S5"},{"S6"},{"S7"},{"S8"}};
char x1[2];

/****************************Global Structures declared**********/

struct travel
{
char tno[5];
//char doj[10];
int d1,m1,y1;
char from[4];
char to[4];
char passen[6][20];
char sex[6][2];
char age[6][3];
int seat_no[6];
char coano[3];
   // float pnr;
long int pnr;
float bill;
}passenger;
struct
{
// float pnr;
long int pnr;
}pass;
struct
{
int seat_no;
char coano[3];
char status[1];
}berth;

struct
{
char tno[5];
char tname[20];
}train;


/***********************User Defined Functions***********/

void firstscreen();
void mainmenu();
void reserv();
void clear();
void setgraph();
void clearline();
void drawbox();
void cancel();
void enquiry();
void color(int,int,int);
void printtic(struct travel);

/*****************Main Function*****************************/
void main()
{
int i;
FILE *ptr,*tr;
/***********loop for writing train nos***************/
if((tr=fopen("tra.dat","r"))==NULL)
{
tr=fopen("tra.dat","w");
strcpy(train.tno,"3143");
strcpy(train.tname,"Darjeeling Mail");
fwrite(&train,sizeof(train),1,tr);
strcpy(train.tno,"3147");
strcpy(train.tname,"Uttarbanga Exp");
fwrite(&train,sizeof(train),1,tr);
fclose(tr);
}

/************loop for writing train berth nos*********/
if((ptr=fopen("berths3.dat","r"))==NULL)
{
   ptr=fopen("berths3.dat","wb");
   strcpy(berth.coano,d[0]);
   strcpy(berth.status,"V");
   for(i=1;i<=73;i++)
   {
  if(i>72)
  {
count++;
strcpy(berth.coano,d[count]);
i=1;
  }
  if(count==8)
  break;
  berth.seat_no=i;
  fwrite(&berth,sizeof(berth),1,ptr);

   }
}
fclose(ptr);
if((ptr=fopen("berths7.dat","r"))==NULL)
{
   ptr=fopen("berths7.dat","wb");
   strcpy(berth.coano,d[0]);
   strcpy(berth.status,"V");
   for(i=1;i<=73;i++)
   {
  if(i>72)
  {
count++;
strcpy(berth.coano,d[count]);
i=1;
  }
  if(count==8)
  break;
  berth.seat_no=i;
  fwrite(&berth,sizeof(berth),1,ptr);

   }
     }
     fclose(ptr);
textcolor(WHITE);
textbackground(BLACK);
clrscr();
firstscreen();
mainmenu();
reserv();
cancel();

getch();
}
void firstscreen()
{
int r,c;
for(r=4;r<=20;r++)
{
gotoxy(20,r);
printf("*");
gotoxy(60,r);
printf("*");
}
for(c=20;c<60;c++)
{
gotoxy(c,4);
printf("*");
gotoxy(c,20);
printf("*");
}

gotoxy(36,6);
textattr(LIGHTRED);
cprintf("PROJECT");

gotoxy(39,8);
textcolor(BLUE);
cprintf("ON");

gotoxy(32,10);
textcolor(BROWN);
cprintf("Railway Reservation");

gotoxy(25,13);
textcolor(YELLOW);
putch(1);

textcolor(CYAN);
cprintf(" DEVELOPERS : ");


gotoxy(25,17);
textcolor(YELLOW);
putch(2);

textcolor(CYAN);
cprintf("RRP");
textcolor(CYAN);
cprintf("popsys");
gotoxy(28,23); textcolor(MAGENTA + BLINK);
cprintf("Press a key to continue");
getch();
}

void mainmenu()
{
char choice;
setgraph();
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(xmax,45,"Railway Reservation");
/* draw a rectangle */
rectangle(100,150,500,350);
gotoxy(17,12);
printf("1. For reservation press  1
");
gotoxy(17,14);
printf("2. For cancellation press 2
");
gotoxy(17,16);
printf("3. For enquiry press 3
");
gotoxy(17,18);
printf("4. For closing the program press 4
");
gotoxy(48,25);
printf("Created by SOFTECH SOLUTIONS Ltd.");
gotoxy(20,20);
printf("Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
reserv();
break;
case 2:
closegraph();
clrscr();
cancel();
break;
case 3:
closegraph();
enquiry();
break;
case 4:
exit(1);
default:
gotoxy(29,20);
printf("Not a valid choice");
getch();
main();
}
}
int pass_name(int i,int x, int y)
{
int flag,pos;
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.passen[i]);
fflush(stdin);
strupr(passenger.passen[i]);
if (strlen(passenger.passen[i]) == 0)
{
if(i>0)
{
return(7);
}
gotoxy(10,24);
printf("aPassenger Name should not be left BLANK");
flag=0;
//i--;
getch();
clearline(24);
}
else if (strlen(passenger.passen[i]) >20)
{
//gotoxy(10,20);
gotoxy(10,24);
printf("aPassenger Name must be less than 20 characters");
flag=0;
getch();
clearline(24);
}
}while(flag==0);
gotoxy(x,y);
puts(passenger.passen[i]);
i++;
return(i);
}
int pass_sex(int j,int x,int y)
{
int flag,i;
char ch[1][1];
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.sex[j]);
fflush(stdin);
strupr(passenger.sex[j]);
if (strlen(passenger.sex[j]) == 0)
{
gotoxy(10,24);
printf("aSex should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if(strlen(passenger.sex[j])!=1)
{
gotoxy(10,24);
printf("aPassenger's Sex must be 1 characters long");
flag=0;
getch();
clearline(24);
}
}while (flag==0);
gotoxy(x,y);
puts(passenger.sex[j]);
j++;
return(j);
}

int pass_age(int k,int x,int y)
{
int flag,i,j;
do
{
flag=1;
gotoxy(x,y);
if(flag==1)
no_pass++;
gets(passenger.age[k]);
fflush(stdin);
strupr(passenger.age[k]);
if (strlen(passenger.age[k]) == 0)
{
gotoxy(10,24);
printf("aPassenger's Age should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.age[k])!=2)
{
gotoxy(10,24);
printf("aPassenger's Age must be equal to 2 characters");
flag=0;
// k--;
getch();
clearline(24);
}
else if(strcmp(passenger.age[k],"00")==0)
{
gotoxy(10,24);
printf("aPassenger's Age cannot be less than 1 year");
flag=0;
getch();
clearline(24);
}
for(i=k;i
{
for(j=0;j<2;j++)
{
if(!isdigit(passenger.age[i][j]))
{
gotoxy(10,24);
printf("aEnter a valid age");
flag=0;
getch();
clearline(24);
}
}
}
}while (flag==0);
gotoxy(x,y);
k++;
puts(passenger.age[k]);
return(k);
}

showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
int restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);

i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}

void clear()
{
int i,j;
for(i=0;i<=80;i++)
{
for(j=0;j<=25;j++)
{
printf("%c",' ');
gotoxy(i,j);
}
}
gotoxy(1,1);
}
void setgraph()
{

clrscr();

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\tc\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
  printf("Graphics error: %s
", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  getch();
  exit(1);
}

xmax = getmaxx()/2;
ymax = getmaxy()/2;
}
void clearline(int yco)
{
int i,j;
gotoxy(5,yco);
for(i=0;i<70;i++)
{
printf("%c",' ');
}
}
void drawbox()
{
int xr,xc;
int la,ra;
gotoxy(0,0);
for(xr=1;xr<=80;xr++)
printf("Ä");
xc=2;
for(la=1;la<=23;la++)
{
gotoxy(1,xc);
printf("³");
gotoxy(80,xc);
printf("³");
xc++;
}
gotoxy(0,25);
for(xr=1;xr<80;xr++)
printf("Ä");
gotoxy(1,1);
printf("Ú");
gotoxy(80,24);
printf("³");
}


void reserv()
{
int i,flag,j,k,test,found,set_no,count1=0,year;
struct date d;
float pnrno;
char tno[3],ans;
char a[1],c,age[3],ch;
char *p;
FILE *ptr,*pno,*tr;
closegraph();
highvideo();
//Design Part
clear();
drawbox();
gotoxy(3,2);
for(i=0;i<74;i++)
printf("Ä");

j=3;
for(i=0;i<4;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,6);
for(i=0;i<75;i++)
printf("Ä");
gotoxy(6,2);
printf(" Journey Details ");
color(15,18,3);
gotoxy(5,3);
printf("Train No : ");
color(32,46,3);
gotoxy(20,3);
printf("Train Name :");
color(67,68,3);
color(71,72,3);
color(75,76,3);
gotoxy(49,3);
printf("Date of Booking :");
gotoxy(67,3);
getdate(&d);
printf("%d",d.da_day);
gotoxy(69,3);
putch('-');
gotoxy(71,3);
printf("%d",d.da_mon);
putch(' -');
printf("%d",d.da_year);
color(12,14,5);
gotoxy(5,5);
printf("From :");
color(27,29,5);
gotoxy(20,5);
printf("To :");
color(67,68,5);
color(71,72,5);
color(75,76,5);
gotoxy(49,5);
printf("Date Of Journey :");
gotoxy(3,8);
for(i=0;i<=74;i++)
printf("Ä");
gotoxy(7,8);
printf("Passenger Details");
j=9;
for(i=0;i<14;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,23);
printf(" ");
gotoxy(3,23);
for(i=0;i<74;i++)
printf("Ä");
gotoxy(4,10);
for(i=0;i<73;i++)
printf("Ä");
color(8,31,11);
gotoxy(22,9);
printf("NAME");

gotoxy(46,9);
printf("SEX");
gotoxy(53,9);
printf("AGE");
gotoxy(65,9);
printf("REMARKS");
j=9;
for(i=0;i<14;i++)
{
gotoxy(44,j);
printf("³");

gotoxy(51,j);
printf("³");

gotoxy(58,j);
printf("³");
j++;
}

for(i=0;i<6;i++)
{
color(5,38,(11+i));
color(46,48,(11+i));
color(53,56,(11+i));
color(5,38,(16+i));
color(46,48,(16+i));
color(53,56,(16+i));
}

//User input part
gotoxy(15,3);
do
{
int len;
fflush(stdin);
//name input
gets(passenger.tno);
if(len=(strlen(passenger.tno))!=4)
{
gotoxy(10,24);
printf("Train No Must be 4 digits long");
getch();
clearline(24); //clears the line
gotoxy(15,3);
}
tr=fopen("tra.dat","r");
fread(&train,sizeof(train),1,tr);
while(!feof(tr))
{
if((strcmp(passenger.tno,train.tno))==0)
{
found=0;
flag=0;
break;
}
else
found=2;
fread(&train,sizeof(train),1,tr);
}
if(found==2)
{
gotoxy(8,24);
printf("This Train No does not exist");
getch();
clearline(24);
gotoxy(15,3);
flag=1;
}
}while(strlen(passenger.tno)!=4||flag==1);
if(found==0)
{
gotoxy(32,3);
puts(train.tname);
found=1;
}

fflush(stdin);
do
{
flag=1;
gotoxy(12,5);
fflush(stdin);
scanf("%[^
]",passenger.from);
fflush(stdin);
strupr(passenger.from);
if (strlen(passenger.from) == 0)
{
gotoxy(10,24);
printf("aFrom field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.from) != 3)
{
gotoxy(10,24);
printf("aFrom should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.from[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Boarding Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(12,5);
puts(passenger.from);

do
{
flag=1;
gotoxy(27,5);
fflush(stdin);
scanf("%[^
]",passenger.to);
fflush(stdin);
strupr(passenger.to);
if (strlen(passenger.to) == 0)
{
gotoxy(10,24);
printf("aTo field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.to) != 3)
{
gotoxy(10,24);
printf("aTo should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.to[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Destination Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(27,5);
puts(passenger.to);
i=j=k=0;

gotoxy(67,5);
while(c!=13)
{
   fflush(stdin);
   c=getch();
   if((c<'0'||c>'9')&&c!=13&&c!=8)
   {
if(c==27)
 return;
gotoxy(5,24);
printf("Please Enter Valid Date.");
getch();
gotoxy(67,5);
printf("  ");
gotoxy(5,24);
clearline(24);
gotoxy(67,5);
count1=0;
c='a';
   }
   else if(c==8)
   {
  if(wherex()>69)
  {
 gotoxy(wherex()-1,15);
 putch(' ');
 count1--;
 gotoxy(wherex()-1,15);
  }
   }
   else if(c!=13)
   {
  if(wherex()<69)
  {
 putch(c);
 age[count1]=c;
 count1++;
  }
  else
  {
 gotoxy(5,24);
 printf("Date Can Be Only 2 Digits.");
 getch();
 clearline(24);
 gotoxy(67,5);
  }
   }
else
{
age[count1]='