You are here:

C/Fortran Question about vectors

Advertisement


Question
I was wondering how to approach and code the following, i know its in Fortran i want it, but i just want to know is my algorithm correct, or am i taking the wrong approach.

"The dot product of two vectors V1 = Vx1i + Vy1j + Vz1k and V2 = Vx2i + Vy2j + Vz2k is a scalar quantity defined by the equation
V1.V2 = Vx1Vx2 + Vy1Vy2 + Vz1Vz2

The cross product of 2 vectors V1 = Vx1i + Vy1j + Vz1k and V2 = Vx2i + Vy2j + Vz2k is a vector quantity defined by the equation
V1xV2 = (Vy1Vx2 - Vy2Vx1)i + (Vz1Vx2 - Vz2Vx1)j + (Vz1Vy2 - Vy2Vx1)

Write a fortran program that will read in 2 vectors V1 and V2 into a 2 dimensional array, and then calculate the dot and cross products according to the equations above."

Is it enough to do the following? Prompt the user for the x y and z co-ordinates of V1 and V2, store them in a 2 x 3 dimensional array and then calculate the above by hardcoding the array positions in the equations.
What i mean is for example vector[1][1] would respresent Vx1
and vector[1][2] would represent Vx2.
Or do you know of another way that would be more efficient?  

Answer
Array indexing begins at 0.
So, Vx1 will be at V[0][0].

Regarding the calculation, what you have written seems to be correct.

But, to make sure that it is correct, do hand-simulation and see what output you are getting.

What I mean by hand-simulation is that, think that you are a computer and trace your program line by line....accept the input, store the values, make the calculations, print the result.
You must act as dumb as your computer for this.

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.