1

i am beginner in c programming, i am currently using gedit of ubuntu 10.04 to write c prog, i want to plot a graph, but i am able to do it, can any one tell me hw it can be done or else is there any way to extract the data from the output to spreadsheet where i can plot the req, graph?? I appreciate your help..n thanx!!!

0

5 Answers 5

1

Medsphere has some pretty great GTK# widgets for graphing (among other things), but you'll need to be a little more clear about your input/output requirements to get more specific help.

Sign up to request clarification or add additional context in comments.

2 Comments

#include<stdio.h> #include<math.h> void main() { int i, k; int co=100; int L=1; float A=0.02; int t[]={0, 5, 10}; float T[21][3]; float y; float x[] = {0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1}; printf("distance temperature\n"); for(k=0;k<=2;k++) { for (i=0;i<=20;i++) { y=exp(-(A*3.14*3.14)*t[k]/(LL)); T[i][k]=coy*sin(3.14*x[i]/L); T[0][k]=0; T[20][k]=0; printf("%f %d %f\n", x[i], t[k], T[i][k]); } } } this is my code i want to plot T[i][k] v/s x[i]
That is utterly unreadable - maybe edit the question and put it in there. Also, my answer was related to C#, and your code (and now the question edit) indicates that you're actually speaking about C, which is a horse of a different color. If you have the data in a spreadsheet, I'd do what Marco said and use gnuplot, without writing a C program at all.
1

You could you this character(■) to represent the count in the graph. This is a character that can be printed by

printf("%c", (char)254u);

Consider some random float_arr and hist array that hold the count.

Code

// Function generating random data
for (i = 0; i < n; i++){
    float random = ((float)rand() / (float)(RAND_MAX));
    float_arr[i] = random;
    printf("%f  ", random);
}
//Dividing float data into bins
for (i = 0; i < n; i++){
    for (j = 1; j <= bins; j++){

        float bin_max = (float)j / (float)bins;
        if (float_arr[i] <= bin_max){
            hist[j]++;
            break;
        }
    }
}
// Plotting histogram
printf("\n\nHistogram of Float data\n");
for (i = 1; i <= bins; i++)
{
    count = hist[i];
    printf("0.%d |", i - 1);
    for (j = 0; j < count; j++)
    {
        printf("%c", (char)254u);
    }
    printf("\n");
}

Output

Histogram of Float data
0.0 |■■■■■■■■■■■■■■■■■■■■■■
0.1 |■■■■■■■■■■■■■■■■
0.2 |■■■■■
0.3 |■■■■■■■■■■■■■■
0.4 |■■■■■■■■
0.5 |■■■■■■■■■■■■■■■■
0.6 |■■■■■■■■■■
0.7 |■■■■■■■
0.8 |■■■■■■■■■■■■■■■
0.9 |■■■■■■■

Comments

0

Gnuplot (http://www.gnuplot.info/) is a pretty capable and free tool for graphing data. Your question is not clear as to whether you want to plot data programmatically though.

2 Comments

if possible prgrammically..or if it is very difficult for a beginner like me, i would llike to know is there any way to extract the output data to spreadsheet where i can plot it...
I second gnuplot. Very easy to output csv and then feed it to gnuplot.
0

I think MathGL can help you. It is GPL plotting library which can create a window with yours plot without knowledge of any widgets library. Also it have nice graphics for matrices and 3-ranged data.

Comments

0

Your problem is the same that I have. To do this there are some libraries written to facilitate the work that is more or less this:

  1. open a new window
  2. draw something. That is coordinates, curves, background, etc.

In c/c++ there are some of these libraries are:

  1. libplot, the gnu plot utils www.gnu.org/s/plotutils. Is a very basic one and the documentation have nice examples, to plot in svg, png, and other files. Also you can plot in a window or make animations.
  2. mathGL is open-gl based.

Another way to make a plot is to generate your data in your c code and then plot it with an other program. It is possible to run gnuplot and pass your data trough a pipe. But you must use fork and pipes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.