0

Could someone please have a look at my code and tell me anything that can be improved? Anything at all - layout, using headers etc?

Thanks in advance!

```
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
typedef double(*fun)(double);
//Declare functions
double gauss(fun f, double &a, double &b, int &n, double &t, double &g1, double &g2, double &g3, double &g4, double &g5, double &h1, double &h2, double &h3, double &h4, double &h5, double &answer, vector<double> &abscisass, vector<double> &weights);
double equation(double t);
double err(double &answer, double &exactanswer, double &error);
//=============================================================================================================
int main()
{
double a = 0.0;
double b = 6.0;
double g1 = 0.0;
double g2 = 0.0;
double g3 = 0.0;
double g4 = 0.0;
double g5 = 0.0;
double h1 = 0.0;
double h2 = 0.0;
double h3 = 0.0;
double h4 = 0.0;
double answer = 0.0;
double exactanswer = 76.750557;
double error = 0.0;
int n = 0;
double t = 0;
vector<double> abscisass(30,0);
vector<double> weights(30,0);
// n = 1
abscisass[0] = 0.0;
weights [0] = 2.000000000000000;
// n = 2
abscisass[1] = 0.577350269189626;
weights [1] = 1.000000000000000;
// n = 3
abscisass[2] = 0.0;
weights [2] = 0.888888888888889;
abscisass[3] = 0.774596669241483;
weights [3] = 0.555555555555556;
// n = 4
abscisass[4] = 0.339981043584856;
weights [4] = 0.652145154862546;
abscisass[5] = 0.861136311594053;
weights [5] = 0.347854845137454;
// n = 5
abscisass[6] = 0.0;
weights [6] = 0.568888888888889;
abscisass[7] = 0.538469310105683;
weights [7] = 0.478628670499366;
abscisass[8] = 0.906179845938664;
weights [8] = 0.236926885056189;
gauss(equation,a,b,n,t,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,answer,abscisass,weights);
err(answer,exactanswer,error);
cout << "the answer is " << setprecision(20) << answer << endl;
cout << "the error is " << setprecision(6) << error << endl;
return 0;
}
double gauss(fun f, double &a, double &b, int &n, double &t, double &g1, double &g2, double &g3, double &g4, double &g5, double &h1, double &h2, double &h3, double &h4, double &h5, double &answer, vector<double> &abscisass, vector<double> &weights)
{
cout << "\n********** GAUSS QUADRATURE **********"<< endl;
cout << "For the equation f(t) = te^2t" << endl;
cout << "Enter the quadrature order, n between 1 and 10 you wish to use: " << endl;
cin >> n;
if(n==1)
{
t = abscisass[0];
g1 = (((b - a)/2)*t) + (b + a)/2; //coordinate transformation
h1 = (weights[0])*((b - a)/2)*(f(g1)); // substitution into original equation
answer = h1;
}
if(n==2)
{
t = abscisass[1];
g1 = (((b - a)/2)*t) + (b + a)/2; //coordinate transformation
h1 = (weights[1])*((b - a)/2)*(f(g1)); // substitution into original equation
t = -1*abscisass[1];
g2 = (((b - a)/2)*t) + (b + a)/2;
h2 = (weights[1])*((b - a)/2)*(f(g2));
answer = h1 + h2;
}
else if(n==3)
{
t = abscisass[2];
g1 = (((b - a)/2)*t) + (b + a)/2; //coordinate transformation
h1 = (weights[2])*((b - a)/2)*(f(g1)); // substitution into original equation
t = abscisass[3];
g2 = (((b - a)/2)*t) + (b + a)/2;
h2 = (weights[3])*((b - a)/2)*(f(g2));
t = -1*abscisass[3];
g3 = (((b - a)/2)*t) + (b + a)/2;
h3 = (weights[3])*((b - a)/2)*(f(g3));
answer = h1 + h2 + h3;
}
else if(n==4)
{
t = abscisass[4];
g1 = (((b - a)/2)*t) + (b + a)/2; //coordinate transformation
h1 = (weights[4])*((b - a)/2)*(f(g1)); // substitution into original equation
t = -1*abscisass[4];
g2 = (((b - a)/2)*t) + (b + a)/2;
h2 = (weights[4])*((b - a)/2)*(f(g2));
t = abscisass[5];
g3 = (((b - a)/2)*t) + (b + a)/2;
h3 = (weights[5])*((b - a)/2)*(f(g3));
t = -1*abscisass[5];
g4 = (((b - a)/2)*t) + (b + a)/2;
h4 = (weights[5])*((b - a)/2)*(f(g4));
answer = h1 + h2 + h3 + h4;
}
else if(n==5)
{
t = abscisass[6];
g1 = (((b - a)/2)*t) + (b + a)/2; //coordinate transformation
h1 = (weights[6])*((b - a)/2)*(f(g1)); // substitution into original equation
t = abscisass[7];
g2 = (((b - a)/2)*t) + (b + a)/2;
h2 = (weights[7])*((b - a)/2)*(f(g2));
t = -1*abscisass[7];
g3 = (((b - a)/2)*t) + (b + a)/2;
h3 = (weights[7])*((b - a)/2)*(f(g3));
t = abscisass[8];
g4 = (((b - a)/2)*t) + (b + a)/2;
h4 = (weights[8])*((b - a)/2)*(f(g4));
t = -1*abscisass[8];
g5 = (((b - a)/2)*t) + (b + a)/2;
h5 = (weights[8])*((b - a)/2)*(f(g5));
answer = h1 + h2 + h3 + h4 + h5;
}
double equation (double t)
{
return log(t) +4*t;
}
double err(double &answer, double &exactanswer, double &error)
{
error = fabs(((answer - exactanswer)/exactanswer)*100);
}
```