Thursday 23 April 2015

bresenham algorithm

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

  void main()
  {
    int dx,dy,x,y,p,x1,y1,x2,y2;
    int gd,gm;

    clrscr();

    printf("\n\n\tEnter the co-ordinates of first point : ");
    scanf("%d %d",&x1,&y1);
    printf("\n\n\tEnter the co-ordinates of second point : ");
    scanf("%d %d",&x2,&y2);

    dx = (x2 - x1);
    dy = (y2 - y1);

    p = 2 * (dy) - (dx);

    x = x1;
    y = y1;

    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"e:\\tc\\bgi");
    putpixel(x,y,WHITE);

    while(x <= x2)
    {
      if(p < 0)
      {
        x=x+1;
        y=y;
        p = p + 2 * (dy);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dy - dx);
     }
     putpixel(x,y,WHITE);
   }
   getch();
   closegraph();
}

c program for dda algorithm :



#include <graphics.h>
#include <stdio.h>
#include <math.h>

int main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,pixel;
    int i,gd,gm;

    printf("Enter the value of x1 : ");
    scanf("%f",&x1);
    printf("Enter the value of y1 : ");
    scanf("%f",&y1);
    printf("Enter the value of x2 : ");
    scanf("%f",&x2);
    printf("Enter the value of y1 : ");
    scanf("%f",&y2);

    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"");

    dx=abs(x2-x1);
    dy=abs(y2-y1);

    if(dx>=dy)
    pixel=dx;
    else
    pixel=dy;

    dx=dx/pixel;
    dy=dy/pixel;

    x=x1;
    y=y1;

    i=1;
    while(i<=pixel)
    {
          putpixel(x,y,1);
          x=x+dx;
          y=y+dy;
          i=i+1;
          delay(100);
    }
    getch();
    closegraph();
}

SJF Scheduling

/* SJF Scheduling */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int P[10],BT[10],AT[10],WT[10],TA[10],s[10],e[10];
int i,j,N,temp1,temp2;
clrscr();
printf("Enter the no. of process=");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("\n\nEnter the pname & burst time=\n");
scanf("%d%d",&P[i],&BT[i]);
}
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(BT[i]>BT[j])
{
temp1=BT[i];
BT[i]=BT[j];
BT[j]=temp1;
temp2=P[i];
P[i]=P[j];
P[j]=temp2;
}
}
}
printf("\n\nThe process table=\n\nPname\tBT\tWT\tTA\n");
printf("______________________________________________");
s[0]=0;
e[0]=s[0]+BT[0];
WT[0]=0;
TA[0]=BT[0];
printf("\n%d\t%d\t%d\t%d",P[0],BT[0],WT[0],TA[0]);
for(i=1;i<N;i++)
{
e[i]=BT[i]+e[i-1];
WT[i]=e[i-1];
TA[i]=WT[i]+BT[i];
printf("\n%d\t%d\t%d\t%d",P[i],BT[i],WT[i],TA[i]);
}
getch();
}







pattern1.

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

#include<stdio.h>
#include<conio.h>
void main()
{
      int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d",i);
.
}
printf("\n");
}
getch();
}

Banker's Algorithm

/* Banker's Algorithm */

#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10],need[10][10],alloc[10][10],avail[10],completed[10],
safeSequence[10];
    int p, r, i, j, process, count;
    count = 0;
printf("Enter the no of processes : ");
    scanf("%d", &p);
for(i = 0; i< p; i++)
        completed[i] = 0;
printf("\n\nEnter the no of resources : ");
    scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
    for(i = 0; i < p; i++)
    {
        printf("\nFor process %d : ", i + 1);
        for(j = 0; j < r; j++)
            scanf("%d", &Max[i][j]);
    }
printf("\n\nEnter the allocation for each process : ");
    for(i = 0; i < p; i++)
    {
        printf("\nFor process %d : ",i + 1);
        for(j = 0; j < r; j++)
            scanf("%d", &alloc[i][j]);
    }
printf("\n\nEnter the Available Resources : ");
    for(i = 0; i < r; i++)
        scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
            need[i][j] = Max[i][j] - alloc[i][j];
do
        {
            printf("\n Max matrix:\tAllocation matrix:\n");

            for(i = 0; i < p; i++)
            {
                for( j = 0; j < r; j++)
                    printf("%d ", Max[i][j]);
                printf("\t\t");
                for( j = 0; j < r; j++)
                    printf("%d ", alloc[i][j]);
                printf("\n");
            }
process = -1;
for(i = 0; i < p; i++)
            {
                if(completed[i] == 0)//if not completed
                {
                    process = i ;
                    for(j = 0; j < r; j++)
                    {
                        if(avail[j] < need[i][j])
                        {
                            process = -1;
                            break;
                        }
                    }
                }
                if(process != -1)
                    break;
            }
if(process != -1)
            {
                printf("\nProcess %d runs to completion!", process + 1);
                safeSequence[count] = process + 1;
                count++;
                for(j = 0; j < r; j++)
                {
                    avail[j] += alloc[process][j];
                    alloc[process][j] = 0;
                    Max[process][j] = 0;
                    completed[process] = 1;
                }
            }
        }
        while(count != p && process != -1);
if(count == p)
        {
            printf("\nThe system is in a safe state!!\n");
            printf("Safe Sequence : < ");
            for( i = 0; i < p; i++)
                printf("%d ", safeSequence[i]);
            printf(">\n");
        }
        else
            printf("\nThe system is in an unsafe state!!");
            getch();
}

