Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:33:16+00:00 2026-05-14T04:33:16+00:00

I’m very new to programming and I am trying to write a program that

  • 0

I’m very new to programming and I am trying to write a program that adds and subtracts polynomials. My program sometimes works, but most of the time, it randomly crashes and I have no idea why. It’s very buggy and has other problems I’m trying to fix, but I am unable to really get any further coding done since it crashes. I’m completely new here but any help would be greatly appreciated.

Here’s the code:

#include <iostream>
#include <cstdlib>

using namespace std;

int getChoice();
class Polynomial10
{
private:
    double* coef;
    int degreePoly;

public:
    Polynomial10(int max); //Constructor for a new Polynomial10
    int getDegree(){return degreePoly;};
    void print(); //Print the polynomial in standard form
    void read(); //Read a polynomial from the user
    void add(const Polynomial10& pol); //Add a polynomial
    void multc(double factor); //Multiply the poly by scalar
    void subtract(const Polynomial10& pol); //Subtract polynom
};

void Polynomial10::read()
{
    cout << "Enter degree of a polynom between 1 and 10 : ";
    cin >> degreePoly;

    cout << "Enter space separated coefficients starting from highest degree" << endl;
    for (int i = 0; i <= degreePoly; i++) cin >> coef[i];
}

void Polynomial10::print()
{
    for (int i = 0;i <= degreePoly; i++) {
       if (coef[i] == 0) cout << "";
       else if (i >= 0) {
           if (coef[i] > 0 && i != 0) cout<<"+";
           if ((coef[i] != 1 && coef[i] != -1) || i == degreePoly) cout << coef[i];
           if ((coef[i] != 1 && coef[i] != -1) && i != degreePoly ) cout << "*";
           if (i != degreePoly && coef[i] == -1) cout << "-";
           if (i != degreePoly) cout << "x";
           if ((degreePoly - i) != 1 && i != degreePoly) {
               cout << "^";
               cout << degreePoly-i;
           }
       }
   }
}

void Polynomial10::add(const Polynomial10& pol)
{
    for(int i = 0; i<degreePoly; i++) {
        int degree = degreePoly;
        coef[degreePoly-i] += pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::subtract(const Polynomial10& pol)
{
    for(int i = 0; i<degreePoly; i++) {
        coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::multc(double factor)
{
    //int degreePoly=0;
    //double coef[degreePoly];
    cout << "Enter the scalar multiplier : ";
    cin >> factor;
    for(int i = 0; i<degreePoly; i++) coef[i] *= factor;
}

Polynomial10::Polynomial10(int max)
{
    degreePoly = max;
    coef = new double[degreePoly];
    for(int i; i < degreePoly; i++) coef[i] = 0;
}

int main()
{
    int choice;
    Polynomial10 p1(1),p2(1);
    cout << endl << "CGS 2421: The Polynomial10 Class" << endl << endl << endl;
    cout
        << "0. Quit\n"
        << "1. Enter polynomial\n"
        << "2. Print polynomial\n"
        << "3. Add another polynomial\n"
        << "4. Subtract another polynomial\n"
        << "5. Multiply by scalar\n\n";

    int choiceFirst = getChoice();
    if (choiceFirst != 1) {
        cout << "Enter a Polynomial first!";
    }
    if (choiceFirst == 1) {choiceFirst = choice;}

    while(choice != 0) {
        switch(choice) {
            case 0:
                return 0;
            case 1:
                p1.read();
                break;
            case 2:
                p1.print();
                break;
            case 3:
                p2.read();
                p1.add(p2);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
            case 4:
                p2.read();
                p1.subtract(p2);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
            case 5:
                p1.multc(10);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
        }
        choice = getChoice();
    }
    return 0;
}

int getChoice()
{
    int c;
    cout << "\nEnter your choice : ";
    cin >> c;
    return c;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:33:17+00:00Added an answer on May 14, 2026 at 4:33 am

    When you create your objects with p1(1) and p2(1) the coef array in each object is allocated to contain one element. Then in read() you simply set degreePoly to a (possibly higher) value but don’t change the allocation of coef. It will still contain only one element, but all coefficients are written to it, probably writing over the bounds of the array. Instead the old coefshould be freed and a new array of suitable size be allocated.

    Also in add and subtract you are assigning to coefficients out of bounds (for i=0):

    coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
    

    It also seems wrong mathematically to subtract the coefficients at index degreePoly-(i+1) from those at index degreePoly-i. Additionally the case that the two polygons might have different degrees is currently not handled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer mydll.PrintArray.restype = None mydll.PrintArray(px) By default ctypes assumes the function… May 15, 2026 at 12:04 pm
  • Editorial Team
    Editorial Team added an answer It appears as though there was too much output to… May 15, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer I would make it an element - they are designed… May 15, 2026 at 12:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.