Tuesday, 6 November 2012

C program: Selection sort

#include<stdio.h>
#include<conio.h>

void main()
{
 int ar[50],n,i=0,j,min,temp;

 clrscr();

 printf("Enter no of element : ");
 scanf("%d",&n);

 printf("\nEnter no : ");
 for(i=0;i<n;i++)
 {
  scanf("%d",&ar[i]);
 }

 for(i=0;i<n;i++)
 {
  min=i;
  for(j=i+1;j<n;j++)
  {
   if(ar[min]>ar[j])
   {
    min=j;
   }
  }
  temp=ar[i];
  ar[i]=ar[min];
  ar[min]=temp;
 }

 printf("After selection sort : ");
 for(i=0;i<n;i++)
 {
  printf("%d\n",ar[i]);
 }

 getch();
}

C program: BUBBLE Sort

#include<stdio.h>
#include<conio.h>

void bubble(int ar[],int n);

void main()
{
 int ar[50],n,i;

 clrscr();

 printf("Enter the no of element : ");
 scanf("%d",&n);

 printf("\nEnter the no : ");
 for(i=0;i<n;i++)
 {
  scanf("%d",&ar[i]);
 }

 bubble(ar,n);

 printf("\nAfter bubble sort : ");
 for(i=0;i<n;i++)
 {
  printf("%d\n",ar[i]);
 }

 getch();
}

void bubble(int ar[],int n)
{
 int j,i,temp;

 for(i=0;i<n;i++)
 {
  for(j=0;j<n-1;j++)
  {
   if(ar[j]>ar[j+1])
   {
    temp=ar[j];
    ar[j]=ar[j+1];
    ar[j+1]=temp;
   }
  }
 }
}

Saturday, 3 November 2012

C program: Lexical Analyzer

#include< conio.h>
#include< string.h>

void main()
{
int i,j,lc;
char *a[9][4]={"PRG","START" ," " ," ",
" " ,"USING" ,"*" ,"15",
" ","L","1","FIVE",
" ","A","1","FOUR",
" ","ST","1","TEMP",
"FOUR ","DC","F","4",
"FIVE","DC","F","5",
"TEMP","DS","1","F",
" " ,"END"," "," ", };
clrscr();
printf("\n \t\t LEXICAL ANALIZER \n");

for(i=0;i< 9;i++)
{ for(j=0;j< 4;j++)
{ if(isalpha(*a[i][j]))
printf("\n STRING : %s",a[i][j]);
if(isdigit(*a[i][j]))
printf("\n DIGIT : %s",a[i][j]);
}
printf("\n");
}

getch();
}

ATM program

/*Note Pin code is 1234*/

#include<stdio.h>

#include<conio.h>

void main(void)

