Starting from:

$45

CPE2600 Lab 5- Vector Calculator Solution

Lab 5 Vector Calculator
Introduction

The purpose of this assignment is to design, code, and test an interactive C program that can perform vector math. It is intended to familiarize you with using structures, arrays, and console input and output C.
Work on the assignment is to be done individually. You are welcome to collaborate with class members, but the project must be your own work.
Background and References

A vector is a quantity that represents both a magnitude and direction. It is used in many fields to represent different things. For example, a vector could be used to indicate a force applied to an object at a given angle.
Different mathematical transformations can be performed against vectors for various reasons. The goal of this assignment is for you to write a program that will perform different mathematical operations on one or more vectors.
There are a variety of ways to represent vectors. As mentioned, notation for vectors in a
For more detailed examples and explanations (in 2D) see:
• Introduction to Vectors – https://www.mathsisfun.com/algebra/vectors.html
Project Description

The goal of this project will be to create an interactive calculator for vectors. It will be reminiscent of a "mini-Matlab" program. Given our current skills, there will be come inherent limitations, but will be continue to learn techniques to overcome those limitations.
One limitation is that we will hardcode in that all vectors have three components. We will also only be able to handle a handful of vectors and implement just a few operations.
The following sample interaction will best communicate how the program will operate:
minimat> a = 1 2 3 a = 1 2 3 minimat> b = 4, 5, 6 b = 4 5 6 minimat> c = a + b
c = 5 7 9 minimat> d = c * 2 d = 10 14 18 minimat> a + d ans = 11 16 21 minimat> b b = 4 5 6
Not shown, there should be a command to quit the program. Also not shown, there should be a command (clear) to clear out the vector memory.
Consider the following mathematical operations that can be performed with vectors. Some of the descriptions below show 2D vectors, but remember that we will be doing all 3D vectors.
Add
Add two vectors together using the formula:
If A = (ai) vector of length n and B = (bi) is vector of length n, then the sum A + B is also a vector of length n such that A + B = (ai + bi)
The definition of vector addition indicates an element-by-element addition. The elements of vector A are added to the exact corresponding elements of vector B. To add A and B they must be the same dimensions.
For example:
A = (1, 4)
B = (1, 3)
A + B = (1 + 1, 4 + 3) = (2, 7)
Subtract
Subtract two vectors using the formula:
If A = (ai) vector of length n and B = (bi) is vector of length n, then the difference A - B is also a vector of length n such that A - B = (ai - bi)
The definition of vector subtraction indicates an element-by-element subtraction. The elements of vector A are subtracted by the exact corresponding elements of vector B. To subtract A and B they must be the same dimensions.
For example:
A = (1, 4)
B = (1, 3)
A - B = (1 - 1, 4 - 3) = (0, 1)
Vector Multiplication
Multiplying two vectors comes in two forms – dot product and cross product. This information is left here for completeness, but our program will not need to implement either of these operations right now.
Dot Product
The dot product results in a scalar value using the following formula:
If A = (ai) vector of length n and B = (bi) is vector of length n, then the dot product A · B is a scalar such that A · B = a1b1 + a2b2 + … + anbn
The definition of dot product indicates an element-by-element multiplication. The elements of vector A are multiplied by the exact corresponding elements of vector B
and all results are added together.
For example:
A = (1, 4)
B = (1, 3)
A · B = 1 * 1 + 4 * 3 = 13
Cross Product
The cross-product results in a vector that is perpendicular to each of the source vectors A and B. A cross product must be performed on vectors of 3 dimensions.
If A = (a1, a2, a3) and B = (b1, b2, b3), then the cross product A x B is a vector such that A x B = (c1, c2, c3) where:
c1 = a2 * b3 - a3 * b2 c2 = a3 * b1 - a1 * b3 c3 = a1 * b2 - a2 * b1
For example:
A = (1, 2, 3)
B = (4, 5, 6)
A x B = (2 * 6 - 3 * 5, 3 * 4 - 1 * 6, 1 * 5 - 2 * 4) = (-3, 6, -3)
Scalar Multiplication
Simple enough, scalar multiplication is between one vector and one non-vector (aka scalar) value. Each element of the vector is multiplied by the scalar.
For example:
A = (1, 2, 3)
A * 2.5 = (1*2.5, 2*2.5, 3*2.5)
Implementation Details

In summary, your task is to create a program that will perform limited vector math. The required operations are vector addition, vector subtraction, and scalar multiplication.
Dot product and cross product are detailed above, but are not required.
Your implementation must represent vectors internally in a struct. That struct must have a member for the vector's name and member(s) for it's three magnitudes. The name could be a reasonably size character array to hold a C-style string. Alternatively, you could limit the name to a single character. For the magnitudes, you should be using floating point math, so you should use type double - either three members or an array would work for this.
Your implementation must be able to store up to 10 individual vectors at the same time, but the names and values must be able to be added at runtime as per the sample interaction above. I suggest you create an array of your vector structs. As you create new vectors, you locate (hint - iterate the array) the first empty location and put the vector there. Once this array is filled, if a new array is attempted to be created, the program should refuse and print a memory full message to the user. If the user issues the clear command, the storage array will be cleared and new vectors can be made. I recommend roughly three layers for your implementation.
// vect is a typedef'ed struct vect add(vect a, vect b)
{
vect returnval; returnval.x = a.x + b.x;
...
return returnval;
}

The next layer would manage the storage array that holds the vector structs. Perhaps it implements some container-like functionality like:
• addvect(vect new) - add new vector to storage array. If same name exists, replace at that location, otherwise add to empty location. If array full, do nothing and return error code.
• findvect(char * name) - search array for name vector and return (a copy) to caller
• clear() - empty the storage array
• ?? maybe a few more...
Since you have several functions accessing the same array, it will be reasonable to make the array visible to all functions in this layer. We do not like global variables, so, what might work well?
And finally, the user interface layer. This layer would be responsible for parsing the commands from the user, calling the storage array functions to store and retrieve vectors, calling the operations layer to add, subtract, and multiply vectors, and display results to user and/or error messages. All input and output from and to the console should be in this layer.
Main should not really have a lot of code. We have a single command line option (see below). Excepting that, main should really just hand off control to your user interface.
As a whole, this sounds like a huge project, but it really is not. If you work from the simplest functions out to the user interface you will find much of the program is trivial. Be sure to test each function and layer as you go. Think unit testing.
Summary Requirements

1. In many ways, the user interaction will be similar to Matlab...
4. Exit Command: quit - this command will cause the program to exit gracefully.
6. Display: varname - typing just a variable name should display the current value of that vector to the console. If varname does not yet exist a message to that effect should be displayed.
11. Command: clear - empties all of the stored vectors.
12. Command: list - lists all of the stored vectors and their values.
14. OPTIONAL - implement the dot and cross products.

NOTE ON PARSING: Parsing user input is hard. Without the spaces, an expression like c=a+b might be challenging to parse. At this point in your skills, I am afraid you spending an inordinate amount of time on the problem, hence the update to allow your implementation to expect spaces. One suggestion made by a student would be to pre-format the string entered by the user to add the spaces if they were not present where expected, then use strtok or sscanf to parse individual elements of the expression. I do like that idea a lot, but again, doing something like this is optional and not required at this point. Parsing user input is hard.

ANOTHER NOTE ON PARSING: Be sure to consult the "string library" (that is, standard library functions in string.h, stdlib.h, etc). While not rich by any means, there are basic functions to locate characters in strings, etc. You should not be duplicating anything that could be accomplished with standard library functions.

Deliverables

More products