CS 1160 C/C++ Programming
12 MWF - Spring 2016
March 28th thru April 1st - Week #11
Monday 03/28/2016
- Annotated addTerm(p, n, coef, exp)
solution - handed out in class on Monday.
- Polynomial In-Class Exercize:
Partner Programming - addTerm(p, n, c, e) - to be finished first thing
on Monday 03/28 in class. Uses the array of structs example from earlier this week.
- Assignment POLYNOMIALS: Create Functions to:
- read a polynomial from a file,
- add two polynomials together to create a third polynomial,
- output a very readable nicely formatted polynomial.
Uses the array of structs example
we have been studying.
Due data: Tuesday April 5th by 11:30 pm.
- Output to demonstrate the
testing of addTerm(p, n, coefficient, exponent)
function.
- What does polynomials.txt input file look like: 4 terms for 1st polynomial
and 5 terms for the 2nd polynomial.
jacobson[weblab]:~$ more polynomials.txt
123.4 6 11.1 5 3.2 3 17 2 0 0
5.5 7 -123.4 6 12.3 4 12.21 2 105 0 0 0
polynomials.txt
123.4 6 11.1 5 3.2 3 17 2 0 0
5.5 7 -123.4 6 12.3 4 12.21 2 105 0 0 0
- Sample output from polynomials.txt input file:
jacobson[weblab]:~$ ./a.out
Poly #1:
6 5 3 2
123.4x 11.1x 3.2x 17x
Poly #2:
7 6 4 2 0
5.5x -123.4x 12.3x 12.21x 105x
Poly #1 + #2:
7 5 4 3 2 0
5.5x 11.1x 12.3x 3.2x 29.21x 105x
Poly #1 + #1:
6 5 3 2
246.8x 22.2x 6.4x 34x
Poly #2 + #2:
7 6 4 2 0
11x -246.8x 24.6x 24.42x 210x
Wednesday 3/30/2016
inputPoly(p1, n1, pfile)
inputPoly(p2, n2, pfile)
- The input file with just two polynomials in it. Read polynomail terms until you
encounter a coefficient that is 0 (ZERO). That is the FLAG to indicate
the end of a polynomial.
polynomials.txt
123.4 6 11.1 5 3.2 3 17 2 0 0
5.5 7 -123.4 6 12.3 4 12.21 2 105 0 0 0
- The input of a polynomial from a file is a little bit tricky.
- Here is the function declaration, aka function prototype that I
used.
void inputPoly(polyTerm p[], int& n, ifstream& pFile);
- The input polynomial function has 3 arguments.
inputPoly(p1, n1, pFile);
-- -- -----
1 2 3 1. The array for storing the terms.
2. The int that will indicate how many terms.
3. THe file handle for the already opened
file from which we will read the
next line of coef exponent pairs,
i.e. the next line of terms.
- The file handle ( pFile )
for the file, which was already opened in the main program, must be passed
by reference.
int main()
{
...
ifstream pFile;
pFile.open("polynomials.txt");
...
inputPoly(p1, n1, pFile);
inputPoly(p2, n2, pFile);
...
}
See week #5 for files and fstream and ifstream preprocessor
directives (#include)
Friday 4/01/2016
- Assignment POLYNOMIALS: Functions to read a polynomial from a file, to
add two polynomials together to create a third polynomial, and to output a polynomial.
DUE BY 11:30 pm on Tuesday April 5th.
- HOMEWORK
SUBMISSION IS READY for programming assignment polynomials.
Login with your UNI CatID and passphrase.
Choose CS 1160, C++, Jacobson and then click the Continue button.
HOMEWORK SUBMIT system:
C++
Homework SUBMISSION for C++ Assignments for Jacobson
polynomials.cpp
DUE BY 11:30 pm on Tuesday April 5th.
- The addTerm() function:
addTerm(p, n, coef, exp) with SELECTION SORT
approach. Different approach covered in class on Friday, April Fools Day.
void addTerm( polyTerm p[], int& n, double c, int e)
{
int locTerm = n, newCoef;
for (int i = 0; i < n; i++)
if (e == p[i].exponent)
locTerm = i;
if (locTerm < n)
{
p[locTerm].coef = p[locTerm].coef + c;
if (p[locTerm].coef == 0)
{
n--;
p[locTerm].coef = p[n].coef;
p[locTerm].exponent = p[n].exponent;
selectionSort(p, n);
}
}
else
{
p[n].coef = c;
p[n].exponent = e;
n++;
selectionSort(p, n);
}
}