CS 112 Project 3: Vector Operations

Objectives:

1. Practice using pointers.
2. Build more vector operations.
3. Practice using the debugger (if necessary).

Introduction

This week's project is a team project to add more functionality to the Vec class we built in this week's lab. To illustrate, suppose that v1, v2, and v3 are Vec objects containing the following values:

`   v1 == {1, 2, 3}`
`   v2 == {2, 4, 6}`
`   v3 == {1, 2, 3}`
1. Subscript. The subscript operator ([i]) has two roles that are similar to the getItem() and setItem() methods we wrote in lab:
• One role is to retrieve the value at index i in our Vec:
`   Item it = v[i];`
just like the getItem(i) method we built in the lab exercise. Normally, the subscript operation does not perform bounds checking on the index value i, but yours should perform bounds checking. More precisely, your subscript operator should throw a range_error if it is passed a "bad" index value.
• The other role is to change the value at index i in our Vec:
`   v[i] = it;`
just like the setItem(i, it) method we built in the lab exercise. As with the other version, this subscript operation does not normally perform any bounds checking on the index value i, but yours should throw a range_error if it receives a bad index value.
To provide these two roles, you must define operator[] twice, once for each role. You should also write test-methods (similar to those used for getItem() and setItem()). See our in-class discussion (or search the web) for the details on how to make these definitions distinct from one another.
2. Inequality. The expression:
`   v1 != v2`
should return true. The expression:
`   v1 != v3`
should return false.
3. File I/O. The expression:
`   v3.readFrom(fileName);`
should fill v3 with values stored in fileName. You may assume that the first thing on the first line of fileName is an integer that specifies the number of values in the file, as shown in the test-files vecTest1.txt and. vecTest2.txt. Be careful that your method does not leak memory!

The expression:

`   v3.writeTo(fileName);`
should write all of the values in v3 to the file named by fileName, one per line. The first line of fileName should indicate the number of values in the file.

4. Vector Addition. The expression:
`   v3 = v1 + v2;`
should set v3 == {3, 6, 9}, without leaking memory.
5. Vector Subtraction. The expression:
`   v3 = v1 - v2;`
should set v3 == {-1, -2, -3}, without leaking memory.
6. Vector Dot Product. The expression:
`double dProd = v1 * v2;`
should set dProd == 28. For two vectors u = {u1, u2, ..., uN} and v = {v1, v2, ..., vN},
the dot product u * v == {u1*v1 + u2*v2 + ... + uN*vN}.

As indicated on the grade sheet, one member of your team is responsible for the !=, readFrom(), and + operations; the other team member should build the writeTo(), -, and * operations. See me for the teams.

The opening documentation in each file should clearly indicate who wrote which methods.

To make life easier for you, VecTester contains a test-method for each of these operations. Use these test-methods and test driven development to build your operations. If you get your program to compile, but it fails a test and you cannot figure out what is wrong, use the debugger! If necessary, draw a memory diagram and trace through the execution of the problematic function one statement at a time, until you identify the logic error.

Application. Vectors can be used to store positions and/or directional forces in a coordinate system. Vectors of length 2 can be used to store 2-dimensional (x, y) information; vectors of length 3 can be used to store 3-dimensional (x, y, z) information; and so on. When you have "finished" your Vec class, your time is to write a program that

1. allows the user to enter the number of dimensions in their space;
2. allows the user to enter a starting position in that space;
3. allows the user to enter an arbitrary number of vectors; and
4. outputs the starting point plus the sum of the vectors.
For example, if the user is in a 3-dimensional system, is starting at point v0 = {0, 0, 0}, and moves in the directions specified by these four vectors:

```   v1 = {1, 0, 0}
v2 = {0, 1, 0}
v3 = {0, 0, 1}
v3 = {1, 2, 3}```
the program should let the user enter these values and then display their sum {2, 3, 4}, which represents the user's final position within the 3-dimensional system.

Submit

• A paper copy of a script file in which you list, compile, and run your project, with this grade sheet stapled to the front.
• An electronic copy of your project, in your /home/cs/112/current/yourUserName/proj3 folder.

CS > 112 > Projects > 03

This page maintained by Joel Adams.