Saturday, February 05, 2011

Bar Graph


/*
* THIS IS A C PROGRAM THAT DRAWS LINE GRAPH AND BAR GRAPH FOR THE GIVEN SETS OF INPUTS.
* DATA MAY RANGE FROM 0.0001 TO 1050000.
*/

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>

typedef struct
{
int x,y;
} POINT;

void Fill_Rect(int,int,int,int,int);

int main()
{
int graphdriver , graphmode, N, i, pos=120;
int maxy=getmaxy(),maxx=getmaxx();
double v[20],f[20], min=v[0],max=v[0];
double MAX=400.0,MIN=50.0,a,b,width=20.0,spacing;
char temp[50];

detectgraph(&graphdriver,&graphmode);
initgraph(&graphdriver,&graphmode,"C:\\TC\\BGI");
if(graphresult()!=grOk) {
closegraph();
return 0;
}

printf("Enter the number of data(<20) :  ");
scanf("%d",&N);

if(N>=20)
    N=20;
if(N<=0) 
    N=1;

for(i=0;i<N;++i)
   scanf("%lf",v+i);

for(i=0;i<N;++i)
{
   if(min>v[i])
        min=v[i];
   if(max<v[i])
       max=v[i];
}


if(N!=1)
     spacing=(400.0-width)/(double)(N-1);

a=(MAX-MIN)/log(max/min);
b=MAX-a*log(max);

for(i=0;i<N;++i)
   f[i]=a*log(v[i])+b;

cleardevice();

line(100,maxy-MIN,maxx,maxy-MIN);
line(100,0,100,maxy-MIN);
settextstyle(2,0,4);

for(i=0;i<N;++i)
{
            bar3d(pos,maxy-f[i]-20,pos+width,maxy-MIN,width/4,1);
            setlinestyle(DASHED_LINE,1,1);
            line(pos,maxy-f[i]-20,100,maxy-f[i]-20);
            sprintf(temp,"%f",v[i]);
            outtextxy(3,maxy-f[i]-20,temp);
            setlinestyle(SOLID_LINE,1,1);
            pos+=spacing;
}

getch();
cleardevice();

line(100,maxy-MIN,maxx,maxy-MIN);
line(100,0,100,maxy-MIN);
pos=120;

for(i=0;i<N-1;++i)   
 {
            line(pos,maxy-f[i]-20,pos+width+spacing,maxy-f[i+1]-20);
            pos+=width+spacing;
}

getch();

closegraph();
return 0;

}


No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.