How do I find the cross product of two vectors in C++ using a function? The two vectors are two 1-D arrays of a class.
-
What i mean by vector here is a 1-d array of size length 3(not the vector container in CPP). Means float a[3],b[3],c[3]; Now suppose we want to store cross product of a and b into array c, how will we do it using a function? The function arguments (inputs) are a[3],b[3] and it should return (output) array c[3]. Can we have return value as an array?
-
Answer:
No, you cannot return an array. However, you can make a function that a reference to 3 arrays. The function would make three assignments. A function that accepts a single array of 5 ints would look like this: void cool( int (&runnings) [5]) Let's assume the arguments are arrays called a and b, and the output (also the last parameter to the function) is called c. The three equations would look similar to: c[0] = a[1] x b[2] - a[2] x b[1] I can't remember the exact order but every resultant component is the difference between the products of each of the two other components. Sounds fancy, but I'm sure you get what I mean. You could consider making your own vector class which will allow you to return that from a function. Also would result in clearer to understand code. Good luck.
Gregory Currie at Quora Visit the source
Other answers
I used class for doing this vectorsHere v1 and v2 were two vectors provided by user and cross_p is the resultant vector void crossproduct(vector v1,vector v2,vector cross_p){ cross_p.i = (v1.j*v2.k) - (v1.k*v2.j); cross_p.j = -((v1.i*v2.k) - (v1.k*v2.i)); cross_p.k = (v1.i*v2.j) - (v1.j*v2.i); cout<<"crossproduct = "<<cross_p.i<<"i"<<cross_p.j<<"j"<<cross_p.k<<"k"<<endl;}
Chinmay Shah
Related Q & A:
- How To Connect Two Different Network Segments Using A Switch And A Router?Best solution by Super User
- How do I find someones name by using just their email address?Best solution by Yahoo! Answers
- How can I promote a new product for Google?Best solution by Quora
- How do I find the model of my burton snowboard using the serial number?Best solution by Yahoo! Answers
- How do i find out how long it takes on the tube from London Marylebone to London Charing Cross?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.