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.
please help me look into this.

#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 add(LList);
    void sub(LList);
    double eval(double);
    void set(int, int);
    
private:
    void addToF(int);//adding a number to the front of the list. 
    Lnode *head, *tail;//a list has two pointers, head and tail.
    int size;

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


void LList::print(){
    Lnode *current = head;

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

}
void LList::printP(){
    Lnode *current = head;
    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;
    Lnode *current=head;
        while(current != NULL)
    {
        if(i==(c))
        {
            current->data = val;
            break;
        }
        else
        {
            current = current->next;
            i--;
        }
    }
}

void LList::add(LList L1){
    Lnode *ptrL;
    Lnode *ptrL1;
    ptrL = head;
    ptrL1 = L1.head;
    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;
    ptrL = head;
    ptrL1 = L.head;
    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;
    Lnode *temp = head;
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.add(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;
    
}

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';

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.