-
beginner to intermediate
Hi,
So i think i have passed the beginners level of c programming. My last program had a nice menu, you input sets of data through options, it read from text files, saved to textfiles, did calculations and displayed it, used arrays etc.
Now i want to try and learn some cooler stuff.
My last program saved its output data to a text file, so the user could then input it to excel, and draw graphs. Is there a way i can open excel (assuming the location of it on the users computer) and input the data to cells there???
Every time you closed my last program and re opened it, you had to reenter data, does anyone know a good way to have an "open recent" option?
If i have a load of sets of data - a 100 say, please can you point me in the direction of drawing a text graph of those data points, with | __ as the axis and . as a point on the graph?
Thanks guys
-
If you have a set of data . . . say, in an array of something much like this structure . . .
Code:
struct plot_point_t {
double xpos, ypos;
};
. . . then, it's really quite simple. Well, relatively simple, anyhow . . .
Assuming that xpos and ypos are both positive, find the maximum of both . . .
Note that this is not completely valid C, just should give you an idea on how to do this kind of thing.
Code:
/* higher above:
size_t plots = 100;
struct plot_point_t *points = malloc(sizeof(struct plot_point_t)*plots)
*/
double xmax = 0.0, ymax = 0.0;
size_t x;
for(x = 0; x < plots; x ++) {
if(points[x].xpos > xmax) xmax = plots[x].xpos;
}
/* . . . repeat above for the y axis . . . */
for(x = 0; x < plots; x ++) {
if(points[x].ypos > ymax) ymax = plots[x].ypos;
}
/* . . . now determine the scale . . . */
/* assuming you've got 80x20 to work with here . . . use env[] to get the COLUMNS environment variable under *nix . . .*/
int xres = xmax/80;
int yres = ymax/20;
int plotpositions[100][2]; /* NOTE: dynamically allocate this using the plots variable. just don't feel like doing a doubly-allocated array here . . . */
for(x = 0; x < plots; x ++) {
plotpositions[x][0] = (int)(points[x].xpos/xres);
plotpositions[x][1] = (int)(points[x].ypos/yres);
}
/* now to paint them . . . */
size_t y;
for(y = 0; y < 20; y ++) {
for(x = 0; x < 20; x ++) {
size_t i;
int printdot = 0;
for(i = 0; i < plots; i ++) {
if(plotpositions[i][0] == x && plotpositions[i][1] == y) printdot = 1;
}
if(printdot) putc('.');
else putc(' ');
}
}
Little lengthy, but it should be easy enough to follow. Note that a triply-nested loop is *not* the best way to do this, but is by far the easiest and simplest to follow.