Question

NEED HELP IN C++, IT'S UGENT. I been trying to figure out for hours what is...

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];
  
if (b > 0)
{
if (a == 1)
ss << "x + " << b;
else if (a == -1)
ss << "-x + " << b;
else
ss << a << "x + " << b;
}
else if (b < 0)
{
if (a == 1)
ss << "x " << b;
else if (a == -1)
ss << "-x " << b;
else
ss << a << "x " << b;
}
else
if (a == 1)
ss << "x";
else if (a == -1)
ss << "-x";
else
ss << a << "x";
}
  
if (poly[0] > 1)
{
int degree = poly[0];
int i;
if (poly[1] == 1)
ss << "x^" << degree;
else if (poly[1] == -1)
ss << "-x^" << degree;
else
ss << poly[1] << "x^" << degree << " ";
for (i = 2; i <= degree-1; i++)
{
if (poly[i] > 0)
{
if (poly[i] == 1)
ss << " + " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " + " << "-x^" << degree - i+1 << " ";
else
ss << " + " << poly[i] << "x^" << degree - i+1 << " ";   
}
else if (poly[i] < 0)
{
if (poly[i] == 1)
ss << " - " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " - " << "-x^" << degree - i+1 << " ";
else
ss << " - " << poly[i] << "x^" << degree - i+1 << " ";   
}
else
;
}
  
  
int a = poly[i];
int b = poly[i + 1];
  
if (b > 0)
{
if (a > 0)
{
if (a == 1)
ss << "+ x + " << b;
else
ss << "+ " << a << "x + " << b;
}
else if (a < 0)
{
if (a == -1)
ss << "- x + " << b;
else
ss << a << "x + " << b;
}
else
ss << "+ " << b;
}
else if (b < 0)
{
{
if (a > 0)
{
if (a == 1)
ss << "+ x +- " << b;
else
ss << "+ " << a << "x - " << b;
}
else if (a < 0)
{
if (a == -1)
ss << "- x - " << b;
else
ss << "- " << a << "x - " << b;
}
else
ss << b;
}
}
else
if (a > 0)
{
if (a == 1)
ss << "+ x";
else
ss <<"+"<< a << "x";
}
else if (a < 0)
{
if (a == -1)
ss << "- x";
else
ss << a << "x";
}
else
;
  
}
  
return ss.str();
  
}

double polyEval(double poly[],double x)
{
int degree = poly[0];
double result = 0;
int n = 1;
int i;

for (i=degree; i > 0; i--)
{
result += poly[i]*(pow(x,n));
n++;
}

result += poly[degree+1];

return result;

}

double* differentiate(double poly[])
{
int degree = poly[0];

double *result = new double[degree+1];
*result = degree-1;
int n = degree;
int i;
for (i = 1; i <= degree; i++)
{
result[i] = poly[i]*n;
n--;
}

return result;
delete[] result;
}


int main()
{
cout << "Enter the degree of the polynomial -> ";
int degree;
cin >> degree;
int size = degree+1;
cout << "Enter the coefficients -> ";
double *poly = new double[size+1];
*poly = degree;
int i;
for (i = 1; i <= size; i++)
{
cin >> poly[i];
}

cout << "f(x) = " << polyToString(poly) << endl<< endl;

cout << "Enter a value at which to evaluate this polynomial -> ";
double x;
cin >> x;

cout << "f(" << x << ") = " <<setprecision(6)<< polyEval(poly,x)<< endl<< endl;

double* oneprime = NULL;
oneprime = differentiate(poly);
cout << "f’(x) = " << polyToString(oneprime) << endl;
  
cout << "Enter a value at which to evaluate this polynomial -> ";
double y;
cin >> y;
cout<< "f'("<<y<<")="<< polyEval(oneprime,y)<< endl;

double* twoprime= NULL;
twoprime = differentiate(oneprime);
cout<<"f\"(x) = "<< polyToString(twoprime) << endl;
cout<< "Enter a value at which to evaluate this polynomial - > ";
double z;
cin>> z;
cout<<"f\"("<<z<<")= "<< polyEval(twoprime,z)<<endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Your code is completely correct. You might be using gcc compiler which might have caused the issue.

Use g++ compiler to compiler C++ source code instead of gcc.

I have given the output from your program for your reference.

It would be real help for me if you please rate my answer.

Add a comment
Know the answer?
Add Answer to:
NEED HELP IN C++, IT'S UGENT. I been trying to figure out for hours what is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- 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()...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    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...

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • My if/else statement wont run the program that I am calling. The menu prints but once...

    My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1;     while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • Trying to figure out how to complete my fourth case 4 (option exit), write a function...

    Trying to figure out how to complete my fourth case 4 (option exit), write a function that display a “Good Bye” message and exits/log out from user’s account displaying a menu (sign in, balance, withdraw and exit.. #include<iostream> #include <limits> using namespace std; void printstar(char ch , int n); double balance1; int main() { system("color A0"); cout<<"\n\t\t ========================================="<< endl; cout<<"\t\t || VASQUEZ ATM SERVICES ||"<< endl; cout<<"\t\t ========================================\n\n"<< endl; int password; int pincode ; cout<<" USERS \n"; cout<<" [1] -...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT