hi,
i wrote this program on polynomials and how to manipulate them. however, i am having a problem with my eval function. it is supposed to evaluate the polynomial at a particular number. however, whenever i call a.eval(2.0), i keep getting polynomial a.

``````#include<iostream>
using namespace std;
//A class to handle a list node
class Lnode{
public:
Lnode();
Lnode(int);
int data;
Lnode *next;//This is the pointer to the next node
};

Lnode::Lnode(){
data = 0;
next = NULL
}
Lnode::Lnode(int d){
data = d;
next = NULL;
}
class LList{
public:
LList();
LList(int);
int getSize    void print();
void printP();
void sub(LList);
double eval(double);
void set(int, int);

private:
int size;

};
//Constructor Definition
LList::LList(){
size = 0;
head = tail = NULL;//initially, there is nothing. none of them exist.
}
LList::LList(int n){
size = n;
for(int i=size; i>0; i--)
}
int LList::getSize(){
return size;//to know the size of the list
}
Lnode *nnode = new Lnode(d);//pointer to the list node
if(tail==NULL)

}

void LList::print(){

while( current!= NULL)
{
cout<<current->data<<"  ";
current = current->next;
}

}
void LList::printP(){
int degree = size-1;
while( current!= NULL)
{
cout<<current->data<<"x^"<<degree<<" "<<"+";
current = current->next;
degree--;
}

}

void LList::set(int c, int val)
{

int i = size-1;
while(current != NULL)
{
if(i==(c))
{
current->data = val;
break;
}
else
{
current = current->next;
i--;
}
}
}

Lnode *ptrL;
Lnode *ptrL1;
while(ptrL != NULL && ptrL1 != NULL)
{
ptrL->data = ptrL->data + ptrL1->data;
ptrL = ptrL->next;
ptrL1 = ptrL1->next;
}

}
void LList::sub(LList L){
Lnode *ptrL;
Lnode *ptrL1;
while(ptrL != NULL && ptrL1 != NULL)
{
ptrL->data = ptrL->data-ptrL1->data;
ptrL = ptrL->next;
ptrL1 = ptrL1->next;
}
}
double LList::eval(double x){
double sum = 0;
while(temp->next != NULL)
{
sum = sum*x + temp->data;
temp = temp->next;
//return sum;
}
return sum;

}

//Driver Main Program
void main(){

LList a(3);
a.set(2, 4);
a.set(1, 5);
a.set(0, 6);
cout<<"First Polynomial is:"<<" ";
a.printP();
cout<<endl;
a.eval(2.0);
a.printP();

LList b(3);
b.set(2, 7);
b.set(1, 8);
b.set(0, 9);
cout<<"Second Polynomial is:"<<" ";
b.printP();
cout<<endl;
cout<<"First + Second is:"<<" ";
b.sub(a);
b.printP();
cout<<" "<<endl;

LList c(3);
c.set(2,1);
c.set(1,2);
c.set(0,3);
cout<<"Third Polynomial is:"<<" ";
c.printP();
cout<<endl;
cout<<"First - Third is:"<<" ";
a.sub(c);
a.printP();
cout<<endl;

}``````
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by olams

First, have main() return an int, not void.

Second, in main() when you call eval() using polynomial a like this:

a.eval(2.0);

you are ignoring the return value of eval(). To see what the return value is you could print it to the screen directly like this:

cout << a.eval(2.0) << '\n';

or you could save it and then print it like this:

double d = a.eval(2.0);
cout << d << '\n';

thanks a lot.

First, have main() return an int, not void.

Second, in main() when you call eval() using polynomial a like this:

a.eval(2.0);

you are ignoring the return value of eval(). To see what the return value is you could print it to the screen directly like this:

cout << a.eval(2.0) << '\n';

or you could save it and then print it like this:

double d = a.eval(2.0);
cout << d << '\n';

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.