Thread: Polynomial

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Question Polynomial

    Hi I'm working on a program in which I need to have the user input a degree for a polynomial with two variables (x,y) and then input the coefficients for each term. I can't figure out how to output the y exponent, can anyone give me any suggestions?

    Code:
    //Exercise Polynomial Part 2
    //May 10, 2005
    
    #include <iostream.h>
    #include <stdlib.h>
    
    const int arraySize = 7;
    void print ( int, int [][arraySize] );
    int main () 
    {
        int power, array [arraySize][arraySize] = {0}, cof;
        
        cout << "What is the degree of your polynomial?\n"
             << "Enter a natural number less than 7: ";
        while ( cin >> power && power > 7 || power < 0 )
            cout << "\nYou need to pick another number: ";
        
        for ( int i = power; i >= 0; i-- )
        {
            for ( int j = 0; j <= i; j ++ )
            {
                    cout << "\nEnter the coefficient of x^" << i - j << "y^" << j << ": ";
                    cin >> cof;
                    array [i][j] = cof;
             }
          }  
          
          print ( power, array ); 
         
        
        system ("PAUSE");
        return 0;
    }
    
    void print ( int c, int buffer [][arraySize] )
    {   
        cout << endl << buffer [c][c] << "x^" << c << "y^" << 
        for ( int i = c 
        
    }
    Last edited by CompiConQuiso; 05-12-2005 at 03:58 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    according to the first nested for loop the exponent of y can range from 0 to c if c is the exponent of x. Therefore, there is no one exponent of y. If you want to display only the possibility of exponent of y == exponent of x you could just do

    cout << buffer[c][c] << "x^" << c << "y^" << c;

    If I understand your original code correctly, I think you can display all the possible exponents of y for a give exponent of x as follows, with the corresponding coefficient:

    for(i = 0; i <= c; ++i)
    cout << buffer[c][i] << "x^" << c << "y^" << i;
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Question

    Thanks a lot but the y part still isn't working, that just gives x and y the same power but they need to add up to that power. thanks though
    Last edited by CompiConQuiso; 05-13-2005 at 06:14 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    so you want to display all terms where the power of x plus the power of y equal c in the form coeffx^ay^b where a and b are the powers of x and y respectively and coeff is stored in buffer[a][b]? Try this:

    for(int i = c; i >= 0; --i)
    cout << buffer[i][c - i] << "x^" << i << "y^" << c - i << endl;
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    thank you SOOOO much it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entering in an nth degree polynomial
    By Noah in forum C Programming
    Replies: 1
    Last Post: 03-02-2006, 09:02 PM
  2. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM
  3. Polynomial Problem
    By softcoder76 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2002, 02:07 PM
  4. linked lists of a polynomial
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2002, 07:51 PM
  5. linked lists of a polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 06:44 PM