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 adds 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: To provide these two roles, you must define operator[] twice, once for each role. See our in-class discussion for the details on how to make these definitions distinct from one another.
  2. Vector Addition. The expression:
     v3 = v1 + v2;
    should set v3 == {3, 6, 9}, without leaking memory.
  3. Vector Subtraction. The expression:
     v3 = v1 - v2;
    should set v3 == {-1, -2, -3}, without leaking memory.

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

Congratulations! You have been named the navigation officer on the starship U.S.S. Boobyprize, whose hyper-spatial engines allow the ship and its crew to explore the universe. Current theories suggest that reality consists of 11 or more dimensions, so you need a program to help you navigate through an N-dimensional space.

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 task is to write a program that:

  1. Prompts for and has the user enter the number of dimensions in their space, storing this number in a variable N, which it can then use to define vector objects.
  2. Prompts for and has the user enter a starting position in that space, and stores that position in a vector object sum.
  3. Uses a loop that:
    1. prompts for and has the user enter a position (relative to their current position), and stores that position in a vector,
    2. adds that vector to the sum vector.
    This loop should let the user enter an arbitrary number of positions.
  4. Outputs the starting position plus the final position (i.e., the sum of the positions from the accumulator-vector).
For example, if the user is in a 3-dimensional system, enters the starting point {0, 0, 0}, and then enters the following four 3-dimensional relative-positions:

   {1, 0, 0}
   {0, 1, 0}
   {0, 0, 1}
   {1, 2, 3}
the program should then display the starting position ({0,0,0}) and the sum from the accumulator ({2, 3, 4}), which is the user's final position within the 3-dimensional system.

As another example, if the user is navigating through a 5-dimensional space, enters {1, 0, 1, 0, 1} as the starting point, and enters three 5-dimensional relative-positions:

   {0, 1, 0, 1, 0}
   {2, 2, 2, 2, 2}
   {-3, -3, -3, -3, -3}
the program should then display the starting position and the final position ({0, 0, 0, 0, 0}).

You may (if you wish) have your program display the starship's new position each time it is updated, rather than just displaying the final position at the end.

Your application should have the user enter the vectors in the format expected by your readFrom(istream&) method: numbers separated by whitespace. (It does not have to parse through curly-brace and comma characters.) Likewise, your application can display the starship's position using the format provided by your writeTo(ostream&) method -- numbers separated by spaces.

To get you well on your way, download App.cpp and App.h. These files provide most of what you have to implement to finish this project.

Preparing to Submit

Create a script file which shows a build of your program -- to demonstrate that it is free of syntax errors, and then a run of your project to demonstrate that it works correctly.

Our grader will grade your submission based on the categories given in this grade sheet, so you should make certain all of those categories are covered.

Submit

Grading Rubric

Passes tests 50
operator[] (write version) 5
operator[] (read version) 5
operator+ 5
operator- 5
Application 25
Style/Self-documentation 5
Total 100

CS > 112 > Projects > 03


This page maintained by Joel Adams.