{ unsigned long amount=1000,deposit,withdr​aw;

int choice,pin=0,k=0;

char another='y';

while(pin!=1234)

{ clrscr();

gotoxy(30,25);

printf("Enter pin:");

scanf("%d",&pin);

}

clrscr();

do

{

printf("********Welcome to ATM Service**************\n");

printf("1. Check Balance\n");

printf("2. Withdraw Cash\n");

printf("3. Deposit Cash\n");

printf("4. Quit\n");

printf("******************​**************************​*\n\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\nYour Balance is Rs : %lu ",amount);

break;

case 2:

printf("\nEnter the amount to withdraw: ");

scanf("%lu",&withdraw);

if(withdraw%100!=0)

{

printf("\nPlease enter

amount in multiples of 100");

}else if(withdraw>(amount-500))

{

printf("\nInsufficient Funds");

}else

{

amount=amount-withdraw;

printf("\n\nPlease collect cash");

printf("\nYour balance is %lu",amount);

}

break;

case 3:

printf("\nEnter amount to deposit");

scanf("%lu",&deposit);

amount=amount+deposit;

printf("Your balance is %lu",amount);

break;

case 4:

printf("\nThank you for using ATM");

break;

default:

printf("\nInvalid Choice");

}

printf("\n\n\nDo you want another transaction?(y/n): ");

fflush(stdin);

scanf("%c",&another);

if(another=='n'||another==​'N')

k=1;

}while(!k);

printf("\n\nHave a nice day");
getch()

de<stdio.h>

#include<conio.h>

void main(void)

{ unsigned long amount=1000,deposit,withdr​aw;

int choice,pin=0,k=0;

char another='y';

while(pin!=1234)

{ clrscr();

gotoxy(30,25);

printf("Enter pin:");

scanf("%d",&pin);

}

clrscr();

do

{

printf("********Welcome to ATM Service**************\n");

printf("1. Check Balance\n");

printf("2. Withdraw Cash\n");

printf("3. Deposit Cash\n");

printf("4. Quit\n");

printf("******************​**************************​*\n\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\nYour Balance is Rs : %lu ",amount);

break;

case 2:

printf("\nEnter the amount to withdraw: ");

scanf("%lu",&withdraw);

if(withdraw%100!=0)

{

printf("\nPlease enter

amount in multiples of 100");

}else if(withdraw>(amount-500))

{

printf("\nInsufficient Funds");

}else

{

amount=amount-withdraw;

printf("\n\nPlease collect cash");

printf("\nYour balance is %lu",amount);

}

break;

case 3:

printf("\nEnter amount to deposit");

scanf("%lu",&deposit);

amount=amount+deposit;

printf("Your balance is %lu",amount);

break;

case 4:

printf("\nThank you for using ATM");

break;

default:

printf("\nInvalid Choice");

}

printf("\n\n\nDo you want another transaction?(y/n): ");

fflush(stdin);

scanf("%c",&another);

if(another=='n'||another==​'N')

k=1;

}while(!k);

printf("\n\nHave a nice day");

getch()

}

C program: Convert ROMAN numerals into Decimal

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{

 int *a,len,i,j,k;
 char *rom;
 clrscr();
 printf("Enter the Roman Numeral:");
 scanf("%s",rom);
 len=strlen(rom);
 for(i=0;i<len;i++)
 {
  if(rom[i]=='I')
  a[i]=1;
  else if(rom[i]=='V')
  a[i]=5;
  else if(rom[i]=='X')
  a[i]=10;
  else if(rom[i]=='L')
  a[i]=50;
  else if(rom[i]=='C')
  a[i]=100;
  else if(rom[i]=='D')
  a[i]=500;
  else if(rom[i]=='M')
  a[i]=1000;
  else
  {
    printf("\nInvalid Value");
    getch();
    exit(0);
  }
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
  if(a[i]>a[i-1])
  k=k-a[i-1];
  else if(a[i]==a[i-1] || a[i]<a[i-1])
  k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}

Two Pass Assembler

#include< stdio.h>
#include< string.h>
#include< conio.h>


void main()
{
char *code[9][4]={
{"PRG1","START","",""},
{"","USING","*","15"},
{"","L","",""},
{"","A","",""},
{"","ST","",""},
{"FOUR","DC","F",""},
{"FIVE","DC","F",""},
{"TEMP","DS","1F",""},
{"","END","",""}
};
char av[2],avail[15]={'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N'};
int i,j,k,count[3],lc[9]={0,0,0,0,0,0,0,0,0},loc=0;
clrscr();
printf("----------------------------------------------------\n");
printf("LABEL\t\tOPCODE\n");
printf("----------------------------------------------------\n\n");
for(i=0;i< =8;i++)
{
for(j=0;j< =3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
getch();
printf("-----------------------------------------------------");
printf("\nVALUES FOR LC : \n\n");
for(j=0;j< =8;j++)
{
if((strcmp(code[j][1],"START")!=0)&&(strcmp(code[j][1],"USING")!=0)&&(strcmp(code[j][1],"L")!=0))
lc[j]=lc[j-1]+4;
printf("%d\t",lc[j]);
}
printf("\n\nSYMBOL TABLE:\n----------------------------------------------------\n");
printf("SYMBOL\t\tVALUE\t\tLENGTH\t\tR/A");
printf("\n----------------------------------------------------\n");

for(i=0;i< 9;i++)
{
if(strcmp(code[i][1],"START")==0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
}
else if(strcmp(code[i][0],"")!=0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
loc=4+loc;
}
else if(strcmp(code[i][1],"USING")==0){}
else
{loc=4+loc;}
}
printf("----------------------------------------------------");

printf("\n\nBASE TABLE:\n-------------------------------------------------------\n");
printf("REG NO\t\tAVAILIBILITY\tCONTENTS OF BASE TABLE");
printf("\n-------------------------------------------------------\n");
for(j=0;j< =8;j++)
{
if(strcmp(code[j][1],"USING")!=0)
{}
else
{
strcpy(av,code[j][3]);
}
}
count[0]=(int)av[0]-48;
count[1]=(int)av[1]-48;
count[2]=count[0]*10+count[1];
avail[count[2]-1]='Y';

for(k=0;k< 16;k++)
{
printf(" %d\t\t %c\n",k,avail[k-1]);
}

printf("-------------------------------------------------------\n");
printf("Continue..??");
getchar();
printf("PASS2 TABLE:\n\n");
printf("LABEL\t\tOP1\t\tLC\t\t");
printf("\n----------------------------------------------------\n");
loc=0;
for(i=0;i< =8;i++)
{
for(j=0;j< =3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
printf("-----------------------------------------------------");

getch();

}

Sunday, 28 October 2012

Job Scheduling in C

#include<stdio.h>
#include<conio.h> 
#include<process.h> 
void main() 
  char p[10][5],temp[5]; 
  int tot=0,wt[10],pt[10],i,j,n,temp1; 
  float avg=0; 
  
  clrscr(); 
  
  printf("enter no of processes:"); 
  scanf("%d",&n); 
  
  for(i=0;i<n;i++) 
 {
   printf("enter process%d name:\n",i+1);
   scanf("%s",&p[i]);
   printf("enter process time");
   scanf("%d",&pt[i]); 
}
 for(i=0;i<n-1;i++)
 { 
    for(j=i+1;j<n;j++)
   {
      if(pt[i]>pt[j]) 
     {
        temp1=pt[i];
        pt[i]=pt[j]; 
        pt[j]=temp1; 
        strcpy(temp,p[i]);
        strcpy(p[i],p[j]); 
        strcpy(p[j],temp); 
     } 
   }
 } 
 wt[0]=0; 
 for(i=1;i<n;i++) 
 {
   wt[i]=wt[i-1]+et[i-1]; tot=tot+wt[i];
 } 
 avg=(float)tot/n; 
 printf("p_name\t P_time\t w_time\n"); 
 for(i=0;i<n;i++) 
   printf("%s\t%d\t%d\n",p[i],et[i],wt[i]); 
 printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
 getch();
}