I'm trying to implement Gram Schmidt in code for a school project and I have a problem.It outputs a weird number and I don't know why.The rest of them are good but this one is bad.Sorry if the post is bad but it's my first post here and I really need help.Thanks
#include <iostream>
#define dim 100
using namespace std;
void inputMatrix(int *n, int *m, double x[dim][dim]){
int i,j;
for(i = 0; i < *n; i++){
cout << "V" << i << ":";
for(j = 0; j < *m; j++)
cin >> x[i][j];
}
}
void outputMatrix(int *n, int *m, double x[dim][dim]){
int i,j;
for(i=0;i<*n;i++){
for(j=0;j<*m;j++)
cout<<x[i][j]<<" ";
cout<<endl;
}
}
void initialize(int *m,double v[dim]){
int i;
for(i=0;i<*m;i++){
v[i]=0;
}
}
int main(){
double v[dim][dim], f[dim][dim], e[dim][dim],p1,p2,a[dim];
int n,m,i,j,z;
cout << "Introduceti numarul de vectori: ";cin >> n;
cout << "Introduceti numarul de elemente: ";cin >> m;
inputMatrix(&n,&m,v);
double div = 0;
for(i = 0; i < m; i++){
f[0][i] = v[0][i];
}
outputMatrix(&n,&m,v);
cout << endl;
outputMatrix(&n,&m,f);
for(i = 1;i < n; i++){
z = 0;
initialize(&m,a);
mk1:
p1 = 0;
p2 = 0;
for(j = 0; j < m; j++){
p1 = f[z][j] * v[i][j] + p1;
p2 = f[z][j] * f[z][j] + p2;
}
div = p1 / p2;
for(j = 0; j < m; j++){
a[j] = f[z][j] * div + a[j];
}
z++;
if( z < i){
goto mk1;
}
else{
for(j = 0; j < m;j++){
f[i][j] = v[i][j] - a[j];
}
}
cout << endl;
outputMatrix(&n,&m,f);
return 0;
}
Output:
Introduceti numarul de vectori: 3
Introduceti numarul de elemente: 4
V0:1
2
3
0
V1:1
2
0
0
V2:1
0
0
1
1 2 3 0
1 2 0 0
1 0 0 1
1 2 3 0
0 0 0 0
0 0 0 0
1 2 3 0
0.642857 1.28571 -1.07143 0
0.8 -0.4 5.55112e-17 1
I don't understand why it outputs this "5.55112e-17" Thanks for help!