Okay, so, here's the assignment that I have to complete:

Write a program that will input two vectors of information. Each vector will contain exactly 8 floating point values. Your program will enter these values from standard input. Once the values have been read in, your program should call a function that will compute the inner product of these two vectors. The function should return the value back to the main program. The main program will then print out the value. You should not print the value out inside the inner product function.

The inner product of two vectors is a single number obtained by multiplying corresponding elements and then adding up their sums. For example, if vector u = (5, 1, 6, 2) and vector v = (1, 2, 3, 4) then the inner product is 33 because

(5 * 1) = 5
(1 * 2) = 2
(6 * 3) = 18
(2 * 4) = 8

and 5 + 2 + 18 + 8 is 33.

Your C program will call a function called inner that has the following interface:

float inner ( float u[] , float v[], int size ) ;

Do not hardwire the loop inside inner to go from 0 to 7. It should go from 0 to size-1.

Now, I'm not asking you to solve this for me. I'd just like to know how to start using vectors. Could anyone give me a small code snippet/example?

Recommended Answers

All 5 Replies

A c++ vector is just the c++ implementation of a normal array. So when the instructions tell you there are exactly 8 floating point values in the vector, that means declare an array like this: float values[8]; If you are confused about how to use arrays then read one of these tutorials

Wait, so you just make the array a float instead of an integer? That's it?

Yup. That's all there is to it. Read the instructions carefully. I assume you will need two of those arrays.
>>Each vector will contain exactly 8 floating point values

Oh, wow, thanks. You just saved me quite a few hours of sleep!

Be a part of the DaniWeb community

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