Hey, so this term I'm taking C/C++ course, and I'm tasked with some exercises in C programming.
Here is one:
Write a program that reads in two vectors and calculates the inner product between them, as follows:
- Read in the length of the vectors (an integer followed by a newline).
- Read in the two vectors (floats separated by spaces, newline separating the vectors).
- The inner (scaler) product is the sum of the multiplication of each two elements:
- If the vectors are [1, 0.5, 0] and [0.2, 1, 0.3] the inner product is: 1 * 0.2 + 0.5 * 1 + 0 * 0.3 = 0.7.
- Print the result up to 2 digits after the decimal point (hint: "%.2f").
You may assume the input is valid, but you may not assume a maximum length.
For example:
Input Result 3
1 0.5 0
0.2 1 0.30.70 0 0.00
I thought first to input the lengths of both the vectors and then define the vectors in the suitable lengths (by pointers).
But my problem is with the input of the coordinates - as it's in one row, so I don't know how I can insert efficiently and elegantly floats directly to the arrays.
Does someone have an idea how can I input the coordinates correctly?
Thank in advance!