Help If u can

That's the code that I did but there is an extra star in sin(90)
and the negative wave is made of an extra star in each row
If Anyone can help, I will be grateful


The output should be:

sin(0) =                        *
sin(10) =                       ****
sin(20) =                       *******
sin(30) =                       **********
sin(40) =                       *************
sin(50) =                       ***************
sin(60) =                       *****************
sin(70) =                       *******************
sin(80) =                       ********************
sin(90) =                       ********************
sin(100) =                      ********************
sin(110) =                      *******************
sin(120) =                      *****************
sin(130) =                      ***************
sin(140) =                      *************
sin(150) =                      **********
sin(160) =                      *******
sin(170) =                      ****
sin(180) =                      *
sin(190) =                   ****
sin(200) =                *******
sin(210) =             **********
sin(220) =          *************
sin(230) =        ***************
sin(240) =      *****************
sin(250) =    *******************
sin(260) =   ********************
sin(270) =   ********************
sin(280) =   ********************
sin(290) =    *******************
sin(300) =      *****************
sin(310) =        ***************
sin(320) =          *************
sin(330) =             **********
sin(340) =                *******
sin(350) =                   ****
sin(360) =                      *
Last edited on
I don't think your premise is correct, sin(190) looks like it's correctly the negative of sin(170). And if it's just sin(90) that's has an extra star, then that would mean you want it to be lower than sin(80), which makes no sense...

Can you please clarify your requirements?
Should an output of 0.0 produce 0 stars, or 1 star?
Should an output of -1.0 produce 21 stars, 20 stars, or 19 stars?
Should an output of +1.0 produce 21 stars, 20 stars, or 19 stars?

In your current setup, it looks like:
* 0.0 maps to 1 star + 0
* 1.0 maps to 1 star + 19 stars to the right
* -1.0 maps to 1 star + 19 stars to the left

is this not accurate? It seems alright to me, but please clarify what you want.

Hums 'One and nineteen more'...
Last edited on
What the output should be vs what it is?
Oh, he gave the "correct" output in his post, I misread the problem. One minute...
Last edited on
@shady1234:

Basically, your problem is a matter of interpolating 0.0 --> 1, and 1.0 --> 20
This means your slope (rise over run) is 19, or unitLength - 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	double const pi = 3.141592653589793;
	double const tolerance = 0.00001;
	int const unitLength = 20;

	for (double x = 0.0; x <= 360.0; x += 10.0)
	{
		cout << "sin(" << std::setw(3) << x << ") = ";
		double const sinus = sin(x * pi / 180.0);
		
		// y = mx + b
		// The + 0.5 is to round to the nearest integer after applying your tolerance
		int length = (unitLength - 1) * abs(sinus) + 1.0 + tolerance + 0.5;
		
		int offset;
		if (sinus < 0.0)
		{ 
			offset = unitLength - length;
		}
		else
		{
			offset = unitLength;
		}
		
		// Personally, I find it easier to count up...
		for (int i = 0; i < offset; i++)
		{
			cout << " ";
		}
		for (int i = 0; i < length; i++)
		{
			cout << "*";
		}
        
		cout << "\n";
	}
}
Last edited on
Topic archived. No new replies allowed.