Helpful Information
 
 
Category: C Programming
how to make graphs in C??

help!im tryin to make a bar graph n c which when given a specified value of percentages will display it corespondingly...
can anyone help me with the source code on how to do it using loops? coz im not familiar with other complex drawing tools stuff in c yet...just want it to be simple...:cool:

as far as i know, there is no graphic functions in ansi c.

which library do you want to use? which OS are you on? this stuff is not portable :(

you could do it portable printing a number of *īs though.. do you want this?

Originally posted by M.Hirsch
as far as i know, there is no graphic functions in ansi c.

which library do you want to use? which OS are you on? this stuff is not portable :(

you could do it portable printing a number of *īs though.. do you want this?

yup,i want the *'s type of printing---what im thnkin is that let's say given the % 50,25,15,10 it will generate a bar graph..but i just want it to be done simply usin loops and being filled up with the character '*' (sort of being done manually) not really usin the lib.functions...do you know a code to do such coz i cant figure it out....:confused:
thnx;)

ok, i got one for you:

maxValue=4000;
myValue=100;
myMaxWidth=79; // max 79 *īs

for (i=0;i<myValue/maxValue*myMaxWidth; i++) {
print("*");
}


just thought about my code again... i think c will cut off the decimals if both vars are int values. then use "floor(1.0*myValue/maxValue*myMaxWidth)" to force fp division.

yeah...you can get a graphics package called gnuplot.

once you have that and figured it out, do a search on the web for gnuplot_i.c ....that's a whole c api for connecting to gnuplot in the first place.

The only problem is, and I hope I didn't get your hopes up....it will only work on Unix.

hope I was a help.

Des

:D

That is not true I have VC++ 6.0 and use GNUplot. on the GNUplot web site there are c and c++ ports. Also there are GUI's ofr gnuplot that run in win

If you just want to display the data, try this (I posted this an another like it, but I guess search won't look into code posts):



#include <stdlib.h>
#include <stdio.h>

#define NUMDIST 10
#define MAXNUMS 1000000
#define MAX_X 10

void printDist(int *bins, FILE *fo){
int i, j, max=0, numPrint;
char display[NUMDIST][MAX_X];

//normalize results
for (i=0; i<NUMDIST; i++){
if (max < bins[i]) max = bins[i];
for (j=0; j<MAX_X; j++)
display[i][j] = ' ';
}

//place results into display grid
for (i=0; i<NUMDIST; i++){
numPrint = (bins[i] / (float)max) * MAX_X;
for (j=0; j<numPrint; j++)
display[i][j] = '*';
}

//output results horizontally
for (i=0; i<NUMDIST; i++){
fprintf(fo, "%4d (%9d) ", i, bins[i]);
numPrint = (bins[i] / (float)max) * MAX_X;
for (j=0; j<numPrint; j++)
fputc(display[i][j], fo);
fprintf(fo, "\n");
}

fprintf(fo, "\n\n");

//output results vertically
for (j=MAX_X-1; j> -1; j--){
for (i=0; i<NUMDIST; i++)
fprintf(fo, "%c ", display[i][j]);
fprintf(fo, "\n");
}
for (i=0; i<NUMDIST; i++)
fprintf(fo, "%d ", i);
fprintf(fo, "\n\n");

return;
}

int main(){
int bins[NUMDIST];
int i, val;

srand(10);//change to get a fresh set of numbers
printf("Iterations: %d\n", MAXNUMS);
//zero the input array
for (i=0; i<NUMDIST; i++){
bins[i] = 0;
}

printf("Normal distribution\n");
//zero the input array
for (i=0; i<NUMDIST; i++){
bins[i] = 0;
}
//increment for each found number
//this should generate a 'uniform' distribution
for (i=0; i<MAXNUMS; i++){
val = (rand() % NUMDIST) / 3;
val += (rand() % NUMDIST) / 3;
val += (rand() % NUMDIST) / 3;
bins[val]++;
}

printDist(bins, stdout);

return 0;
}


Output:



Iterations: 1000000
Normal distribution
0 ( 27089) *
1 ( 80961) ***
2 ( 161792) *******
3 ( 216140) **********
4 ( 216114) *********
5 ( 161620) *******
6 ( 90524) ****
7 ( 35936) *
8 ( 8857)
9 ( 967)


*
* *
* *
* * * *
* * * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * * * * *
0 1 2 3 4 5 6 7 8 9










privacy (GDPR)