Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone tell me what wrong with the C++ code below #include <iostream> #inclu

ID: 3767551 • Letter: C

Question

Can anyone tell me what wrong with the C++ code below

#include <iostream>
#include <vector>
#include <math.h>
int main()
using namespace std;
#define m 4
template<class T>
< T> tri(int n, vector<T> a,vector<T> b,vector<T> c, vector<T> u, vector<T>& v)
{
/* b :subdiagonal
* a :diagonal
* c :super diagonal
* v :column vector elements
* u :column vector unknown
* n :dimension of the matrix(n*n matrix)
*/
T xmult;
for(int i=2;i<n;i++)
{
xmult = b[i-1]/a[i-1];
a[i]-=xmult*c[i-1];
v[i]-=xmult*v[i-1];
}
u[n]=v[n]/a[n];
for(i=n-1; i<=1; i--)
u[i]=(v[i]-c[i]*u[i+1])/a[i];
}
< T> main()
{
for (i = 1; i <= m; i++)
{
a[i] = 0.5;
b[i] = 3.0;
c[i] = 0.5;
v[i] = 2.0;
}
tri (m, a, b, c, u, v);
return 0;
}

Explanation / Answer


#include <iostream>
#include <vector>
#include <math.h>
#define m 4
using namespace std;
int main()
template<class T>
<T> tri(int n, vector<T> a,vector<T> b,vector<T> c, vector<T> u, vector<T>& v)
{
/* b :subdiagonal
* a :diagonal
* c :super diagonal
* v :column vector elements
* u :column vector unknown
* n :dimension of the matrix(n*n matrix)
*/
T xmult;
for(int i=2;i<n;i++)
{
xmult = b[i-1]/a[i-1];
a[i]-=xmult*c[i-1];
v[i]-=xmult*v[i-1];
}
u[n]=v[n]/a[n];
for(i=n-1; i<=1; i--)
u[i]=(v[i]-c[i]*u[i+1])/a[i];
}
template<class T>
<T> main()
{
for (i = 1; i <= m; i++)
{
a[i] = 0.5;
b[i] = 3.0;
c[i] = 0.5;
v[i] = 2.0;
}
tri (m, a, b, c, u, v);
return 0;
}