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;
}

Recommended Answers

All 3 Replies

Try this :

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;
}

Change the prototype as well.

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!

...

You're calling a function that returns a double value but you aren't storing the return value anywhere.

You're not initializing any of your double variables to 0 so they are all storing "garbage" to begin with. Additionallly, you are not putting a value into it before you use it as an argument to your function call.

I suspect it's your intent to store the return value to norm_1. Problem is that on line 38 you forgot to set it equal to the return value of the call to norms() so the "garbage" never gets cleared.

If that is in fact your intent, I would suggest declaring norms() as a void function and make the norm_1 argument/parameter a reference parameter. Then you don't even have to worry about the return value, just add a couple ampersands/reference operators ("&").

EDIT:
oops, looks like firstPerson beat me to it...

Thank you both so much for the replies, very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.