FCFS Scheduling

/* FCFS Scheduling */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int BT[10], AT[10], WT[10], TA[10],s[10],e[10];
int pname[10],temp3;
int N,i,temp1,temp2,j,n;
//clrscr();
printf("Enter no. of processes to be entered=");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("\n\nEnter Process name, burst & arrival time=\n");
scanf("%d",&pname[i]);
scanf("%d",&BT[i]);
scanf("%d",&AT[i]);
}
printf("\n\nThe process table=\nPName\tBT\tAT\tWT\tTA\n");
printf("________________________________________________");
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(AT[i]>AT[j])
{
temp1=AT[i];
AT[i]=AT[j];
AT[j]=temp1;
temp2=BT[i];
BT[i]=BT[j];
BT[j]=temp2;
temp3=pname[i];
pname[i]=pname[j];
pname[j]=temp3;
}
}
}
s[0]=0;
if(AT[0]==0)
{
s[0]=AT[0];
e[0]=AT[0]+BT[0];
WT[0]=s[0]-AT[0];
TA[0]=WT[0]+BT[0];
printf("\n%d\t%d\t%d\t%d\t%d\n",pname[0],BT[0],AT[0],WT[0],TA[0]);
}
for(i=1;i<N;i++)
{
if(AT[i]<e[i-1])
{
s[i]=e[i-1];
e[i]=BT[i]+e[i-1];
WT[i]=s[i]-AT[i];
TA[i]=WT[i]+BT[i];
}
else if(AT[i]>=e[i-1])
{
s[i]=AT[i];
e[i]=s[i]+BT[i];
WT[i]=s[i]-AT[i];
TA[i]=WT[i]+BT[i];
}
else
{
break;
}
}
for(i=1;i<N;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d\n",pname[i],BT[i],AT[i],WT[i],TA[i]);
}
getch();
}

Priority Scheduling program

/* Priority Scheduling */

#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int P[10],BT[10],AT[10],Pri[10],WT[10],TA[10],s[10],e[10];
int i,j,N,temp1,temp2,temp3;
clrscr();
printf("Enter the no.of processes=");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("\n\nEnter the pname, burst time & priority=\n");
printf("P");
scanf("%d%d%d",&P[i],&BT[i],&Pri[i]);
}
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(Pri[i]>Pri[j])
{
temp2=Pri[i];
Pri[i]=Pri[j];
Pri[j]=temp2;
temp1=BT[i];
BT[i]=BT[j];
BT[j]=temp1;
temp3=P[i];
P[i]=P[j];
P[j]=temp3;
}
}
}
printf("\n\nThe process table=\n\nPname\tBT\tPriority\tWT\tTA\n");
printf("___________________________________________________________\n");
s[0]=0;
e[0]=BT[0];
WT[0]=0;
TA[0]=BT[0];
printf("\nP%d\t%d\t%d\t%d\t%d",1,BT[0],Pri[0],WT[0],TA[0]);
for(i=1;i<N;i++)
{
e[i]=e[i-1]+BT[i];
WT[i]=e[i-1];
TA[i]=WT[i]+BT[i];
printf("\nP%d\t%d\t%d\t%d\t%d",P[i],BT[i],Pri[i],WT[i],TA[i]);
}
getch();
}

Thursday 16 April 2015

magic square

#include<stdio.h>
#include<conio.h>
#define row 3
#define col 3
void main()
{
int ms[row][col]={0},n=1,cr,cc,pr,pc;
cr=0,cc=col/2;
ms[cr][cc]=n++;
{
pr=cr;
cr--;
if(cr<0)
{

cr=row-1;
pc=cc;
cc++;
}
if(cr>2)
{
cc=0;
}
if(ms[cr][cc]!=0)
{
cr=pr=1;
cc=pc;
}
ms[cr][cc]=n++;

}
clrscr();
printf("\n \n magic square:\n");
for(cr=0;cc<row;cr++)
{
for(cc=0;cc<col;cc++)
{
printf("\n %3d",ms[cr][cc]);
}
printf("\n\n");
}
getch();
}

to enter char in array

#include<stdio.h>
#include<conio.h>
void main()
{
char arr[10];
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("enter a char");
fflush(stdin);//plz try with out fflush u will see system accept enter key as char,
// and only 5 char rch as result//
scanf("%c",&arr[i]); //try this arr[i]=getch() ; instead of scanf o
}
printf("\n given char");
for(i=0;i<10;i++)
{
printf("\n %c",arr[i]);
}
getch();
}

to find letter in array

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char ch;
char arr[]={'a','b','c','e','p','t','w','l'};
clrscr();
printf("\n char array  ");
for(i=0;i<10;i++)
{
printf("%c",arr[i]);
}
printf("\n enter  a char to search in array:");
scanf("%c",&ch);
for(i=0;i<10;i++)
{
if(arr[i]==ch)
{
printf("\n %c found at %d location",ch,i);
break;
}
}
if(i==10)
{
printf("\n %c not found",ch);
}
getch();
}

program hidden password

#include<stdio.h>
#include<conio.h>
void main()
{
char arr[10];
int i;
clrscr();
printf("\n enter your password:");
for(i=0;i<5;i++)
{
arr[i]=getch();
      // printf("*"); try without this//
}
printf("\n your password is ");
for(i=0;i<5;i++)
{
printf("%c",arr[i]);
}
getch();
}