Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods:
-------------------------------------------------------
This is Main file cpp file
#include "polynomial.h"
#include <iostream>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::stringstream;
int main(int argc, char* argv[]){
stringstream buffer1;
buffer1.str(
"3 -1 2 0 -2.5"
);
Polynomial p(3);
p.Read(buffer1);
cout << p.ToString() << endl;
p.Write(cout);
cout << endl;
cout << endl << endl;
cout << p.Minus().ToString() << endl;
return 0;
}
--------------------------------------------
This is Polynomial cpp file
#include "polynomial.h"
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cfloat>
using std::istream;
using std::ostream;
using std::string;
using std::stringstream;
using std::fixed;
using std::setprecision;
using std::showpos;
Polynomial::Polynomial(size_t degree) : _degree(degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = 0.0;
}
}
Polynomial::Polynomial(size_t degree, const float* coefficients):
_degree(degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] =
coefficients[i];
}
}
Polynomial::Polynomial(const Polynomial& polynomial):
_degree(polynomial._degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] =
polynomial._coefficients[i];
}
}
Polynomial::~Polynomial(){
// DO THIS FIRST TO PREVENT MEMORY LEAKS!
}
const Polynomial Polynomial::Sum(const Polynomial&
rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Subtract(const Polynomial&
rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Minus()const{
Polynomial retVal(*this);
for (size_t i = 0; i < _degree + 1; i++) {
retVal._coefficients[i] *=
-1;
}
return retVal;
}
const Polynomial Polynomial::Multiply(const Polynomial&
rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Divide(const Polynomial&
rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Derive()const{
return Polynomial(0);
}
float Polynomial::Evaluate(float x)const{
return FLT_MAX;
}
float Polynomial::Integrate(float start, float end)const{
return FLT_MAX;
}
const Polynomial& Polynomial::operator=(const Polynomial&
rhs){
if (&rhs == this){
return *this;
}
if (_degree != rhs._degree){
if (_coefficients){
delete[]
_coefficients;
}
_degree = rhs._degree;
_coefficients = new float[_degree +
1];
}
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] =
rhs._coefficients[i];
}
return *this;
}
bool Polynomial::Equals(const Polynomial& rhs)const{
if (_degree != rhs._degree){
return false;
}
for (size_t i=0; i < _degree; i++){
if (abs(_coefficients[i] -
rhs._coefficients[i]) > 0.0001){
return
false;
}
}
return true;
}
string Polynomial::ToString()const{
stringstream ss;
for (size_t i = _degree; i > 0; i--) {
ss << showpos << fixed
<< setprecision(2) << _coefficients[i] << "x^"
<< i << " ";
}
ss << showpos << fixed <<
setprecision(2) << _coefficients[0];
return ss.str();
}
ostream& Polynomial::Write(ostream& output)const{
output << _degree << " ";
for (size_t i = 0; i < _degree + 1; i++) {
output << _coefficients[i]
<< " ";
}
return output;
}
istream& Polynomial::Read(istream& input){
size_t degree;
input >> degree;
if (input.fail()){
return input;
}
float* coefficients = new float[degree + 1];
for (size_t i = 0; i < degree + 1; i++) {
input >>
coefficients[i];
if (input.fail()){
delete[]
coefficients;
return
input;
}
}
if (degree != _degree){
if (_coefficients){
delete[]
_coefficients;
}
_degree = degree;
_coefficients = coefficients;
}else{
for (size_t i = 0; i < _degree +
1; i++) {
_coefficients[i]
= coefficients[i];
}
delete[] coefficients;
}
return input;
}
-------------------------
this is the header file of polynomial .h file
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <string>
using std::istream;
using std::ostream;
using std::string;
class Polynomial{
size_t _degree;
float* _coefficients;
public:
Polynomial(size_t);
Polynomial(size_t, const float*);
Polynomial(const Polynomial&);
~Polynomial();
const Polynomial Sum(const
Polynomial&)const;
const Polynomial Subtract(const
Polynomial&)const;
const Polynomial Minus()const;
const Polynomial Multiply(const
Polynomial&)const;
const Polynomial Divide(const
Polynomial&)const;
const Polynomial Derive()const;
float Evaluate(float)const;
float Integrate(float, float)const;
const Polynomial& operator=(const
Polynomial&);
string ToString()const;
bool Equals(const Polynomial&)const;
ostream& Write(ostream&)const;
istream& Read(istream&);
};
#endif
------------------------------------------------------
This is the test file cpp file
#include "polynomial.h"
#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
using std::stringstream;
int main(int argc, char* argv[]){
stringstream data[5];
data[0].str("3 -1 2 0 -2.5");
data[1].str("4 -1 -2 0 0 3");
data[2].str("2 -1 0 4");
data[3].str("3 -6 -5 2 1");
data[4].str("1 -2 1");
stringstream answers[6];
answers[0].str("4 -2 0 0 -2.5 3"); // a + b
answers[1].str("7 1 0 -4 2.5 2 6 0 -7.5"); // a *
b
answers[2].str("2 3 4 1"); // d / e
answers[3].str("2 1 0 -4"); // -c
answers[4].str("4 0 -4 0 2.5 3"); // b - a
answers[5].str("2 2 0 -7.5"); // d/dx a
Polynomial a(0), b(0), c(0), d(3), e(1), r(0);
a.Read(data[0]);
b.Read(data[1]);
c.Read(data[2]);
d.Read(data[3]);
e.Read(data[4]);
Polynomial s(0), t(0), u(0), v(0), w(0),
x(0);
s.Read(answers[0]);
t.Read(answers[1]);
u.Read(answers[2]);
v.Read(answers[3]);
w.Read(answers[4]);
x.Read(answers[5]);
float total = 0;
if (a.Sum(b).Equals(s)){
cout << "\tSUM PASSED "
<< ++total << endl;
}else{
cout << "\tSUM FAILED"
<< endl;
}
if (b.Subtract(a).Equals(w)){
cout << "\tSUBTRACT PASSED "
<< ++total << endl;
}else{
cout << "\tSUBTRACT FAILED"
<< endl;
}
if (a.Multiply(b).Equals(t)){
cout << "\tMULTIPLY PASSED "
<< ++total << endl;
}else{
cout << "\tMULTIPLY FAILED"
<< endl;
}
if (a.Derive().Equals(x)){
cout << "\tMULTIPLY PASSED "
<< ++total << endl;
}else{
cout << "\tMULTIPLY FAILED"
<< endl;
}
if (b.Evaluate(3) == 236.0){
cout << "\tEVALUATE PASSED "
<< ++total << endl;
}else{
cout << "\tEVALUATE FAILED"
<< endl;
}
cout << "TOTAL TESTS TO PASS = 5" <<
endl;
cout << "UNIT TEST GRADE = " << (total / 5
* 100) << endl;
cout << " =======================" <<
endl;
cout << "Extra Credit : : : : : : : : : "
<< endl;
if (a.Integrate(1, 2) == -7.375){
total += 0.75;
cout << "\tINTEGRATE PASSED "
<< total << endl;
}else{
cout << "\tINTEGRATE FAILED"
<< endl;
}
if (d.Divide(e).Equals(u)){
cout << "\tDIVIDE PASSED "
<< ++total << endl;
}else{
cout << "\tDIVIDE FAILED"
<< endl;
}
cout << "UNIT TEST FINAL GRADE = " <<
(total / 5 * 100) << endl;
return 0;
}
------------------------------------------------------------------------
Polynomial::~Polynomial(){
// DO THIS FIRST TO PREVENT MEMORY LEAKS!
//delete &_degree; //don't need to delete since it was a local var for the obj, not a seperate reserved space
delete[] _coefficients; //delete[] will delete the entire array from given address
}
const Polynomial Polynomial::Sum(const Polynomial& rhs)const{
Polynomial sum((_degree > rhs._degree) ? _degree : rhs._degree); //degree of the sum will be same as the polynomial with greater degree
//we create three variables to help us iterate through the polynomials. We want to iterate from the last index of each polynomial
int pt_lhs = _degree; //for polynomial at lhs
int pt_rhs = rhs._degree; //for polynomial at rhs
int pt_sum = sum._degree; //for polynomial in which we will store sum
//we need to add coefficients of both polynomials until there are no terms left in either lhs or rhs
while(pt_lhs>=0 && pt_rhs>=0){
sum._coefficients[pt_sum--] = _coefficients[pt_lhs--] + rhs._coefficients[pt_rhs--];
}
if(pt_lhs > 0){ //if there are more terms left in lhs, they will be added to sum (i.e added to 0)
sum._coefficients[pt_sum--] = _coefficients[pt_lhs--];
}
else if(pt_rhs >= 0){ //if there are more terms left in rhs, they will be added to sum (i.e added to 0)
sum._coefficients[pt_sum--] = rhs._coefficients[pt_lhs--];
}
return sum; //return the polynomial in which we will store sum
}
const Polynomial Polynomial::Subtract(const Polynomial& rhs)const{
Polynomial diff((_degree > rhs._degree) ? _degree : rhs._degree); //degree of the difference will be same as the polynomial with greater degree
//we create three variables to help us iterate through the polynomials. We want to iterate from the last index of each polynomial
int pt_lhs = _degree; //for polynomial at lhs
int pt_rhs = rhs._degree; //for polynomial at rhs
int pt_diff = diff._degree; //for polynomial in which we will store difference
//we need to subtract coefficients of both polynomials until there are no terms left in either lhs or rhs
while(pt_lhs>=0 && pt_rhs>=0){
diff._coefficients[pt_sum--] = _coefficients[pt_lhs--] - rhs._coefficients[pt_rhs--];
}
if(pt_lhs >= 0){ //if there are more terms left in lhs, they will be added to sum (i.e added to 0)
diff._coefficients[pt_sum--] = _coefficients[pt_lhs--];
}
else if(pt_rhs >= 0){ //if there are more terms left in rhs, they will be subtracted from sum (i.e subtracted from 0)
diff._coefficients[pt_sum--] -= rhs._coefficients[pt_lhs--];
}
return diff; //return the polynomial in which we will store difference
}
float Polynomial::Evaluate(float x)const{
float val = 0; //variable to store the evaluated value
for(int i=0; i<_coefficients.length; i++){
val += _coefficients[i] * pow(x, _degree-i); //since the degree of the term reduces by one every time you go to next term
}
return val; //return the evaluated value
}
Hi guys! I need help for the Data Structure class i need to provide implementation of...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...
Please Implement ParkingLot.cpp below based off the completed Automobile Class and ClaimCheck class down below. Automobile.hpp #pragma once #include <iostream> #include <string> class Automobile { friend bool operator==( const Automobile& lhs, const Automobile& rhs ); friend std::ostream & operator<<( std::ostream& stream, const Automobile& vehicle ); private: std::string color_; std::string brand_; std::string model_; std::string plateNumber_; public: Automobile( const std::string & color, const std::string & brand, const std::string & model, const std::string & plateNumber ); }; bool operator!=( const Automobile& lhs, const...
Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) { // average function , declaring variable int i; char str[40]; float avg = 0; // iterating in...
please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...
NEED HELP IN C++, IT'S UGENT. I been trying to figure out for hours what is wrong with my code. I can not get the first derivative to display and its evaluation to display. my code is below: #include <iostream> #include <cmath> #include <iomanip> #include <string> #include <sstream> using namespace std; string polyToString(double poly[]) { stringstream ss; if (poly[0] == 0) { ss << poly[1]; } if (poly[0] == 1) { int a = poly[1]; int b = poly[2];...