Please Help ( Nested Loop Pattern)

I tried 2 days to make it but I don't get even near.
Using nested loops, write a C++ program that draws sin(x) function approximation pattern.
Hint: draw 20 stars when sin(x) is max “i.e. sin (90) & sin (270) “
Your output must look exactly like this:

Using nested loops, write a C++ program that draws sin(x) function approximation pattern.
Hint: draw 20 stars when sin(x) is max “i.e. sin (90) & sin (270) “
Your output must look exactly like this:

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)=                        *
the second wave is opposite
Last edited on
@shady1234,
Why don't you break it down into 3 stages?

STAGE 1: Just loop through degrees 0, 10, 20, ..., 360 and output the sine of the angle.
STAGE 2: Same loop, but this time output the NUMBER of stars instead of the sine value.
STAGE 3: Same, but change the number of stars into actually printing out that many.


Thus:
STAGE 1 - just the value of sine (careful degrees -> radians)
sin(  0) = 0
sin( 10) = 0.173648
sin( 20) = 0.34202
sin( 30) = 0.5
sin( 40) = 0.642788
sin( 50) = 0.766044
sin( 60) = 0.866025
sin( 70) = 0.939693
sin( 80) = 0.984808
sin( 90) = 1
sin(100) = 0.984808
sin(110) = 0.939693
sin(120) = 0.866025
sin(130) = 0.766044
sin(140) = 0.642788
sin(150) = 0.5
sin(160) = 0.34202
sin(170) = 0.173648
sin(180) = 1.22461e-016
sin(190) = -0.173648
sin(200) = -0.34202
sin(210) = -0.5
sin(220) = -0.642788
sin(230) = -0.766044
sin(240) = -0.866025
sin(250) = -0.939693
sin(260) = -0.984808
sin(270) = -1
sin(280) = -0.984808
sin(290) = -0.939693
sin(300) = -0.866025
sin(310) = -0.766044
sin(320) = -0.642788
sin(330) = -0.5
sin(340) = -0.34202
sin(350) = -0.173648
sin(360) = -2.44921e-016



STAGE 2: convert to numbers of stars (note: include a negative sign where appropriate); there's actually a little bit of ambiguity here, if every row is to have at least one star.
sin(  0) = stars( 1 )
sin( 10) = stars( 4 )
sin( 20) = stars( 7 )
sin( 30) = stars( 10 )
sin( 40) = stars( 13 )
sin( 50) = stars( 15 )
sin( 60) = stars( 17 )
sin( 70) = stars( 19 )
sin( 80) = stars( 20 )
sin( 90) = stars( 20 )
sin(100) = stars( 20 )
sin(110) = stars( 19 )
sin(120) = stars( 17 )
sin(130) = stars( 15 )
sin(140) = stars( 13 )
sin(150) = stars( 10 )
sin(160) = stars( 7 )
sin(170) = stars( 4 )
sin(180) = stars( 1 )
sin(190) = stars( -4 )
sin(200) = stars( -7 )
sin(210) = stars( -10 )
sin(220) = stars( -13 )
sin(230) = stars( -15 )
sin(240) = stars( -17 )
sin(250) = stars( -19 )
sin(260) = stars( -20 )
sin(270) = stars( -20 )
sin(280) = stars( -20 )
sin(290) = stars( -19 )
sin(300) = stars( -17 )
sin(310) = stars( -15 )
sin(320) = stars( -13 )
sin(330) = stars( -10 )
sin(340) = stars( -7 )
sin(350) = stars( -4 )
sin(360) = stars( -1 )



STAGE 3: actually output (spaces + ) stars
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
Hi shady1234,
Nested loops... >_< why ?

It's a vertical to horizontal draw of the sinus wave. I consider the amplitude as being 20 stars character. I didn't output the info "sin(0) =" ^_^. If you want to do it, you have to pad the string of stars yourself :oP~

You can download the code here https://www.punksheep.com/partage/C++/sinusAsStringsOfStars.zip
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
#include <iostream>
#include <string>
#include <cmath>

std::string sinusText(double degreeAngle)
{
	double const pi = 3.141592653589793;
	double const sinus = sin(degreeAngle * pi / 180.0);
	int const unitLength = 20;
	int sinusLength = (1.0 + sinus) * unitLength;
	int start, end;
	if(sinus < 0.0)
	{
		start = sinusLength;
		end = unitLength;
	}
	else
	{
		start = unitLength;
		end = sinusLength;
	}
	std::string starText;
	for(int i = start; i--;)
	{
		starText += ' ';
	}
	for(int i = end - start; i--;)
	{
		starText += '*';
	}
	starText += '*';
	return starText;
}

int main()
{
	for(double angle = 0; angle <= 360.0; angle += 10.0)
	{
		std::cout << sinusText(angle) << std::endl;
	}

	return 0;
}


Your teacher is a porc ! gruiiiiiiiiiiiiiiiiiiiiiiiic
And I refuse to embed my loop in the main loop :oD~
You also have to do that yourself. I refuse ! \o/
Last edited on
Topic archived. No new replies allowed.