#include <iostream> #include <fstream> #include <math.h> using namespace std; #define N 35 // The number of values of h #define PI 3.14159 #define M 600 // The number of samples in t int main(){ double h[N], t[M], x[M], y[M]; // The arrays for h,t,x,y double tmax = 0.06, dt = 0.0001; // To find t // Changes the values of f, a to get different x(t) signals double f = 2000, a = 100; // The frequency in Hz, amplitude // Calculates the values of h h[0] = 361.922; h[34] = h[0]; h[1] = 589.000; h[33] = h[1]; h[2] = 52.556; h[32] = h[2]; h[3] = -538.095; h[31] = h[3]; h[4] = -58.657; h[30] = h[4]; h[5] = 499.472; h[29] = h[5]; h[6] = -251.531; h[28] = h[6]; h[7] = -785.168; h[27] = h[7]; h[8] = 381.999; h[26] = h[8]; h[9] = 812.822; h[25] = h[9]; h[10] = -934.419; h[24] = h[10]; h[11] = -1082.725; h[23] = h[11]; h[12] = 1547.666; h[22] = h[12]; h[13] = 1083.109; h[21] = h[13]; h[14] = -3229.928; h[20] = h[14]; h[15] = -1275.738; h[19] = h[15]; h[16] = 10268.660; h[18] = h[16]; h[17] = 17571.900; h[17] = h[17]; // Calculates the values of t int j=0; for(double tt = 0; tt < tmax; tt += dt) t[j++] = tt; // Compute x[n] for(int i=0;i<M;i++) x[i] = a*sin(2*PI*f*t[i]); //Storing x[n] to file ofstream xfile; xfile.open ("filex.txt"); for(int i=0;i<M;i++) xfile <<x[i]<<endl; xfile.close(); //View x[n] in console for(int i=0;i<M;i++) cout<<"n = "<<i<<", x= "<<x[i]<<endl; // Compute y[n] for(int n = 0; n < M; n++){ double sum = 0; for(int k = 0; k <= min(N-1,n); k++) sum += x[n-k] * h[k]; y[n] = sum; } // Storing y[n] to file ofstream yfile; yfile.open ("filey.txt"); for(int i=0;i<M;i++) yfile <<y[i]<<endl; yfile.close(); cout<<"----------------------------------------"<<endl; //view y[n] in console for(int i=0;i<M;i++) cout<<"n = "<<i<<", y= "<<y[i]<<endl; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}