|
Due: 11pm, Wednesday, September 16
Worth: 50 points
Goals
|
|
Overview
Write a program in C++ that computes the dot product of two vectors.
Be sure to meet my requirements.
You must complete this individually - not as a pair.
But you may discuss it with each other.
Input and Output Specification
All input is from standard input, cin.
The first input is one integer representing the length of two vectors.
Both vectors are the same length.
That integer should be positive and no larger than 100.
If it is not, the program should print an error message and quit.
Otherwise, the program should read values of type double to fill the two vectors.
If there are not enough values, fill with zero.
(Hint: fill with zero when you create the vectors.
Use 0 as a second parameter to the constructor.)
Ignore any extra values.
After input, print a blank line.
Then, print each vector on one line with a label at the front.
Label each element with its index and have two spaces between elements.
Print all real numbers with one digit after the decimal point.
See below for a function to do that.
You have to write the code to print the index; it is not built-in.
An output line would look like:
v1: [0] 2.5 [1] 3.0 [2] 1.2
Then print the dot product of the two vectors. The dot product is the sum of the products of the elements with the same index. The vectors must be the same length. The summation formula above is another representation of the dot product. Label your answer.
Other Requirements
The file should have a .cpp ending.
The compiler takes other endings, but we'll use .cpp this term.
Use the C++ compiler in our Linux lab with the -Wall option. The program should compile cleanly with no errors or warnings.
You must use functions throughout this program. Pass the parameters appropriately. If a parameter shouldn't change, make sure it doesn't. Each function should do one thing well (have high cohesion).
You must use the vector class from the Standard Template Library. Use its member functions to get the size of the vector. It returns an unsigned (not int) value. Do not declare bigger vectors than you need. Vectors do not check whether you go outside their declared bounds.
Declare storage as locally as possible to complete the task. Do not declare global variables. You should, however, use the std namespace.
Use good programming style including a summary comment with your name at the top. Each function should have a short summary comment. That comment should be on the function prototype above the main function. The function implementation below main does not need a comment.
Output Digits
The easiest way I've found for setting how many digits to use after
the decimal point when printing real numbers is to use my
setOutputDigits function.
You'll need to put this function in your code
and then call it once before printing to set how many digits to use
throughout the rest of the execution.
Note that you can copy and paste this code from the web page.
// set output format and number of digits after decimal
void setOutputDigits(int digits) {
cout.precision(digits);
cout.setf(ios::fixed | ios::dec | ios::showpoint);
}