Need help graphing a sine wave.

I have an assignment to graph a sine wave using modular programming, but i have no idea where to even start. it doesn't have to be like an exact thing, i beleive i could graph it out with *'s, but im unsure about how to tell it to keep putting those down, and alternate them into a sine wave. any help would be greatly appreciated.

Let's work from the top down. First, recognize that the sin() function takes it's argument in radians. So to print a full cycle of the sine wave you'd do something like
1
2
3
4
for (double angle=0.0; angle <= PI; angle += PI/50) {
    double value = sin(angle);
    plot(value);
}


Now how do you plot the value? It will be between -1 and 1. Let's divide that range into 50 pieces and print a star in the right spot
1
2
3
4
5
6
void plot(double value)
{
    int spot = (value+1.0) / 50;
    print spot_minus_one spaces
    print "*\n";
}

Now how do you print those spaces? That will be a loop:
1
2
3
for (int i=0; i <spot-1; ++i) {
    cout << ' ';
}


Jam it all together, fix whatever errors exist and you have the answer.
first of all, thanks for the quick response, and sorry for my slowness, ive had a lot of homework. ive been trying to fiddle with it, but ive mostly been having trouble with the

print spot_minus_one spaces

part. ive never heard of a "print" command, only "cout" and also the spot_minus_one is something ive never been taught about either.
Sorry, that's pseudo-code.You should replace that with code that will print spot-1 spaces. And the next line means that you should print a star and a newline. Translate these to C++ code and you'll have it.
oh, sorry. like i said, im completely new to this, im still trying to grasp the basics still. for the void plot "module" i think its called, im not sure what to return at the end. ive tried return plot, value, i, etc...but i always get the same error, "return value type does not match the function type." sorry if im asking really stupid questions.

EDIT:
i realized voids dont have returns, so currently i have a program that outputs the result of the sine function from 0 to 1 then back down, and there are 50 results of that function, which is way more than i was able to do before, so thank you a million times, but im not sure how to set those to be the "*"'s.
Last edited on
No need to apologize. This is the beginners' forum so you're in the right place. Please post your code so we can take a look at it.
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
#include "stdafx.h"
#include <iostream>
#include <cmath>





#define PI 3.141592

using namespace std;



int main()
{
	for (double angle = 0.0; angle <= PI; angle += PI / 50)
	{
		double value = sin(angle);
		cout << (value) << endl;	
	}
	
    return 0;
}

void plot(double value)
{
	int spot = (value + 1.0) / 50;

	cout << "*\n";

	for (int i = 0; i <spot - 1; ++i)
	{
		cout << " ";
	}

}
Ah My fault. Line 28 should be int spot = (value+1.0) * 50;

Line 30 should go after line 35.

Add void plot (double value); at line 13 to declare the plot function. In C++ you have to declare functions before you use them, even if you define the code for the function later on.

With all that done, and if it appears that line 20 is printing the right values. then replace line 20 with plot(value);
oh, that is so cool(it's working now), thank you a million times!
Topic archived. No new replies allowed.