1. Homepage of Dr. Zoltán Porkoláb
2. Teaching
3. Research
4. Affiliations

# Student project

During the semester the students will (individually) work on a larger project. They will step by step create a Polynom class and its test environment. For every time you have to create a small additional task built on the previous results. We publish the solutions of every steps after some delay, so if you are disrupted for a while you still can catch up later.

Submit the solutions on Canvas as one single C++ source file containing the main() function given in the task.

## Task 1: Create a pretty printer for polynoms

Write a pretty printer function for polynoms which returns a string representation of the polynom. The polynom is given by its coefficients, stored in a std::vector (the lower coefficients are in the lower indexes). We can suppose, that the highest element of the vector is not 0.

Expected result is:

Consider the edge cases: 0 degree polynom, no constant tag, negatives, etc.. Write a main() function which tests your function with various cases. Use the std::to_string() function to convert numbers to string.

Submit your solution to Canvas by 26th March, 23:59h.

### Solution

Video:

cpp-mat-task-1-1.mkv

cpp-mat-task-1-2.mkv

cpp-mat-task-1-3.mkv

## Task 2: Evaluate a polynom

Write a function to evaluate polynoms at a value t.

Expected result is:

Write a main() function which tests your function with various cases. Submit your solution (one single c++ source file) to Canvas in mail by 12 April (Monday) 24h.

### Soultion

We will use the Horner’s method to evaluate the polinomial.

p(x) = a0 + a1x + a2x^2 + … + anx^n = a0 + x( a1 + x( a2 + … xan … ) )

The output is:

## Task 3: Write a Polynom class

Write a (non-templated) class to represent polynoms. Write constructor(s) to initialize a polynom with a vector<> or to create a “0” polynom. Write member functions to return the string representation and evaluate a polynom (like to_string() and evaluate() in the prevous tasks). Write a deg() member function to return the degree of the polynom (rank of the highest coefficient), and a coeff(i) member function to return the
coefficient i. The member function coeff(i) should return 0 if i is higher than the degee of the polynom.

Separate the interface and the implementation of the class. Write a header file polynom.h for the interface and a source file polynom.cpp for the implementation.

This is a sample, how we want to use the class. Consider the doubled brace pairs { { … } } at the construction of the std::vector of Polynoms. This is necessary, because the inner brace pair { 42, 0, 5, 0, 8, 9 } is specifying the initialization of a vector (like earlier), and the outer pair is the parenthesis for the Polynom constructor.

The expected result is:

Write a main() function which tests your function with various cases. Submit your solution (one single c++ source file) to Canvas in mail by 26 April (Monday) 24h.

### Solution

To implement the polynom class we will create a polynom.h for the interface of the class, and the polynom.cpp as the implementation.

In the polynom.h header file (inside the mandatory include guards), we define the class. The constructor will take a std::vector - the same we used in the earlier version as the parameter of the to_string() and eval() function. The constructor has a default parameter: an empty vector. Without parameter (e.g. as a default constructor) we create an empty representation, which is the 0 polynom.

The deg(), coeff(), eval() and to_string() functions are the public interface of the class. See the deg() functon as implemented inside the class declaration. Also, consider that it is valide to call the deg() function with a parameter higher than the degree of the polynom. In that case deg() returns 0.

The only data member will be the __coeffs - a vector of integers. The normalize() and coeff_to_string() functions are private members, as they are not part of the interface of the class.

The polynom.cpp contains the implementation of the rest of the methods. Most of the functions are straightforward to rewrite from global function to member.

## Task 4: Write a Polynom class template with operators

Write a class template for the version of the Polynom. The interface is similar than the prevoius non-templated class, but extend it with some operators. (As it is a class template, you will write only a header file and a main.cpp for test). (2 pts)

Write operators for the template Polynom class. If p1 and p2 are polynoms, then let p1(t) is the evaluation of p1 at t. Also, implement the global operators p1 + p2, and p1 - p2 (2pts).

A test program:

The expected result is:

Write a main() function which tests your function with various cases, including other types than double. Submit your solution as one single source into canvas (separating polynom.h and main.cpp with /****/) by 12th May (Wednesday) 23:59h.

If you have time, implement p1 * p2 and p1 / p2 but these are just extras… Also, think how could you implement the + operator allowing to add a Polynom<int> to a Polynom<double>?

Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.