Newbie having trouble with a Sine Wave.

So we have to make a program to graph a Triangle and a Sine wave. The triangle bit is working fine, but the sine waves always comes out like this:

*
*
*
*
*
*
*

Instead of being properly curved. I double checked the math, so I know it is right, there is just some bug that is keeping the *'s from moving out on the X axis.

Any help you can offer would be appreciated.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void drawLine( int height, char character) 
{
for (int x=0; x < height; x=x+1)
{
cout << character;
}
cout << endl;
}

int main(void)

{
int height=10;
char character= ' ';
int x = 1;
int choice = 0;
int t = 0;
cout << "Press 1 to make a triagle, or 2 to make a sine wave." << endl;
cin >> choice;

if (choice == 1){

	cout << "What is the triangle's height? " << endl;
	cin >> height ;
	cout << "What is the triangle's character? " << endl;
	cin>> character ;

	for (x=1; x<=height; x=x+1)
	{
	drawLine( x, character );
	}
	for (x=height-1; x>=1; x=x-1)
	drawLine( x, character );
	return 0;}

else if (choice == 2){
		{
	void placePoint (int t );
		if (t < 180);
		for (int t=0;  t < 180;  t=t+4)
		{
		double ans;
		double ans2;
		int final;
		ans = t * 3.14 / 180;
		ans2 = sin(ans);
		final = (int)(80.0 * ans2);
		if (x < final);
			{x = x+1;
			cout << " ";}
			cout << "*" << endl;}
		}
		return 0;
	}}
Last edited on
I see a major problem with your code on lines 40-58. I have fixed the indentation to make it more obvious:
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	else if (choice == 2)
	{
		{
			void placePoint (int t ); //declaration, does nothing

			if (t < 180); //semicolon, does nothing

			for (int t=0;  t < 180;  t=t+4) //why not "t += 4"?
			{
				double ans;
				double ans2;
				int final;
				//why separate the above and the below?
				ans = t * 3.14 / 180; //looks ambiguous - use parens?
				ans2 = sin(ans);
				final = (int)(80.0 * ans2);

				if (x < final); //semicolon, does nothing

				{
					x = x+1; //why not "++x;"?
					cout << " ";
				}
				cout << "*" << endl;
			}
		}
		return 0;
	}
}
Last edited on
I suppose I really am missing something super obvious. I'll go over the code again and see if I cannot find where I screwed up.

Thanks for your time.
Semicolons are deviously hidden and not very obvious, but they are in plain sight. Look for semicolons that should not be there, such as the ones I pointed out in my post...
Yeah, I trimmed out some of the fat, as well as those two and got it...well I don't wanna say half working, but now I am even more confused on what happened.

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
else if (choice == 2)
	{
		{
						
			for (int t=0;  t < 180;  t +=4)
			{
				double ans;
				double ans2;
				int final;
				
				ans = t * (3.14) / (180);
				ans2 = sin(ans);
				final = (int)(80.0 * ans2);

				while (x < final) 
				{
					x = x+1;
					cout << " ";
				}
				cout << "*" << endl;
			}
		}
		return 0;
	}
}


And the output it gives me looks something like this:

*
.....*
.........*
.........*
............*
........*
........*
.......*
..... *
.....*
*
*
*
*
*
*
*
*

The thing is, it is supposed to be roughly 45 lines, out to a potential of 80 spaces. I got it to give me the numbers it was using and I got:

0, 5, 11, 16, 22 etc etc etc, up to just shy of 80, where it then turned around and went back towards zero. So it is behaving like a Sine wave should mathematically, but I still am lost as to why it starts to shift at the start, and then sort of dies.
Last edited on
line 57, it should a a while, not a if.
Consider: can you have a negative number of spaces?

The center of your sinewave is aligned to the far left of your output.
but I still am lost as to why it starts to shift at the start, and then sort of dies
The Sine function can take negative values if the angle is beyond 180°.
That means "final" is negative, hence the while loop never loops.

The minimum value a sine function can take is -1.
If you do ans2 = 1 + sin(ans); it should display the entire wave.
Last edited on
If you do ans2 = 1 + sin(ans); it should display the entire wave.


It does not. All it does is increase the numerical output and keep the same style as before. A bump at the start, and then it falls flat.

Also, I do not understand how the Sine could possibly be negative. The function only continues while t < 180. Stopping it at 180 degrees should keep all the numbers positive (or zero, in the case of t=0).
Last edited on
Do you ever reset the value of x?
Do you ever reset the value of x?


And THAT was the line that made me smack my forehead. Thanks, I think I know how to fix it now.

EDIT: It works perfectly now. Thanks for your patience and assistance, both of you.
Last edited on
If you're interested, here's my implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cmath>

const long double Pi {3.1415926535897932384626433};
const long double RadConvDeg {Pi/180.0}; //multiply RtL, divide LtR

int main()
{
    using str = std::string;
    using st = str::size_type;
    for(st i {0}; i < 180; i += 4)
    {
        std::cout << str(static_cast<st>(std::sin(RadConvDeg*i)*80.0), ' ') << '*' << std::endl;
    }
}
http://ideone.com/D9zId8
It takes advantage of the fill constructor of std::string, which takes a number and a character - the string is then initialized with the given number of the given character.
Topic archived. No new replies allowed.