Description
In this assignment, you are going to implement classes for two mathematical expressions:
Polynomials and Vectors(do not confuse this with std::vector). These classes will hold
necessary information to represent the data, and will also support some of the mathematical operations of these expressions: In Polynomial class, you will implement polynomial
addition and multiplication and in Vector class, you will implement vector addition, scalar
multiplication and dot product. You will also implement a user interface for users to perform
operations with Polynomials and Vectors. More details are given below.
As a reminder, following section gives basic explanations of polynomials and vectors, and
how to perform the operations.
Polynomials and Vectors
A polynomial is an expression in the form of
anx
n + an−1x
n−1 + …a2x
2 + a1x
1 + a0 (1)
where x is a variable and ak is a real number and called the coefficient of x
k
. Such polynomial is called “n-th” degree polynomial. For example: 2x
3 + x
2 + 5x + 12 is a third degree
polynomial. Coefficients can be zero, so x
4 + 2x is also a polynomial and degree of it is four.
Polynomial addition is an operation between two polynomials and is performed by adding
the coefficients of the variables with the same exponent. An example: (x
3 + 2x
2 + 3x + 4) +
(−2x
3 + 2x − 2) = −x
3 + 2x
2 + 5x + 2
Polynomial multiplication is performed by multiplying every pair of terms between two
polynomials, such that axk
.bxl = (a+b)x
k+l
. An example: (x
2+2x+3).(x−1) = x
3+x
2+x−3.
The definition of a vector is given as “an object that has a magnitude and direction”. In this
assignment, we are only working with real numbers, so in the sense of list representation, we
can represent a vector in an n-dimensional vector space with n real numbers, each number
showing the length of vector in one axis. For example, in a 4-dimensional vector space, a
vector can be represented as (a1, a2, a3, a4), where ak is a real number.
Vector Addition can be performed with two vectors in a vector space. Since the vectors
must be in the same vector space, we can not perform vector addition with different sized
vectors(we can extend the vectors but we will assume that it can not be performed in this
homework). For example: (2, 3, 1) + (1, 5, 11) = (3, 8, 12). (3, 4, 2) + (1, 2, 3, 4) can not be
performed, since their size is not equal.
2
Homework 1
Scalar multiplication of a vector and a scalar is performed by multiplying all elements of
a vector with a scalar such that (a1, a2, …, an)xc = (ca1, ca2, …, can) where c is a real number.
Dot product between two vectors is performed by multiplying the elements that represents
the same dimension and summing them together such that (a1, a2, …, an).(b1, b2, …, bn) =
a1b1 + a2b2 + … + anbn. For example: (1, 2, 3).(2, 1, 3) = 2 + 2 + 9 = 13.
You can find more sources on the internet if you are not familiar with concepts above.
Implementation Details
As explained, you will implement 2 classes and a user interface. First of all you should
implement Vector and Polynomial classes in their respective header files, “Vector.h” and
Polynomial.h. In these 2 classes, following should be included:
• In the Vector class, size of the vector and the value array should be maintained.
Similarly in Polynomial class, degree and values should be maintained.
• Both of the classes should have a constructor that initializes objects with given values. They should also have a copy constructor. If your class uses arrays, your copy
constructor should allocate new memory for arrays in the new objects.
• You need to use operator overloading feature to implement the operations. + operation
should perform vector addition in Vector class and polynomial addition in Polynomial
class, while ∗ operation should perform multiplication operations. Note that Vector
has two forms of multiplication: scalar multiplication and dot product. You need to
overload ∗ operation such that it performs scalar multiplication when given input is
integer, and dot product when given input is a Vector. Also do not forget that, for dot
product and vector addition operations, size of the vectors must be the same!
• To print both classes, use operator overloading to overload “<<” operator. Make sure
to print the object in a suitable format(Example is given in the “Screen Outputs”
section). Make sure that terms with higher degrees are printed out first and the terms
with zero coefficient are not printed when printing Polynomial objects. If a term has
a coefficient of 1, in that case coefficient should not be printed(Not 1x
2 but x
2
).
• For both of the classes, you should also implement necessary getter methods as well.
You should not need setter methods for this assignment, but if you find them necessary
for your implementation, you can implement them as well (However, you can NOT use
setter methods to initialize values, that is constructor’s job).
In your main program, you will maintain an array for Polynomial objects and an array for
Vector objects, both of which will be initialized with the values read from files. You will
3
be given 2 text files, one is called Polynomial.txt and the other one is called Vector.txt.
Both of these files’ first line indicate the number of objects they consist. Following lines give
the attributes of the objects. For polynomials, in each line, first number specifies degree of
polynomial, and the following numbers specify the coefficients in decreasing order. Similarly,
in vector file, first number specifies size of vector, and the following numbers specify the
elements of vector. For example, for polynomials, the line 3 2 3 1 4 represents the polynomial:
2x
3 + 3x
2 + x + 4, and for vectors, the line 3 1 10 2 represents the vector: (1, 10, 2). Here is
a screenshot of Polynomial textfile similar to the one you will receive:
You also need to implement a user interface that will be displayed on the terminal. The
interface should provide the user possible options to perform. Each option should be assigned
a number, so the user enters the corresponding number to perform the desired action. Check
Screen Outputs section for clarity. Following options should be provided to user:
• Print the list of vectors and polynomials. List index should start from 1. You will
• Do a vector operation and print the result. Vectors in the operation are given with their
4
index. 1 + 2 should perform addition with first and second vectors in the list. There
will be 3 operation options here: +, ∗, and ..
• Do a polynomial operation and print the result. Similarly, polynomials are given with
their index. With polynomials, there will be 2 operation options: + and ∗.
• Exit the program
For both vector and polynomial operation, user should enter the operation in a single line.
For example, when user is asked a vector operation, 1 + 2 should print the result of addition
of vectors1 and vector2 in the list. 1.2 should print dot product of vectors1 and vector2, while
1 ∗ 2 should print scalar multiplication of vector1 with integer 2.
Submission
Submit your homework files through Ninova. Please zip and upload all your files using filename BLG252E_HW_1_STUDENTID.zip. You are going to submit the following files:
1. Polynomial.h
2. Vector.h
3. main.cpp
In addition, you can submit any other files if you found it necessary, but above should be
sufficient.
5
Screen Outputs
After you are through with everything, your program should look like below. It would be
better if it looks exactly like the screenshots, but printing parts might differ and they will
not affect your grade negatively. However, in your program, user should be able to give
inputs as in the screenshot!
6