How to Make a Sine Wave in C++
Trigonometric functions are fundamental to physics and engineering. There are three main trigonometric functions -- sine (sin) cosine (cos) and tangent (tan). All of these functions have a periodic dependence; sine and cosine are also oscillatory. C++ is one of the most well known programming languages and is used throughout the software industry, games industry and scientific community. Sometimes, it can be necessary to generate a sine wave within C++.
Instructions
-
-
1
Open a new plain text file. Some C++ compilers have a dedicated editor. If this is the case, create a new file.
-
2
Find the include files that are needed for the program. For this program, two main include files will be needed; "iostream" and "cmath.i". Type the following lines at the top of the new file:
#include <iostream>
#include <cmath>
Type the beginning of the main function, which is where the bulk of the program is placed:
int main()
{
float sinwave [63];
-
-
3
Generate the sine function and place it into an array. To do this, a "for loop" is used that increments a variable between two set number limits. Type the following:
for ( n=0; n<6.28 ; n=n+0.1)
{
sinwave [n]=sin(n);
}
-
4
Save the file and compile it. The array sin(n) will be filled with a sine wave.
-
1
References
- Photo Credit Jupiterimages/Photos.com/Getty Images