Hi,
I've tried to make a program that inputs two 3D vectors and then calculates various norms of the first vector, and the dot product and addition of both vectors.
My code is working fine when everything is put into the main, however I wanted to create some functions to improve it. I've started doing this for the norm calculation, however the wrong value, 'norm_1' is being given and I can't figure out why...can anyone please explain?
Thanks in advance!
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
double norms(double vectorA_xcomponent, double vectorA_ycomponent, double vectorA_zcomponent, double norm_1);
int main()
{
double vectorA[3], vectorB[3], vectorA_xcomponent, vectorA_ycomponent, vectorA_zcomponent, vectorB_xcomponent, vectorB_ycomponent, vectorB_zcomponent, vectorC_xcomponent, vectorC_ycomponent, vectorC_zcomponent, norm_1, norm_2, dot_product;
int i;
// Get input vectors from user.
cout << "Enter elements of first vector: " << endl;
for(i=0;i<3;i++)
{
cin >> vectorA[i];
}
vectorA_xcomponent = vectorA[0];
vectorA_ycomponent = vectorA[1];
vectorA_zcomponent = vectorA[2];
cout << "Enter elements of second vector: " << endl;
for(i=0;i<3;i++)
{
cin >> vectorB[i];
}
vectorB_xcomponent = vectorB[0];
vectorB_ycomponent = vectorB[1];
vectorB_zcomponent = vectorB[2];
// Calculates norms
norms(vectorA_xcomponent, vectorA_ycomponent, vectorA_zcomponent, norm_1);
norm_2 = sqrt(vectorA_xcomponent*vectorA_xcomponent+vectorA_ycomponent*vectorA_ycomponent+vectorA_zcomponent*vectorA_zcomponent);
// Adds 2 vectors.
vectorC_xcomponent = (vectorA_xcomponent + vectorB_xcomponent);
vectorC_ycomponent = (vectorA_ycomponent + vectorB_ycomponent);
vectorC_zcomponent = (vectorA_zcomponent + vectorB_zcomponent);
// Dot product on 2 vectors
dot_product = (vectorA_xcomponent*vectorB_xcomponent+vectorA_ycomponent*vectorB_ycomponent+vectorA_zcomponent*vectorB_zcomponent);
// Output result.
cout << "The l_1 norm of the first vector is " << norm_1 << endl;
cout << "The l_2 norm of the first vector is " << norm_2 << endl;
cout << "The l_inf norm of the first vector is " << *max_element(vectorA,vectorA+3) << endl;
cout << "The addition of the two vectors is " << vectorC_xcomponent <<" "<< vectorC_ycomponent <<" "<< vectorC_zcomponent << endl;
cout << "The dot product of the two vectors is " << dot_product << endl;
return 0;
}
double norms(double vectorA_xcomponent, double vectorA_ycomponent, double vectorA_zcomponent, double norm_1)
{
norm_1 = (vectorA_xcomponent+vectorA_ycomponent+vectorA_zcomponent);
return norm_1;
}