I'm learning c++ at university and I need to make a program that would calculate the dot product of two vectors. So I need to :
-ask the size of 2 vector <double>;
-ask the value of each table[n], with n as the size of the vector;
-put the values in the vector;
-calculate the dot product;
-return the dot product.
I've written something but I really don't know if it's ok or not, the thing is that when I try to run it, I seem to have a problem putting the values in my dot product function :(
#include <iostream>
#include <cmath>
#include <vector>
usingnamespace std;
double scalaire(vector <double> u, vector <double> v){ //scalaire is for produit
int nu(-1); // nu: size of vector 1 scalaire : dot product
int nv(-1); // nv : size of vector 2, both sizes will be changed below
while(nu<0){
cout<<"Type in the size of vector 1"<<endl;
cin>>nu;
}
while(nv<0){
cout<<"Type in the size of vector 2"<<endl;
cin>>nv;
}
int nuf(1); //nvf & nuf are just integers for the input of the values
int nvf(1); //of the 2 vectors
double u1; //u1 : value of input component of vector u below
while(nuf<=nu){
cout<<"Add a value for the value"<< nuf<<" of vector 1"<<endl;
cin>>u1;
u.push_back(u1);
nuf++;
}
double v1; //v1:value of input component of vector v below
while(nvf<=nv){
cout<<"Add a value for the value"<< nvf<<" of vector 2"<<endl;
cin>>v1;
u.push_back(v1);
nvf++;
}
int nbr(1);
double a;
double b(0);
while(nbr<=nu&&nbr<=nv){
a = u[nbr-1]*v[nbr-1]+b;
b = a;
nbr++;
}
return b;
} //my function seems ok to me but tell me if you find
//anything suspicious :)
int main()
{
scalaire(); //lost, don't know what to put in my function ...
return 0;
}
I've added some commentaries to make it easier to understand (it was the intention at least, i don't know if it really helps.), but don't hesitate to ask if you need more information.
I'm learning c++ at university and I need to make a program that would calculate the dot product of two vectors.
can we establish (cuz i'm a bit slow sorry) if you need to use c++ vectors to work out your MATHS vectors? I don't see anything in there that deals with the mathematical dot product.
To be honest i think you're getting confused between the two.
I don't know if you understand French but that's what I need to do : (skip it if you don't)
Écrivez un programme scalaire.cc qui calcule le produit scalaire de deux vecteurs, implémenté au moyen de tableaux dynamiques.
Votre programme devra utiliser (entre autres) les éléments suivants :
-deux variables de type tableau dynamique de réels ;
-une fonction qui calcule le produit scalaire : double scalaire(vector<double> u, vector<double> v);
Méthode :
-demander à l'utilisateur d'entrer n, la taille effective des vecteurs.
-vérifier que n est compris entre 1 et N_MAX (et demander à l'utilisateur d'entrer à nouveau une valeur tant que ce n'est pas le cas).
-demander à l'utilisateur les composantes (v1 0... v1n-1 , v2 0 ... v2n-1) des vecteurs v1 et v2.
-appeler la fonction scalaire(...) pour calculer le produit scalaire de v1 et v2.
-afficher le résultat.
So the programming teacher (this isn't my main course, I study physics but I must have this lesson too, I think it is kinda basic) wants us to make 2 vectors, and these vectors can have n dimensions, and we must make a function called : double scalaire(vector<double> u, vector<double> v);
And there I need to ask the user to give the program the size of each vector, to type in some values for my vectors (u(a,b,c,...,n) where n is the size of my vector), then I need to do the dot product of the two vectors. I think that's all.
Hope it helps, don't hesitate to ask for more information if needed :)
I need to ask the user to give the program the size of each vector
Is this:
The length of a mathematical vector (in other words the magnitude)?
OR
The number of elements a C++ vector contains?
It looks like the latter, but why ask a user (who might not know anything about a c++ vector), how many elements they want it to be? Perhaps "how many dimensions do the (maths) vectors have?" might be more relevant?
Well, the number of elements of my c++ vector is the same as the dimension of my mathematical vector, so it was the same in my mind ^^ i translated it really quick at the same time as I was adding the comments in the code, didn't bother to make it user-friendly.
The aim is that my program would then calculate (with the math vectors u and v with n and m dimensions) like that : u1*v1 + u2*v2 + ... +un*vn= something. I think that's what I have written in my code in the last part of the "double scalaire" function : the dot product.
1. in your main(), create your two input vectors.
2. Copy lines 7 to 32 into your main(), so that all of the input questions are in your main.
3. Then you call your scalaire(.... , ...) function, passing in the two vectors you have created based on the user input.
4. are lines 33 to 40 the actual dot product calculation? if so this is the only thing that should be in your function.
The aim was to put everything in one function only (including the ask the user part), but I think i'll just need to write another function that will ask the user for the required information (ie vector dimension and components) and then in my int main i'll put the ask function and the dot product function so i'll just have the functions in my main without too much code. Well, unless if it's possible to put everything into the scalaire function!
I finally got it !
Btw, my code was all right ? No error ? It would really be the first time ^^ (in 4 days of learning :p )
Oh yeah i get it, but what should be in my function (scalaire (here,here)) ?
For the second part : SADNESS, it was about the only part where i was pretty sure it was right x)
Actually i'm surprised, i really think it is right, with nbr =1 and b=0; it gives us: a = u[0]*v[0], b = u[0]*v[0], then {a = u[1]*v[1] + u[0]*v[0] }= b, then again and again until nbr is equal to the smallest dimension if the 2 vectors where it would calculate one last time and stop.
I tried it like that on codepad.org (i'm on my iphone at the moment)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cmath>
usingnamespace std;
int main(){
int nbr,b,nu,nv,a;
nbr =1;
b=0;
nu=nv=5;
while(nbr<=nu&&nbr<=nv){
a = (nbr)*(nbr)+b;
b = a;
nbr++;
}
cout<<b;
return 0;
}
Gave me 55 so it worked, why wouldn't it work in my function ? :(
Thanks for helping :) you don't need to answer right away if you're working >.>
while(nbr<=nu&&nbr<=nv){
a = u[nbr-1]*v[nbr-1]+b;
b = a;
nbr++;
}
is untidy, unreadable and difficult too debug. Just iterate over the size of the dimension you're working in, find the product of both corresponding elements, and keep a running sum, like i did for you here:
1 2 3 4 5 6 7 8 9
int nDimensions = u.size();
double sum = 0.0;
for(int i=0; i< nDimensions;++i)
{
sum += u.at(i) * v.at(i);
}
return sum;
Well, my formula is much more fun, while yours is just the usual formula ;)
Just kidding, why is my formula unreadable? I'll use yours if you convince me mine is bad :) i'm trying to learn only essential things that allow me to do tons of things without knowing too much, that why i use almost only while and never for, and only rarely if.
Well, 4 days of learning but I've worked hard as I have an exam thursday and I've missed all my programmation classes up to now :| btw thanks for your comment :D
Booradley60, thank you for your comment, i'll try to change my code as soon as i get home!
- your variables have horrible nondescript names (nbr, nu etc)
- you are creating more than you need (why create an 'a', just to immediately assign it to 'b'?)
- and: while(nbr<=nu&&nbr<=nv)
do you honestly think this is nice code to try and read and understand?
Well, i forgot to rename them for other people :) nbr for number (just needed to start at 1), nu for number of elements in u/v, they all meant something to me !
I love the WHILE!!! :D
This very line may seem hard to read for those who didn't write it indeed ^^
Thanks for everything!!
Ps: sorry for the delay, my iphone just turned off at 26% battery when i was outside (i wonder whether it is because it was freezing cold and my phone was always out of my pocket)
You're definitely on the right track. Here are some pointers for writing better code.
- rather than having one large function, it's often better to have a few smaller functions that do independent things. In this case, you might want to prompt the user for the size N. Call a function that prompts the user to enter N values into a vector. Pass the vector as a parameter. Then call the same function again to populate the other vector. Now call scalaire() so it just computes the dot product - it doesn't prompt for the values.
The advantage of doing it this way is that you can reuse the code. Suppose the next assignment is to compute the cross product of two vectors. You already have the code that prompts to populate the vectors so you can just reuse that. You would just have to write code to compute the cross product of two vectors that already contained values.
Here are my functions, I've tried to make them easy to understand :
m : name of vector
n : dimension of vector
nbr : integer used in the pushback function
nber : integer used in the scalaire (dot product) function
a,b : doubles used in the scalaire (dot product) function
//first function : vector name
int vect_nbr(int s){ //give an integer as vector name, can s not be used ? the vector would then be "vector 1"
int m(-1); //if the used choses 1 as the vector name... still confused about the s, can i put m instead ?
while(m<0){ //then type m = -1; and not change the rest ? the m would be initialized by the function
cout<<"type in an integer as vector name (so you don't forget it)"<<endl;
cin>>m;
}
return m;
}
//second function : vector dimension
int askdim(int m){ //m is the name of the vector as chosen above, you chose the vector dimension in this
int n(-1); //function
while(n<0){
cout<<"add the dimension of the vector "<<m<<endl;
cin<<n;
}
return n;
}
//third function : vector values (with vect.push_back)
void pushback(vector<double> u, int n, int m){ //the void was a bit random, i think it's because this function
int nbr(1); //doesn't return anything, right ? n is the vector dimension as above, m the vector's name
double u1(0); //nbr is just an integer used as repair. u1 is the name for the input, could be anything else
while(nbr<=n){
cout<<"add a value for the component "<<nbr<<" of the vector "<<m<<endl;
cin>>u1;
u.push_back(u1);
nbr++;
}
cout<<"All components of this vector have been added!"<<endl;
}
//fourth function : dot product, as i need 2 vectors, the dimensions of above have been called nu and nv here
double scalaire(vector <double> u, vector <double> v, int nu, int nv){
int nber(1); //nber is an integer with the same role as nbr's in the pushback function
double a; //a and b are just doubles that I use for my dot product, I use them as repairs
double b(0);
while(nber<=nu&&nber<=nv){
a = u[nber-1]*v[nber-1]+b;
b = a;
nber++;
}
return b;
}
Now I think I just need to put my functions in my int main() :)
If anything is wrong, uneasy to understand, please tell me ! I'll try my best to explain it to you (I think it was longer to write the comments than the code...). The only function in which I have a doubt is the first one for the vector name, I need this one because otherwise the user can't know which vector he is inputting. It's good that all the functions are separate because in the next exercise I need to do a matrix multiplication, so I could re-use/modify these ones :D
What you have there may be overkill. I was thinking more along the lines of:
1. A function that creates a vector. It takes no arguments, and returns a vector of doubles. Inside the function, you ask the user for a dimension and fill the vector based on its dimension.
2. A function that calculates dot product. It takes two vectors of double and returns a double
3. main. It calls the function from 1. twice in order to populate two vectors. It then passes these vectors to function 2. You may also have a variable here to store the return value from function 2.