wanna generate square wave

want to generate square wave which repeats every period. but the thing in for{} isn't repeating. plz help me with this.

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
 fstream fout;		
	fout.open("data1.txt");

	cout << "enter the frequency of the signal" << endl;
	cin >> frequency;

	cout << "enter the amplitude of the signal" << endl;
	cin >> amplitude;

	cout << "enter the sampling rate of the signal" << endl;
	cin >> samplingrate;

	cout << "enter the start time of the signal" << endl;
	cin >> starttime;

	cout << "enter the end time of the signal" << endl;
	cin >> endtime;

	time = starttime;

	period = 1 / frequency;

	for(time = starttime; time <= endtime; time += (1/samplingrate))
	{
		
		if (time <= period/2)
		{
			result = amplitude;
		}
		else
		{
			result = (-1)*amplitude;
		}

	fout << time << "	" << result << endl;
	cout << result << endl;
	}

	fout. close();
"I think my car is out of brake fluid. Here's the exhaust pipe, can you fix it?"

Please show the whole program. In specific, we need to know how those variables are defined.
sorry. I just started learning c++ and I didn't know how to question.
The whole program is

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
59
60
61
62
63
64
65
#include <iostream>
#include <math.h>
#include <fstream>
#include <cstdlib>
using namespace std;

#define PI 3.14159265
#define e 2.71828182

int main()
{
	double period;
	double frequency;
	double angularfrequency;
	double amplitude;
	double time=0;
	double samplingrate;
	double endtime;
	double starttime;
	double result;
	double phase;
	double alpha;
	double gradient;


/*periodic rectangular signal*/
	fstream fout;		
	fout.open("data1.txt");

	cout << "enter the frequency of the signal" << endl;
	cin >> frequency;

	cout << "enter the amplitude of the signal" << endl;
	cin >> amplitude;

	cout << "enter the sampling rate of the signal" << endl;
	cin >> samplingrate;

	cout << "enter the start time of the signal" << endl;
	cin >> starttime;

	cout << "enter the end time of the signal" << endl;
	cin >> endtime;

	time = starttime;

	period = 1 / frequency;

	for(time = starttime; time <= endtime; time += (1/samplingrate))
	{
		
		if (period <= time <= period/2)
		{
			result = amplitude;
		}
		else
		{
			result = (-1)*amplitude;
		}

	fout << time << "	" << result << endl;
	cout << result << endl;
	}

	fout. close();	
It looks like your for loop is working fine:
http://ideone.com/NCUtxS
However I'm pretty sure it shouldn't be the amplitude unchanged all the time.
if (period <= time <= period/2)

This doesn't due what you expect.

Even if it did, this would never be true because time can never be greater than period while being less than period/2. (since period/2 is going to be less than period)

Maybe you meant to do this?:

if (period >= time && time >= period/2)
Topic archived. No new replies allowed.