Loading
Archive for 09/01/2012 - 10/01/2012
C - Program for Rate Monotonic Scheduling Algorithm
C - Program for Rate Monotonic Scheduling Algorithm
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int n; float e[20],p[20]; int i; float ut,u,x,y; clrscr(); printf("\n Enter Number of Processes :: "); scanf("%d",&n); for(i=0;i<n;i++) { clrscr(); printf("\n Enter Execution Time for P%d ::",(i+1)); scanf("%f",&e[i]); printf("\n Enter Period for P%d ::",(i+1)); scanf("%f",&p[i]); } //calculate the utilization for(i=0;i<n;i++) { x=e[i]/p[i]; ut+=x; } //calculate value of U y=(float)n; y=y*((pow(2.0,1/y))-1); u=y; clrscr(); if(ut<u) { printf("\n As %f < %f ,",ut,u); printf("\n The System is surely Schedulable"); } else printf("\n Not Sure....."); getch(); }
Posted in
Rate Monotonic Scheduling Algorithm
|
3 Comments
C - Program to Solve Knapsack problem
C - Program to Solve Knapsack Problem
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("Total profit = %d\n",profit);
getch();
}
Posted in
Solve Knapsack problem
|
Leave a comment
C - Program to Cyclically Permute the Elements of an Array
by
Abc
C - Program to Cyclically Permute the Elements of an Array
#include <stdio.h>
void main ()
{
int i,n,number[30];
printf("Enter the value of the n = ");
scanf ("%d", &n);
printf ("Enter the numbers\n");
for (i=0; i<n; ++i)
{
scanf ("%d", &number[i]);
}
number[n] = number[0];
for (i=0; i<n; ++i)
{
number[i] = number[i+1];
}
printf ("Cyclically permted numbers are given below \n");
for (i=0; i<n; ++i)
printf ("%d\n", number[i]);
}
C - Program to print Fibonacci numbers using Recursion
by
Abc
C - Program to Print Fibonacci Numbers using Recursion
#include<stdio.h>
#include<conio.h>
int fib(int);
int f=1,fib1=0,fib2=0,i=0,j;
void main()
{
int num;
clrscr();
printf(" How many Fibonacci numbers do you want?\n");
scanf("%d",&num);
printf("\nFibonacci Numbers are:\n");
f=0;
printf("\n%d\n",f);
f=1;
printf("\n%d\n",f);
for(j=0;j<num-2;j++)
{
f=fib(num);
printf("\n%d\n",f);
}
getch();
}
int fib(int n)
{
while(i<n)
{
if(i<=n)
{
i++;
fib1=fib2;
fib2=f;
f=fib2+fib1;
fib(1);
return f;
}
}
#include<conio.h>
int fib(int);
int f=1,fib1=0,fib2=0,i=0,j;
void main()
{
int num;
clrscr();
printf(" How many Fibonacci numbers do you want?\n");
scanf("%d",&num);
printf("\nFibonacci Numbers are:\n");
f=0;
printf("\n%d\n",f);
f=1;
printf("\n%d\n",f);
for(j=0;j<num-2;j++)
{
f=fib(num);
printf("\n%d\n",f);
}
getch();
}
int fib(int n)
{
while(i<n)
{
if(i<=n)
{
i++;
fib1=fib2;
fib2=f;
f=fib2+fib1;
fib(1);
return f;
}
}
}
Posted in
Fibonacci Numbers using Recursion
|
Leave a comment
C - Program To Find The Largest Of 3 Numbers
by
Abc
C - Program To Find The Largest Of 3 Numbers
#include<stdio.h>
int main() {
int a, b, c;
printf("Enter the value for a:\n");
scanf("%d", &a);
printf("Enter the value for b:\n");
scanf("%d", &b);
printf("Enter the value for c:\n");
scanf("%d", &c);
if ((a > b) && (a > c)) {
printf("\n The big one is a= %d", a);
} else if (b > c) {
printf("\n The big one is b= %d", b);
} else {
printf("\n The big one is c= %d", c);
}
return 0;
}
Posted in
Largest Of 3 Numbers
|
Leave a comment
C - Program to Find Type of Triangle (equilateral, isosceles and scalene)
by
Abc
C - Program to Find Type of Triangle (equilateral, isosceles and scalene)
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
void main()
{
int a, b, c;
float s, area;
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))
{
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
if (a == b && b == c)
{
printf("Equilateral Triangle. \n");
printf("Area of Equilateral Triangle is: %f", area);
}
else if (a == b || b == c || a == c)
{
printf("Isosceles Triangle. \n");
printf("Area of an Isosceles Triangle: %f", area);
}
else
{
printf("Scalene Triangle. \n");
printf("Area of Scalene Triangle: %f", area);
}
}
int a, b, c;
float s, area;
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))
{
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
if (a == b && b == c)
{
printf("Equilateral Triangle. \n");
printf("Area of Equilateral Triangle is: %f", area);
}
else if (a == b || b == c || a == c)
{
printf("Isosceles Triangle. \n");
printf("Area of an Isosceles Triangle: %f", area);
}
else
{
printf("Scalene Triangle. \n");
printf("Area of Scalene Triangle: %f", area);
}
}
else {
printf("Triangle formation not possible");
}
getch();
}
C - Program To Find The Roots Of Quadratic Equation
by
Abc
C - Program To Find The Roots Of Quadratic Equation
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int a,b,c;
float disc,r1,r2;
for(;;)
{
printf("\nEnter the non Zero co-efficients a, b,c\n");
scanf("%d%d%d",&a,&b,&c);
if((a==0) || (b==0) || (c==0))
{
printf("\nPlease enter non zero co-efficients \n");
}else{
disc=((b*b)-(4*a*c));
if(disc>0)
{
printf("Roots are Real:\n");
r1=(-b-(sqrt(disc)))/(2.0*a);
r2=(-b+(sqrt(disc)))/(2.0*a);
printf("Roots are:%f and %f \n", r1,r2);
}
else if(disc<0)
{
printf("Roots are Imaginary:\n");
r1=-b/(2.0*a);
printf("First root: %lf +%fi \n", r1,sqrt(-disc)/(2.0*a));
printf("Second root:%lf -%lfi\n", r1,sqrt(-disc)/(2.0*a));
}
else
{
printf("Roots are Equal\n");
r1= -b/(2.0*a);
printf("Equal; roots are:%f and %f \n", r1,r1);
}
getch();
}
}
}
Posted in
Find The Roots Of Quadratic Equation
|
Leave a comment
C - Program to Implement Interpolation Search
by
Abc
C - Program to Implement Interpolation Search
#include <stdio.h>
#include <stdlib.h>
#define MAX 200
int interpolation_search(int a[], int bottom, int top, int item) {
int mid;
while (bottom <= top) {
mid = bottom + (top - bottom)
* ((item - a[bottom]) / (a[top] - a[bottom]));
if (item == a[mid])
return mid + 1;
if (item < a[mid])
top = mid - 1;
else
bottom = mid + 1;
}
return -1;
}
int main() {
int arr[MAX];
int i, num;
int item, pos;
printf("\nEnter total elements (num< %d) : ", MAX);
scanf("%d", &num);
printf("Enter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("\nELEMENTS ARE\n: ");
for (i = 0; i < num; i++)
printf("%d\t", arr[i]);
printf("\nSearch For : ");
scanf("%d", &item);
pos = interpolation_search(&arr[0], 0, num, item);
if (pos == -1)
printf("\nElement %d not found\n", item);
else
printf("\nElement %d found at position %d\n", item, pos);
return 0;
}
Posted in
Interpolation Search
|
Leave a comment
C - Program to Implement Shell Sort
by
Abc
C - Program to Implement Shell Sort
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void shell_sort(char *chars, int c)
{
register int i, j, space, k;
char x, a[5];
a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;
for(k=0; k < 5; k++) {
space = a[k];
for(i=space; i < c; ++i) {
x = chars[i];
for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space)
chars[j+space] = chars[j];
chars[j+space] = x;
}
}
}
int main() {
char string[300];
printf("Enter a string:");
gets(string);
shell_sort(string, strlen(string));
printf("The sorted string is: %s.\n", string);
return 0;
}
Posted in
Shell Sort
|
Leave a comment
C - Program to Sort Names
by
Abc
C - Program to Sort Names
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;
clrscr();
printf("Enter the value of N\n");
scanf("%d", &N);
printf("Enter %d names\n", N);
for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}
for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
for(i=0; i< N ; i++)
{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
}
Posted in
Sort Names
|
Leave a comment
C - Program to Implement Bucket Sort
by
Abc
C - Program to Implement Bucket Sort
#include<stdio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for(i=0; i < n; i++)
{
count[i] = 0;
}
for(i=0; i < n; i++)
{
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
}
int main()
{
int array[100];
int num;
int i;
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("Enter the %d elements to be sorted:\n",num);
for(i = 0; i < num; i++ )
{
scanf("%d",&array[i]);
}
printf("\nThe array of elements before sorting : \n");
for (i = 0;i < num;i++)
{
printf("%d ", array[i]);
}
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, num);
for (i = 0;i < n;i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Posted in
Bucket Sort
|
Leave a comment
C - Program to Implement Topological Sort
by
Abc
C - Program to Implement Topological Sort
#include<stdio.h>
#define MAX 200
int n,adj[MAX][MAX];
int front = -1,rear = -1,queue[MAX];
void main()
{
int i,j = 0,k;
int topsort[MAX],indeg[MAX];
create_graph();
printf(“The adjacency matrix is:\n”);
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;i<=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
printf("Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
printf("%d",topsort[i]);
printf("\n");
}
create_graph()
{
int i,max_edges,origin,destin;
printf("\n Enter number of vertices:");
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
printf("\n Enter edge %d (00 to quit):",i);
scanf("%d%d",&origin,&destin);
if((origin == 0) && (destin == 0))
{
printf("Invalid edge!!\n");
i–;
}
else
adj[origin][destin] = 1;
}return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
printf(“Queue Underflow”);
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
returnin_deg;
}
Posted in
Topological Sort
|
Leave a comment
C - Program for Radix Sort
by
Abc
C - Program for Radix Sort
#include <stdio.h>
#define MAX 100
#define SHOWPASS
void print(int *a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radix_sort(int *a, int n)
{
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
}
}
int main()
{
int arr[MAX];
int i, num;
printf("\nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("\nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("\nARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\n\nSORTED : ");
print(&arr[0], num);
return 0;
}
Posted in
Radix Sort
|
Leave a comment
C - Program to implement Floyd's Algorithm
C - Program to implement Floyd's Algorithm
#include<stdio.h>
#include<conio.h>
int
min(
int
,
int
);
void
floyds(
int
p[10][10],
int
n)
{
int
i,j,k;
for
(k=1;k<=n;k++)
for
(i=1;i<=n;i++)
for
(j=1;j<=n;j++)
if
(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int
min(
int
a,
int
b)
{
if
(a<b)
return
(a);
else
return
(b);
}
void
main()
{
int
p[10][10],w,n,e,u,v,i,j;;
clrscr();
printf
(
"\n Enter the number of vertices:"
);
scanf
(
"%d"
,&n);
printf
(
"\n Enter the number of edges:\n"
);
scanf
(
"%d"
,&e);
for
(i=1;i<=n;i++)
{
for
(j=1;j<=n;j++)
p[i][j]=999;
}
for
(i=1;i<=e;i++)
{
printf
(
"\n Enter the end vertices of edge%d with its weight \n"
,i);
scanf
(
"%d%d%d"
,&u,&v,&w);
p[u][v]=w;
}
printf
(
"\n Matrix of input data:\n"
);
for
(i=1;i<=n;i++)
{
for
(j=1;j<=n;j++)
printf
(
"%d \t"
,p[i][j]);
printf
(
"\n"
);
}
floyds(p,n);
printf
(
"\n Transitive closure:\n"
);
for
(i=1;i<=n;i++)
{
for
(j=1;j<=n;j++)
printf
(
"%d \t"
,p[i][j]);
printf
(
"\n"
);
}
printf
(
"\n The shortest paths are:\n"
);
for
(i=1;i<=n;i++)
for
(j=1;j<=n;j++)
{
if
(i!=j)
printf
(
"\n <%d,%d>=%d"
,i,j,p[i][j]);
}
getch();
}
Posted in
Floyd's Algorithm
|
Leave a comment
C Program to Implement Warshall's Algorithm
by
Abc
C - Program to Implement Warshall's Algorithm
#include<stdio.h>
#include<conio.h>
#include<math.h>
int
max(
int
,
int
);
void
warshal(
int
p[10][10],
int
n)
{
int
i,j,k;
for
(k=1;k<=n;k++)
for
(i=1;i<=n;i++)
for
(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int
max(
int
a,
int
b)
{
if
(a>b)
return
(a);
else
return
(b);
}
void
main()
{
int
p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf
(
"\n Enter the number of vertices:"
);
scanf
(
"%d"
,&n);
printf
(
"\n Enter the number of edges:"
);
scanf
(
"%d"
,&e);
for
(i=1;i<=e;i++)
{
printf
(
"\n Enter the end vertices of edge %d:"
,i);
scanf
(
"%d%d"
,&u,&v);
p[u][v]=1;
}
printf
(
"\n Matrix of input data: \n"
);
for
(i=1;i<=n;i++)
{
for
(j=1;j<=n;j++)
printf
(
"%d\t"
,p[i][j]);
printf
(
"\n"
);
}
warshal(p,n);
printf
(
"\n Transitive closure: \n"
);
for
(i=1;i<=n;i++)
{
for
(j=1;j<=n;j++)
printf
(
"%d\t"
,p[i][j]);
printf
(
"\n"
);
}
getch();
}
Posted in
Warshall's Algorithm
|
Leave a comment
How to Print Output sreen of a C Graphics Program
by
Abc
How to Print Output sreen of a C Graphics Program
Normally we press Prt Scr(Print Screen) button to copy the output of a c-program. However, this does not works for C Graphics Programs. So here's the alternate way to Copy/Print Output Screen of Graphical C-Program :
- Download DOSBox (an x86 emulator with dos).
- Open DOSBox and Follow the commands,
- mount c c: [press enter]
- c: [press enter]
- cd c:\tc\bin [press enter]
- tc [press enter]
C program for Countdown
by
Abc
C program for Countdown
#include<graphics.h> #include<dos.h> #include<conio.h> main() { int gd = DETECT, gm, i; char a[5]; initgraph( &gd, &gm, "C:\\TC\\BGI"); settextjustify( CENTER_TEXT, CENTER_TEXT ); settextstyle(DEFAULT_FONT,HORIZ_DIR,3); setcolor(RED); for(i = 30 ; i >=0 ; i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2, getmaxy()/2, a); delay(1000); if ( i == 0 ) break; cleardevice(); } getch(); closegraph(); return 0; }
Posted in
Countdown
|
Leave a comment
C - Program for Press me button game
by
Abc
C - Program for Press me button game
#include <stdio.h> #include <conio.h> #include <dos.h> #include <graphics.h> #include <stdlib.h> union REGS i, o; int left = 265, top = 250; void initialize_graphics_mode() { int gd = DETECT, gm, error; initgraph(&gd,&gm,"C:\\TC\\BGI"); error = graphresult(); if (error != grOk) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } } void showmouseptr() { i.x.ax = 1; int86(0x33,&i,&o); } void hidemouseptr() { i.x.ax = 2; int86(0x33,&i,&o); } void getmousepos(int *x,int *y) { i.x.ax = 3; int86(0x33,&i,&o); *x = o.x.cx; *y = o.x.dx; } void draw_bar() { hidemouseptr(); setfillstyle(SOLID_FILL,CYAN); bar(190,180,450,350); showmouseptr(); } void draw_button(int x, int y) { hidemouseptr(); setfillstyle(SOLID_FILL,MAGENTA); bar(x,y,x+100,y+30); moveto(x+5,y); setcolor(YELLOW); outtext("Press me"); showmouseptr(); } void draw() { settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(BLUE); rectangle(0,0,639,450); setcolor(RED); outtextxy(160,25,"Try to press the \"Press me\" button"); outtextxy(210,50,"Press escape key to exit"); setfillstyle(XHATCH_FILL,GREEN); setcolor(BLUE); bar(1,1,75,449); bar(565,1,638,449); showmouseptr(); draw_bar(); draw_button(left,top); } void initialize() { initialize_graphics_mode(); if( !initmouse() ) { closegraph(); printf("Unable to initialize the mouse"); printf("Press any key to exit...\n"); getch(); exit(EXIT_SUCCESS); } draw(); } int initmouse() { i.x.ax = 0; int86(0x33,&i,&o); return ( o.x.ax ); } void get_input() { int x, y; while(1) { getmousepos(&x,&y); /* mouse pointer in left of button */ if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left ) { draw_bar(); left = left + 4; if (left > 350) left = 190; draw_button(left,top); } /* mouse pointer in right of button */ else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100)) { draw_bar(); left = left - 4; if (left < 190) left = 350; draw_button(left,top); } /* mouse pointer above button */ else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3)) { draw_bar(); top = top + 4; if (top > 320) top = 180; draw_button(left,top); } /* mouse pointer below button */ else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3)) { draw_bar(); top = top - 4; if (top < 180) top = 320; draw_button(left,top); } if (kbhit()) { if (getkey() == 1) exit(EXIT_SUCCESS); } } } int getkey() { i.h.ah = 0; int86(22,&i,&o); return( o.h.ah ); } main() { initialize(); get_input(); return 0; }
Posted in
Press me button game
|
Leave a comment
C Program for Pattern Matching
by
Abc
C - Program for Pattern Matching
#include <stdio.h> #include <string.h> int match(char [], char []); int main() { char a[100], b[100]; int position; printf("Enter some text\n"); gets(a); printf("Enter a string to find\n"); gets(b); position = match(a, b); if(position != -1) { printf("Found at location %d\n", position + 1); } else { printf("Not found.\n"); } return 0; } int match(char text[], char pattern[]) { int c, d, e, text_length, pattern_length, position = -1; text_length = strlen(text); pattern_length = strlen(pattern); if (pattern_length > text_length) { return -1; } for (c = 0; c <= text_length - pattern_length; c++) { position = e = c; for (d = 0; d < pattern_length; d++) { if (pattern[d] == text[e]) { e++; } else { break; } } if (d == pattern_length) { return position; } } return -1; }
Posted in
Pattern Matching
|
Leave a comment
C -Program to Open Website
by
Abc
C -Program to Open Website
or
Web Browser in C
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #include<dos.h> #include<string.h> void initialize_graphics_mode(); int get_key(); void draw(); union REGS i, o; main() { int key, i = 0, xpos, ypos, button; char arr[200], temp[5], *ptr; char a[] = "C:\\Progra~1\\Mozill~1\\firefox "; strcpy(arr,a); i = strlen(a); initialize_graphics_mode(); draw(); while(1) { if(kbhit()) key = get_key(); if((key>=97&&key<=122)||(key>=65&&key<=90)||key==46||key==47||key==63) { arr[i] = key; sprintf(temp,"%c",arr[i]); outtext(temp); if(getx()>470) { clearviewport(); moveto(5,2); } i++; } else if ( key == 13 ) { arr[i] = '\0'; system(arr); break; } else if ( key == 27 ) { closegraph(); exit(EXIT_SUCCESS); } if(button==1&&xpos>=150&&xpos<=480&&ypos>=300&&ypos<=330) { system("C:\\Progra~1\\Mozill~1\\firefox programmingsimplified.com"); break; } key = -1; } closegraph(); return 0; } void initialize_graphics_mode() { int gd = DETECT, gm, errorcode; initgraph(&gd,&gm,"C:\\TC\\BGI"); errorcode = graphresult(); if( errorcode != grOk ) { printf("Graphics error : %s\n",grapherrormsg(errorcode)); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } } int get_key() { i.h.ah = 0; int86(22,&i,&o); return( o.h.al ); } void draw() { settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(275,11,"Web Browser"); outtextxy(155,451,"<a href="http://www.programmingsimplified.com"">www.programmingsimplified.com"</a>); outtextxy(5,105,"Enter URL : "); rectangle(120,100,600,130); setviewport(121,101,599,129,1); moveto(5,1); }
Posted in
C -Program to Open Website,
Web Browser in C
|
Leave a comment
C - Program for Smiling Face Animation
by
Abc
C - Program for Smiling Face Animation
#include<graphics.h> #include<conio.h> #include<stdlib.h> main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW); setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; }
Posted in
Smiling Face Animation
|
Leave a comment
C - Program for Traffic Light Simulation
by
Abc
C - Program for Traffic Light Simulation
#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TC\\BGI"); midx = getmaxx()/2; midy = getmaxy()/2; setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 3); settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(midx, midy-10, "Traffic Light Simulation"); outtextxy(midx, midy+10, "Press any key to start"); getch(); cleardevice(); setcolor(WHITE); settextstyle(DEFAULT_FONT, HORIZ_DIR, 1); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy-50, 22); setfillstyle(SOLID_FILL,RED); floodfill(midx, midy-50,WHITE); setcolor(BLUE); outtextxy(midx,midy-50,"STOP"); delay(2000); graphdefaults(); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy, 20); setfillstyle(SOLID_FILL,YELLOW); floodfill(midx, midy,WHITE); setcolor(BLUE); outtextxy(midx-18,midy-3,"READY"); delay(2000); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy+50, 22); setfillstyle(SOLID_FILL,GREEN); floodfill(midx, midy+50,WHITE); setcolor(BLUE); outtextxy(midx-7,midy+48,"GO"); setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 4); outtextxy(midx-150, midy+100, "Press any key to exit..."); getch(); closegraph(); return 0; }
Posted in
Traffic Light Simulation
|
Leave a comment
C/C++ Program for Implementation of Extended Euclidian Algorithm
C/C++ Program for Implementation of Extended Euclidian Algorithm
#include<iostream.h> #include<conio.h> main() { int a,b,x,y,lx,ly,q,temp; clrscr(); cout<<"\n\t\t *** Extended Euclidian Algorithm ***\n"; cout<<"\n For : ax + by = gcd(a,b) "; cout<<"\n\n Enter value of a :: "; cin>>a; cout<<"\n Enter value of b :: "; cin>>b; x=0,lx=1; y=1,ly=0; while(b!=0) { q=a/b; temp=b; b=a%b; a=temp; temp=x; x=lx-(q*x); lx=temp; temp=y; y=ly-(q*y); ly=temp; } cout<<"\n x :: "<<lx<<"\ty :: "<<ly; cout<<"\n\n GCD :: "<<a; getch(); } /* Sample Output - *** Extended Euclidian Algorithm *** For : ax + by = gcd(a,b) Enter value of a :: 5 Enter value of b :: 8 x :: -3 y :: 2 GCD :: 1 */
Posted in
Extended Euclidian Algorithm
|
Leave a comment
C/C++ Program for Implementation MD5 Hashing Technique
by
Abc
C/C++ Program for Implementation MD5 Hashing Technique
#include<iostream.h> #include<conio.h> #include<math.h> int input[2000],pointer; int carry; class Block { public: int word[32]; Block() { word[32]=0; } Block operator +(Block); void operator =(Block); Block operator &(Block); Block operator |(Block); Block operator ^(Block); Block operator !(); void operator <<(int shift_bits); }X[16],T[64],a,b,c,d,result,temp; Block Block::operator +(Block t) { int x,y,ans; for(int i=31;i>=0;i--) { x=word[i]; y=t.word[i]; switch(carry) { case 0: if(((x==0)&&(y==0))||((x==1)&&(y==1))) { ans=0; carry=x; } if(((x==0)&&(y==1))||((x==1)&&(y==0))) { ans=1; carry=0; } break; case 1: if(((x==0)&&(y==0))||((x==1)&&(y==1))) { ans=1; carry=x; } if(((x==0)&&(y==1))||((x==1)&&(y==0))) { ans=0; carry=1; } break; } result.word[i]=ans; } return(result); } Block Block::operator &(Block t) { for(int i=0;i<32;i++) result.word[i]=word[i] & t.word[i]; return(result); } Block Block::operator |(Block t) { for(int i=0;i<32;i++) result.word[i]=word[i] | t.word[i]; return(result); } Block Block::operator ^(Block t) { for(int i=0;i<32;i++) result.word[i]=word[i] ^ t.word[i]; return(result); } Block Block::operator !() { for(int i=0;i<32;i++) result.word[i]=!word[i]; return(result); } void Block::operator=(Block t) { for(int i=0;i<32;i++) word[i]=t.word[i]; } //Circular left shift int get_shift_bits(int round,int step) { int bits; int p[17]={0,7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22}; int q[17]={0,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20}; int r[17]={0,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23}; int s[17]={0,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21}; switch(round) { case 1: bits=p[step]; break; case 2: bits=q[step]; break; case 3: bits=r[step]; break; case 4: bits=s[step]; break; } return(bits); } void Block::operator <<(int shift_bits) { int i,p,j=0; p=32-shift_bits; for(i=shift_bits;i<p;i++) { temp.word[j]=word[i]; j++; } for(i=0;i<shift_bits;i++) { temp.word[j]=result.word[i]; j++; } } void convert(char msg[]) { int charvalue,i,j; for(i=0;msg[i]!='\0';i++) { int temp[8]={0}; charvalue=(int)msg[i]; j=0; while(charvalue!=0) { temp[j]=charvalue%2; charvalue=charvalue/2; j++; } for(j=7;j>=0;j--) { input[pointer]=temp[j]; pointer++; } } cout<<"\n length of orignal msg before padding: " <<pointer; } void padding() { int r,p,i,padding_bits; r=pointer+64; i=1; p=512*i; while(r>=p) { i++; p=512*i; } padding_bits=p-r; cout<<"\n\npadding bits:"<<padding_bits; input[pointer]=1; pointer++; for(i=1;i<padding_bits;i++) { input[pointer]=0; pointer++; } cout<<"\n\nlength of message after padding: "<<pointer; } void append_length() { int i,t; i=pointer+64; t=pointer; pointer=i; while(t!=0) { input[i]=t%2; t=t/2; i--; } cout<<"\n \n message after padding:\n"; for(i=0;i<pointer;i++) cout<<" "<<input[i]; } void display(Block t) { cout<<"\n"; for(int i=0;i<32;i++) cout<<t.word[i]<<" "; } void div_into_sblk(int round) { int ptr=0,i,j,counter=0,x; cout<<"\n\n"; for(i=0;i<16;i++) { switch(round) { case 1: ptr=0+ptr; break; case 2: x=1+(5*i); ptr=x%16; break; case 3: x=5+(3*i); ptr=x%16; break; case 4: x=7*i; ptr=x%16; break; } cout<<"\nsubblock"<<i+1<<" :"; for(j=0;j<32;j++) { X[ptr].word[j]=input[counter]; cout<<X[i].word[j]<<" "; counter++; } ptr++; } } void cal_table() { int i,radian_i,abs_i,power_i,ans; for(i=1;i<=64;i++) { radian_i=(3.14/180)*i; abs_i=abs(sin(radian_i)); power_i=pow(2,32); ans=power_i *abs_i; int index=31; while(ans!=0) { T[i].word[index]=ans%2; ans=ans/2; } } } void init_registers() { int i; int p[32]={0,1,1,0,0,1,1,1,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,1}; int q[32]={1,1,1,0,1,1,1,1,1,1,0,0,1,1,0,1,1,0,1,0,1,0,1,1,1,0,0,0,1,0,0,1}; int r[32]={1,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,1,0,0,1,1,1,1,1,1,1,0}; int s[32]={0,1,1,1,0,1,1,0,0,1,0,1,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0}; for(i=0;i<32;i++) { a.word[i]=p[i]; b.word[i]=q[i]; c.word[i]=r[i]; d.word[i]=s[i]; } } void rounds() { int round,k,cntr=0; getch(); for(round=1;round<=4;round++) { for(k=0;k<16;k++) { switch(round) { case 1: result=(b&c|(!b)&d); break; case 2: result=(b&d)|(c&(!d)); break; case 3: result=b^c^c; break; case 4: result=c^(b|(!d)); break; } result=result+a+X[k]+T[cntr]; int shift_bits=get_shift_bits(round,k+1); result<<shift_bits; a=d;d=c;c=b; b=b+result; cntr++; } cout<<"\n result after round"<<round<<" :"; display(a); display(b); display(c); display(d); } cout<<"\n\nmessage digest:\n"; display(a); display(b); display(c); display(d); } void MD5_algorithm() { char message[100]; cout<<"\n enter input message:"; cin>>message; convert(message); padding(); append_length(); div_into_sblk(1); init_registers(); rounds(); } void main() { clrscr(); MD5_algorithm(); getch(); }
Posted in
MD5 Hashing Technique
|
Leave a comment
C/C++ Program for Implementation of Diffie Hellman Key Exchange Algorithm
by
Abc
C/C++ Program for Implementation of Diffie Hellman Key Exchange Algorithm
#include<conio.h> #include<iostream.h> #include<math.h> int alice(int,int,int); int bob(int,int,int); void main() { long int g,x,y,a,b,k1,k2,n; clrscr(); cout<<"\n\t Enter value of n & g"; cin>>n>>g; cout<<"\n\t Enter value of x & y"; cin>>x>>y; a=alice(n,g,x); cout<<"\n\t alice end value:"<<a; b=bob(n,g,y); cout<<"\n\t bob end value:"<<b; k1=alice(n,b,x); cout<<"\n\t valueof k1 :"<<k1; k2=alice(n,a,y); cout<<"\n\t valueof k2 :"<<k2; getch(); } int alice(int n,int g,int x) { long int a,a1; a1=pow(g,x); a=a1%n; return(a); } int bob(int n,int g,int y) { long int b,b1,k2,t2; b1=pow(g,y); b=b1%n; return(b); }
Posted in
Diffie Hellman Key Exchange Algorithm
|
4 Comments
C - Program for Character Generation
C - Program for Character Generation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,j;
int a[20][20]=
{{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<19;i++)
{
for(j=0;j<19;j++)
{
if(a[i][j]==1)
putpixel(100+j,200+i,WHITE);
}
}
getch();
}
Posted in
Character Generation
|
Leave a comment
C - Program for Implementation of Mid-Point Ellipse Drawing Algorithm
by
Abc
C - Program for Implementation of Mid-Point Ellipse Drawing Algorithm
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
int main(void)
{
int gd=DETECT,gm;
int cenx,ceny;
float Pk,a,b,x,y;
clrscr();
printf("\n\n Enter 'a' and 'b': ");
scanf("%f%f",&a,&b);
initgraph(&gd,&gm,"c:\\tc\\bgi");
cenx=getmaxx()/2;
ceny=getmaxy()/2;
Pk=b*b-b*a*a+0.25*a*a;
x=0;
y=b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (2*x*b*b <= 2*y*a*a)
{
if (Pk<0)
{
x=x+1;
y=y;
Pk=Pk+2*x*b*b+3*b*b;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (y>0)
{
if (Pk>0)
{
x=x;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a;
}
else
{
x=x+1;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*b;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
gotoxy(1,25);
printf ("\npress any key to exit.");
getch();
closegraph();
return 0;
}
Posted in
Mid-Point Ellipse Drawing Algorithm
|
Leave a comment
C - Program for Liang Barsky Line Clipping Algorithm
by
Abc
C - Program for Liang Barsky Line Clipping Algorithm
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);
printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}
Posted in
Liang Barsky Line Clipping Algorithm
|
1 Comment
C - Program for Cohen Sutherland Line Clipping Algorithm
by
Abc
C - Program for Cohen Sutherland Line Clipping Algorithm
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}
C - Program to Implement Flood Fill Algorithm
by
Abc
C - Program to Implement Flood Fill Algorithm
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\n\tEnter the seed pt. : ");
scanf("%d%d",&x,&y);
cleardevice();
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}
/*SAMPLE INPUT*/
/*Enter the number of edges of polygon 4
X0 Y0 = 50 50
X1 Y1 = 200 50
X2 Y2 = 200 300
X3 Y3 = 50 300
Enter the seed point 100 100*/
Posted in
Flood Fill Algorithm
|
Leave a comment
C - Program to Implement Boundary Fill Algorithm
by
Abc
C - Program to Implement Boundary Fill Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}
void main()
{
int x,y,n,i;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x = 100; y = 100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
Posted in
Boundary Fill Algorithm
|
Leave a comment
C - Program to Implement Bezier Curve Drawing Algorithm
by
Abc
C - Program to Implement Bezier Curve Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }
printf("\n\n Enter four points : \n\n");
for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}
C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}
Posted in
Bezier Curve Drawing Algorithm
|
Leave a comment
C - Program without Main Function
by
Abc
C - Program without Main Function
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)int begin()
{
printf(” hello “);
}
Explanation -
Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function.
The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.
NOTE: A Preprocessor is program which processess the source code before compilation.
Look at the 2nd line of program -
#define decode(s,t,u,m,p,e,d) m##s##u##t
What is the preprocessor doing here. The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m,s,u & t into msut). The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).
Now look at the third line of the program -
#define begin decode(a,n,i,m,a,t,e)
Here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,’a’,’i’ & ‘n’.
So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s it…
The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there exists a hidden main function in the program. Here we are using the proprocessor directive to intelligently replace the word begin” by “main”. In simple words int begin=int main.
Posted in
C - Program without Main Function
|
Leave a comment
C - Program for Guessing Game
by
Abc
C - Program for Guessing Game
#include<stdio.h> #include<stdlib.h> #include<time.h>
void main() { int num,guess=-1,tries=0,pass=0; time_t t; srand((unsigned)time(&t)); num=rand()%100; while((guess!=num)&&tries<8) { printf("Enter the guess num b/w 0&100
(you have %d tries left out)\n",(8-tries));
scanf("%d",&guess); tries++; if(guess==num) { printf("Hurray you guessed it correctly!!!\n"); pass=1; } else if(num< guess) printf("Your guess is too high\n"); else printf("Your guess is too low\n"); } if(pass==0) printf("Sorry you lost! The correct number is %d\n",num);
}
Posted in
Guessing Game
|
Leave a comment
Self Destructing Program in C
by
Abc
Self Destructing Program in C
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { printf("This program will destroy itself if u press any key!!!\n"); getch(); remove(_argv[0]);/*array of pointers to command line arguments*/ }
Posted in
Self Destructing Program in C
|
Leave a comment
C - Program for Pigeon Breeding Problem
by
Abc
C - Program for Pigeon Breeding Problem
#include<stdio.h> #include<process.h> struct node { int age; struct node *link; }; typedef struct node* NODE;
NODE getnode() { NODE x; x=(NODE)malloc(sizeof(struct node)); if(x==NULL) { printf(“Out of memory\n”); exit(1); } return x; } void main() { unsigned long int count=1; unsigned int months,i; NODE first=getnode();/*this is the intial adult pair*/ first->age=2; /*assume the age of initial adult pair as 2*/ first->link=NULL; printf(“Enter the no. of months\n”); scanf(“%u”,&months); for(i=0;iage>=2)&&(temp->age<=60)) { NODE temp1=getnode(); temp->age+=1; temp1->age=1; temp1->link=first; first=temp1; temp=temp->link; ++count; } else { temp->age+=1; temp=temp->link; } } } printf(“Total no. of pairs after %u months=%ld\n”,months,count); }
Posted in
Pigeon Breeding Problem
|
Leave a comment
C - Program to Produce Sound of Specific Frequency
by
Abc
C - Program to Produce Sound of Specific Frequency
#include<dos.h> main() { int a; for ( a = 200 ; a <= 1000 ; a = a + 20 ) { sound(a); delay(25); } nosound(); return 0; }
Posted in
Produce Sound of Specific Frequency
|
Leave a comment
C - Program to Display Time
by
Abc
C - Program to Display Time
#include<stdio.h> #include<dos.h> main() { struct time t; gettime(&t); printf("Current system time is %d : %d : %d\n",t.ti_hour,t.ti_min,t.ti_sec); return 0; }
Posted in
Display Time
|
Leave a comment
C - Program to Display Date
by
Abc
C - Program to Display Date
#include<stdio.h> #include<dos.h> main() { struct date d; getdate(&d); printf("Current system date is %d/%d/%d\n",d.da_day,d.da_mon,d.da_year); return 0; }
Posted in
Display Date
|
Leave a comment
C - Program to Calculate Square Root of Given Number
by
Abc
C - Program to Calculate Square Root of Given Number
#include <stdio.h> #include <math.h> int main() { double n, result; printf("Enter a number to calculate it's square root\n"); scanf("%lf", &n); result = sqrt(n); printf("Square root of %.2lf = %.2lf\n", n, result); return 0; }
Posted in
Square Root of Number
|
Leave a comment
C - Program to Display Mouse Pointer in Graphics Mode
by
Abc
C - Program to Display Mouse Pointer in Graphics Mode
#include<graphics.h> #include<conio.h> #include<dos.h> int initmouse(); void showmouseptr(); union REGS i, o; main() { int status, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else showmouseptr(); getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); }
C Program to Display Mouse Pointer in Textmode
by
Abc
C Program to Display Mouse Pointer in Textmode
#include<dos.h> #include<conio.h> int initmouse(); void showmouseptr(); union REGS i, o; main() { int status; status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else showmouseptr(); getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); }
Posted in
Display Mouse Pointer in Text mode
|
Leave a comment