1
#include <stdio.h>
#include <math.h>
void plotline (float y)
{
    char c='@';
    if (x==0.0)
        printf("%c\n",c);
    else if (x==1.0)
        printf("%41c\n",c);

}
void plot ()
{
    float y,x;
    for (x=0.0;x<=3.2;x+=0.2)
    {
        y=sin(x)*sin(x);
        plotline(y);
    }
}
void main()
{
    plot();
}

I'm trying to print the graphic curve of sin^2(x) based on some given values on my plot but I'm getting this output so far,

@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@

I want want the first @ to be diplayed on 0,0 and then each sum to print another at sign (@) to make it look like the sin^2(x) curve. I'm currently trying to fix it using a two dimensional array but it doesnt seem to work either.

expected output
@
 @
      @
            @
                    @
                            @
                                  @
                                      @
                                       @
                                     @
                                 @
                          @
                  @
          @
    @
@
  • 1
    That is because you plot in only two locations, and the value is never *exactly* `1.0`. But anyway, that is not the correct code you posted: please copy/paste the *actual* code. – Weather Vane Mar 24 '20 at 19:44
  • @WeatherVane Yeah I thought so too, I'm trying too google a solution for that. Tips are welcome if you want to give me an advice. – George Lekk Mar 24 '20 at 19:46
  • 2
    What I mean, is that code will not compile, so it's not the code that produces the output you show. Welcome to StackOverflow! May I suggest you take the [tour](https://stackoverflow.com/tour) and read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Weather Vane Mar 24 '20 at 19:47
  • @WeatherVane Thanks for the links on [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Well I just picked up C but the online compiler compiles it just fine. – George Lekk Mar 24 '20 at 19:50
  • 1
    BTW I suggest you scale the `float` to the integer width of the console. – Weather Vane Mar 24 '20 at 19:50
  • Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) Try `printf("%.9f\n", x);` to see what is happening – Weather Vane Mar 24 '20 at 19:57
  • @WeatherVane I'm getting this output `0.000000000 0.039469503 0.151646644 0.318821132 0.514599800 0.708073437 0.868696868 0.971111178 0.999147356 0.948379099 0.826821625 0.653666139 0.456250161 0.265741318 0.112216793 0.019914724` I'm think those are the sin^2(x) sums. Thanks for the links again they are really helpful!. *Edit* I did it. I used the solution posted by @Eraklon but then there where some spaces on the output on the first and last line but I fixed it using this `if (n==0) printf("%c\n",'@'); else printf("%*c%c\n", n, ' ', '@');` – George Lekk Mar 24 '20 at 20:07
  • 1
    It can't: in the function `plotline` variable `x` has not been defined. That's why you should always copy/paste the code samples. But which one of those values `== 1.0`? – Weather Vane Mar 24 '20 at 20:09

1 Answers1

1

Comparing equivalence between floats are tricky. x might never be exactly 1.0. Also as a solution you could try this

void plotline (float x)
{
    int n = x * 50;
    printf("%*c%c\n", n, ' ', '@'); 
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29