Assignment 4: Vectors

We want to create a large, menu-driven vector manipulation program, where a vector is simply the mathematical term for an array. We want to include operations such as adding, subtracting, and multiplication of two vectors, finding the sum, product, and dot-product of a vector, and so on.

Since this is going to be a large project, we will do it in groups. Each group will get some well-defined tasks (i.e. functions) that they will have to solve. Then, all functions will be combined into the complete project via #include statements.

The functions we need to declare  to handle vectors (or arrays) are grouped into several files, and are declared as follows. More details about these functions can be found at the end.
 

Group 1:

Filename: 

vectio.cpp

Functions:

void getVector(double[], int);
void display(double[], int);
void wait(void);
void clearScreen(void);

Group 2:

Filename:

vectalg.cpp

Functions:

void add(double[], double[], double[], int);
void sub(double[], double[], double[], int);
void mult(double[], double[], double[], int);
void addNum(double[], double c, double[], int);
void subNum(double[], double c, double[], int);
void multNum(double[], double c, double[], int);
void reverse(double[], double[], int);

 

Group 3:

Filename:

vectscal.cpp

Functions:

double sum(double[], int);
double product(double[], int);
double dot(double[], double[], int);

Group 4:

Filename:

vectmisc.cpp

Functions:

double max(double[], int);
double min(double[], int);
void swap(double[], int, int);

void sort(double[], double[], int);

 

The way we will attack this large project is as follows: we will form teams of people, each team consisting of 3 people. Each team has to create all functions in group 2, group 3, and group 4. They have to test all functions and save the functions, exactly as defined, in the filenames for the groups, exactly as defined, on their kitten account. I will - actually, I already have - create the functions in group 1. You must follow the exact definition and spelling of the functions and of the file names !

I will also create a menu program that will use the functions you have defined for a complete program via appropriate #include statements, and "give" each group the appropriate files so that they can each have their own, complete program.

While you are testing your program, you can already use the functions defined in group 1 (which I did). You will, however, not get the complete menu program until the assignment is finished.

To use my functions for testing your own functions, you could proceed as follows:

On MathSci:

You must be logged in to the MathSci account, using a floppy, if necessary, if your account is not on MathSci. Suppose you are working on the function "add", which is to add two arrays, or vectors, into a third one. Your code, while working on the function, would look as follows:

#include <iostream.h>
// include the files from group 1 as follows:
#include "f:\prof\wachsmbe\vector\vectio.cpp"

// next, define the function you are working on, say "add"
void add(double A[], double B[], double C[], int N)
{
   // some code goes here
}

// finally, create a small main function to test your function
int main(void)
{
   // Define a constant for the length of the vectors
   const int MAX = 5;

   // define the three vectors needed in the program
   double A[MAX], B[MAX], C[MAX];

   // use my function to input the vector
   getVector(A);
   getVector(B);

   // test your own function
   add(A,B,C,MAX);

   // use my function to display the result (which should be A + B)
   display(C, MAX);
  
   return 0;
}

On Kitten:

You can also work on your kitten account to develop the functions. Your code, while testing, would look exactly like above, with one difference:

Instead of

#include "f:\prof\wachsmbe\vector\vectio.cpp"

you would use the line

#include "/export/home/faculty/wachmut/vector/vectio.cpp"

Once your function "add" is tested, you would move only the code for that function into the file "vectalg.cpp" and proceed to test the next function. Once that works, add that code to the file "vectalg.cpp". Keep doing this until all functions necessary for group 1 are defined.

Exact Specifications of all Functions:

void add(double A[], double B[], double C[], int N);

input:

A, B: two arrays of the same size
N: the size of all arrays

output:

C: an array where each element is the sum of the corresponding elements of A and B

void sub(double A[], double B[], double C[], int N);

input:

A, B: two arrays of the same size
N: the size of all arrays

output:

C: an array where each element is the difference of the corresponding elements of A and B

void mult(double A[], double B[], double C[], int N);

input:

A, B: two arrays of the same size
N: the size of all arrays

output:

C: an array where each element is the product of the corresponding elements of A and B

void reverse(double A[], double B[], int N);

input:

A = the array to be reversed
N = the size of the array

output:

B is the array A in reverse order

void addNum(double A[], double c, double C[], int N);

input:

A: an array of doubles
c: a double number
N: the size of all arrays

output:

C: an array where each element is the sum of the corresponding element from A and the number c

void subNum(double A[], double c, double C[], int N);

input:

A: an array of doubles
c: a double number
N: the size of all arrays

output:

C: an array where each element is the difference of the corresponding element from A and the number c.

void multNum(double A[], double c, double C[], int N);

input:

A: an array of doubles
c: a double number
N: the size of all arrays

output:

C: an array where each element is the product of the corresponding element from A and the number c.

 void getVector(double A[], int N);

input:

A: a declared, but undefined array
N: the size of the array

output:

A: will contain values that the user has entered.

void display(double A[], int N);

input:

A: an array
N: the size of the array

output:

None, but prints the values of the array on the screen.

void wait(void);

input:

none

output:

None, but waits for the user to press the RETURN key

void clearScreen(void);

input:

none

output:

None, but clears the screen by printing 25 empty lines

double max(double A[], int N);

input:

A: an array
N: the size of the array

output:

returns the largest value of the array

double min(double A[], int N);

input:

A: an array
N: the size of the array

output:

returns the smallest value of the array

void swap(double A[], int pos1, int pos2);

input:

A: an array
pos1, pos2: two integers between 0 and the size of the array

output:

None, but the elements at pos1 and pos2 will have switched places with eachother.

void sortInc(double A[], double C[], int N);

input:

A: an array
N: the size of the array

output:

C: will be the same as array A, but sorted in increasing order of magnitude

void sortDec(double A[], double C[], int N);

input:

A: an array
N: the size of the array

output:

C: will be the same as array A, but sorted in decreasing order of magnitude

double sum(double A[], int N);

input:

A: an array
N: the size of the array

output:

returns the sum of all elements in the array A

double product(double A[], int N);

input:

A: an array
N: the size of the array

output:

returns the product of all elements in the array A

double dot(double A[], double B[], int N);

input:

A, B: two arrays of the same size
N: the size of the arrays

output:

returns the dot product of the two arrays. The dot product is obtained by first multiplying the corresponding entries of A and B together, and then computing the sum of those products (see class notes for details).