Plotting y = a*(sin(x)) + b in text-mode

Hey, I initiated the following code and now I’m running the code for a=0 and any real value of b, for which I should get a line y=b, but that isn’t happening, instead I get x and y axes only with no plot. So please tell me where the code has a shortcoming.

#include <stdio.h>
#include <math.h>

#define WIDTH 60
#define HEIGHT 20
#define X WIDTH/2
#define Y HEIGHT/2
#define XMAX WIDTH-X-1
#define XMIN -(WIDTH-X)
#define YMAX HEIGHT-Y
#define YMIN -(HEIGHT-Y)+1

char coordinate_system[HEIGHT][WIDTH];

int plot(int x, int y);
void x_y_axes(void);
void show_plot(void);

int main()
{
    int a, b;
    float x,y;

    printf("Enter a and b:\n");
    scanf("%d%d", &a, &b);

    x_y_axes();
    for(x=-3.1416;x<=3.1416;x+=0.1)
    {
        y = (a*(sin(x))) + b;
        plot(rintf(x*10),rintf(y*8));
    }
    show_plot();

    return(0);
}

/* Set "pixel" at specific coordinates */
int plot(int x, int y)
{
    if( x < XMAX && x > XMIN && y < YMAX && y > YMIN )
        //return(-1);
        //else
            coordinate_system[Y-y][X+x] = '*';
}

/* displays coordinate system */
void x_y_axes(void)
{
    int x,y;
    /* draw the intersecting axes */
    for(y=0;y<HEIGHT;y++)
        coordinate_system[y][X] = '|';
    for(x=0;x<WIDTH;x++)
        coordinate_system[Y][x] = '-';
}

/* display grid */
void show_plot(void)
{
    int x,y;

    for(y=0;y<HEIGHT;y++)
    {
        for(x=0;x<WIDTH;x++)
            putchar(coordinate_system[y][x]);
        putchar('\n');
    }
}

plot(rintf(x10),rintf(y8));

editing the above to

plot(x,y);

solved your problem.

I googled “rintf()” but could not get much about it. What are they and why are you using them anyway?

1 Like

Very good answer brother, @saurabh2561 . You’ll get your answer hereunder
http://www.mkssoftware.com/docs/man3/rint.3.asp

1 Like

@saurabh2561:
You made it look so easy.
Thanks.

@saurabh2561:
Regarding rintf(), I used it give the plot an enlarged look.

For more info: