1

I'm trying to write a c program that draws sine and cosine curves simultaneously by using '+' for sine and 'x' for cosine and '*' when the values of sine and cosine are equal. Here is the code:

#include<stdio.h>
#include<math.h> /* for  sin(x) */

#define M_PI 3.14159265358979323846264338327950288

int main() {
  double x;
  int s_indent;
  int c_indent;
  for(x = -180.0; x <=180.0; x+=15.0) {
    /*  compute  value  */
    s_indent = 10 + 10* sin(x/180 * M_PI);
    c_indent = 10 +  10* cos(x/180 * M_PI);


    if(c_indent == s_indent){
      for(;s_indent;--s_indent) putchar(' ');
      printf("*\n");
    }
    else{
      for(; s_indent; --s_indent) putchar(' ');
      printf("+\n");

      /* plot x at position */

      for(; c_indent; --c_indent) putchar(' ');
      printf("x\n");
    }
  }
  return 0;
}

but the problem with the code is that it produces the curves line by line. Like here:

And I want to make it on the same line like this one here:

Thoughts?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
firat
  • 23
  • 3
  • 1
    Write the characters into a 2D buffer of, say 80 x 25 characters. Then print each line. – Weather Vane Dec 04 '19 at 13:34
  • 1
    Yes, this is possible. Stack Overflow is not a "write my code for me" service. – user253751 Dec 04 '19 at 13:34
  • You may find [one of the answers here](https://stackoverflow.com/q/47906401/645128) to be helpful. – ryyker Dec 04 '19 at 13:36
  • 1
    In your `else` you use two `for` each with a `\n`. How is it supposed to be on the same line? How could you modify it to make it work? – Maxime B. Dec 04 '19 at 13:36
  • I tried it by comparing the values of indents and implying \n according to this. Then the curve went crazy after some point. I posted this code as an example of the one which is closest to the output, which I should be getting at the end. – firat Dec 04 '19 at 14:16
  • OT: when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to obtain the same results. – user3629249 Dec 05 '19 at 18:25
  • @user3629249 thanks for advice. But I need to do it with ISO C90 and need to compile with -Wall -ansi -pedantic. But I think to apply the math library I need to use -lm, is it right? – firat Dec 06 '19 at 19:59
  • @firat. When performing the link step, the linker needs to be supplied with `-lm`. The compile step doesn't care which libraries are involved. Also, using the `-ansi` option is very problematic. and in today's world, not desirable. Many years ago, when C began to diverge from the ANSI standard, it was often used to force the ANSI standard for C. That standard has not been updated (and in general should not be used) – user3629249 Dec 07 '19 at 03:31
  • @firat. if need to use the C90 standard (not really a good idea) then replace: `-std=gnu11` with `-std=c90` – user3629249 Dec 07 '19 at 03:33
  • I hope you realize that the definition of `M_PI` is defined in the header file: `math.h`. it is slightly different than the value your using in the posted code. – user3629249 Dec 07 '19 at 03:41
  • @user3629249 Yeah it s not a good idea you are right but those are the terms of the assignment. If I don't define M_PI, it can't run it direct from the math.h – firat Dec 07 '19 at 19:50

3 Answers3

3

You can: create an empty line

char line[] = "                          ";

set the characters at the appropriate places

line[c_indent] = 'x';
line[s_indent] = '+';

and then output this line:

puts(line);

The case of both sine and cos in a single point is left to you as an exercise ;)

Ctx
  • 18,090
  • 24
  • 36
  • 51
1

In the posted code, each symbol is printed in a separate line at the calculated position, but what you have to do is to determine the order of the symbols and print both in the same line.

Consier using a simple function like

void print_after_n_spaces(int n, const char* str)
{
    while (n-- > 0)
        putchar(' ');
    printf("%s", str);
}

Also, add another branch and calculate the difference between the two positions:

for(int x = -180; x <= 180; x += 15)
{
    double angle = x / 180.0 * M_PI;
    int s_pos = 10.5 + 10.0 * sin(angle);
    int c_pos = 10.5 + 10.0 * cos(angle);
    //          ^^^^ To "round" the values

    if (c_pos > s_pos)
    {
        print_after_n_spaces(s_pos, "+");
        //                          ^^^                 Don't print the newline here
        print_after_n_spaces(c_pos - s_pos - 1, "x\n");
        //                   ^^^^^^^^^^^^^^^^^          Difference between the positions
    }
    else if (c_pos < s_pos) 
    {
        // Here prints first the "x" (the cosine), then the "+"
    }
    else
    {
        // Here prints only "*"
    }
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
0
  • In the else statement, check which one that is larger of s_indent and c_indent.
  • Copy these variables to two new variables largest and smallest.
  • Iterate with for loop over smallest to print spaces, then print either + or x depending on which was smallest.
  • Then iterate from smallest to largest, print spaces, then print either + or x depending on which was largest.

An alternative, more elegant solution is to make a function void printline (int n, char symbol) then call it two times.

Lundin
  • 195,001
  • 40
  • 254
  • 396