Rewritining a code to do the same thing

Below is my code and I want to change it up but for it to function the same way.

#include <vector>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <array>
#include <complex>
#include <cmath>
using namespace std;

vector<double> calculate_mag_response(double start_freq, double end_freq,
int N, vector<double> num, vector<double> den);

int main(int argc,char *argv[])
{
vector<double> myNum, myDen, results;
//fstream outfile;
int N=100;
//outfile.open("datafile.txt");
myNum.push_back(1.0);
myNum.push_back(1.0); //create a smple zero at s=-1
myDen.push_back(1.0);
myDen.push_back(3.0);
myDen.push_back(2.0); //create two poles at s=-1,-2
results=calculate_mag_response(.1,1000,100,myNum,myDen); //Call the student function
//if (N=results.size()!= 100)
//{
//cout << "Your function did not provide correct number of data points. Please fix" << endl;
//exit(0);
//}
for (int k=0;k<=N-1;k++)
cout<<results[k]<<endl;
int r;
cin>> r;
//outfile << i << "\t" << results[i] << endl; //Write this garbage to a file
return(0);
}

vector<double> calculate_mag_response(double w0, double wf,
int N, vector<double> num, vector<double> den)
{
std::vector<double>result;
// double delta, freq, n, d;

//delta = (end_freq-start_freq)/(N-1);
// n=num.size();
///d=den.size();



for(double w=w0;w<=wf;w+=wf/N)


{ complex<double>s (0.0,w);
complex<double>num_result=0;
complex<double>den_result=0;

//freq=start_freq;
//for(int i=N;i>0;i--)
// {
for(unsigned int t=0;t<=num.size()-1;t++)
num_result+=num[t]*pow(s,num.size()-t-1);


//freq=start_freq;
// for(int i=N;i>0;i--)
// {
for(unsigned int i=0;i<=den.size()-1;i++)
den_result+=den[i]*pow(s,den.size()-1-i);
double G = 20*log10(abs(num_result/den_result));
result.push_back(G);
}
return (result);}

// cout << "num is " << num_result << endl << endl;
// cout << "den is " << den_result << endl << endl;






//return result;
// cout << result << " dB" << endl;
//system("pause");
//return (0);
Code review stack exchange could be willing to tediously buff up your code.
(Next time, write down what your code does, and format your code)
closed account (1CfG1hU5)
helps if you put [ code ] before program code and [ /code ] after program code.
exclude the space after each [ and before each ]. have to put this way, or will
colorize the screen only.

example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
  {
   int values[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, i, p=0;
   
   for (i = 0; i <= 9; i++)
     printf("%d ", values[i]);
   
   for (i = 0; i <= 9; i++)
     p += values[i];

   printf("\n\nthe total of all array elements is %d", p);

  return 0;
  }


should look like that
closed account (48T7M4Gy)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <vector>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <array>
#include <complex>
#include <cmath>
using namespace std;

vector<double> calculate_mag_response(double start_freq, double end_freq,
int N, vector<double> num, vector<double> den);

int main(int argc,char *argv[])
{
vector<double> myNum, myDen, results;
//fstream outfile;
int N=100;
//outfile.open("datafile.txt");
myNum.push_back(1.0);
myNum.push_back(1.0); //create a smple zero at s=-1
myDen.push_back(1.0);
myDen.push_back(3.0);
myDen.push_back(2.0); //create two poles at s=-1,-2
results=calculate_mag_response(.1,1000,100,myNum,myDen); //Call the student function
//if (N=results.size()!= 100)
//{
//cout << "Your function did not provide correct number of data points. Please fix" << endl;
//exit(0);
//}
for (int k=0;k<=N-1;k++)
cout<<results[k]<<endl;
int r;
cin>> r;
//outfile << i << "\t" << results[i] << endl; //Write this garbage to a file
return(0);
}

vector<double> calculate_mag_response(double w0, double wf,
int N, vector<double> num, vector<double> den)
{
std::vector<double>result;
// double delta, freq, n, d;

//delta = (end_freq-start_freq)/(N-1);
// n=num.size();
///d=den.size();



for(double w=w0;w<=wf;w+=wf/N)


{ complex<double>s (0.0,w);
complex<double>num_result=0;
complex<double>den_result=0;

//freq=start_freq;
//for(int i=N;i>0;i--)
// {
for(unsigned int t=0;t<=num.size()-1;t++)
num_result+=num[t]*pow(s,num.size()-t-1);


//freq=start_freq;
// for(int i=N;i>0;i--)
// {
for(unsigned int i=0;i<=den.size()-1;i++)
den_result+=den[i]*pow(s,den.size()-1-i);
double G = 20*log10(abs(num_result/den_result));
result.push_back(G);
}
return (result);}

// cout << "num is " << num_result << endl << endl;
// cout << "den is " << den_result << endl << endl;






//return result;
// cout << result << " dB" << endl;
//system("pause");
//return (0);  


This seems to be a 'do my homework' request.
Topic archived. No new replies allowed.