Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My goal is to create a program that creates two polynomials and adds them together and displays the result. I am having trouble with the constructors to create the two polynomials that will be displayed in the ToString() Method that I have to override. I am having issues overriding the ToString() Method to return the polynomial and display it in the Display Method.

static void Main(string[] args)
{

//create two polynomials that can be added together

Polynomial[] polCoefficents = new Polynomial[5]; //creates 5 spaces in array
polCoefficents.ToString();
Polynomial polynomialone = new Polynomial(); //creates first polynomial
Polynomial polynomialtwo = new Polynomial(); //creates second polynomial
Polynomial polDegrees = new Polynomial(); //set degress to length of coefficents

for (int x = 0; x < polCoefficents.Length; x++) //creates the coefficents
{
polCoefficents[x] = new Polynomial(x);
// numbers in brackets represent degrees
polCoefficents[0] = new Polynomial(0);
polCoefficents[0].Coefficents = 20; //the coefficent for x ^ 0 is 20
polCoefficents[1] = new Polynomial(1);
polCoefficents[1].Coefficents = -5; //the coefficent for x is -5
polCoefficents[2] = new Polynomial(2);
polCoefficents[2].Coefficents = 4; //the coefficent for x ^ 2 is 4
polCoefficents[3] = new Polynomial(3);
polCoefficents[3].Coefficents = 5; //the coefficent for x ^ 3 is 5
polCoefficents[4] = new Polynomial(4);
polCoefficents[4].Coefficents = 9; //the coefficent for x ^ 4 is 9
polCoefficents[x].ToString();
}

Console.WriteLine("");
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();

}
}
class Polynomial: IComparable 
{
int intDegree;
int[] intCoefficents = new int[] { 1, 5, 10, 4, 8};

//sorts
int IComparable.CompareTo(Object B)
{
int returnVal;

Polynomial nextvalue = (Polynomial)B;
if (this.intDegree > nextvalue.intDegree)
{
returnVal = 1;
}
else if (this.intDegree < nextvalue.intDegree)
{
returnVal = -1;
}
else
{
returnVal = 0;
}
return returnVal;
}

public int Degree
{
get
{
return intDegree;
}
set
{
intDegree = value;
}
}
public int Coefficents
{
get
{
return intCoefficents[4];
}
set
{
intCoefficents[4] = value;
}
}
public Polynomial()
{
//creates polynomial 0
// intDegree = intCoefficents.Length;
for (int i = 0; i < intCoefficents.Length; i++)
{
intCoefficents[i] = 0;
}
}

//constructors
public Polynomial(int intDegree)
{
//this code will create the second polynomial (x^4)
this.intDegree = intDegree;
for (int x = 0; x < intCoefficents.Length; x++)
{
this.intCoefficents[x]=0;
}
this.intCoefficents[this.Degree] = 1;

ToString();
}
public Polynomial(int Degree, int[] intArrayIntegers)
{
intCoefficents = intArrayIntegers;
intDegree = intArrayIntegers.Length;
}

//displays a polynomial
public override string ToString()
{
for (int i = 0; i < intCoefficents.Length; i++) //has to be for loop, but confused on what goes inside
{
//confused on what goes in here
}
return string.Format("{0}x^{1} + ", intCoefficents, intDegree); //returns polynomial
}
public void Display()
{
//displays polynomial
Console.WriteLine();
} 


What I have tried:

I was able to make that work but now I am confused on what should go in the for loop to make the first and second polynomials display and then add them.
Posted
Updated 11-May-17 21:40pm
Comments
Ralf Meier 12-May-17 1:46am    
What exactly do you want to see as result from 'ToString' ?
Which elements should be worked with the loop ? (I'm a little bit confused too)

The code makes no sense. A polynomial is described by it's degree and the coefficients (degree + 1 values). The coefficients are usually floating point values and not integers.

There is no need to store the degree because it can be retrieved by getting the coefficients array length.

Untested example:
C#
class Polynomial: IComparable 
{
    double[] Coefficents;
    
    public Polynomial(double[] coeffs)
    {
        Init(coeffs);
    }

    public void Init(double[] coeffs)
    {
        Coefficents = coeffs;
    }

    public int GetDegree()
    {
        return Coefficients.Length - 1;
    }
    
    public double Calculate(double x)
    {
        double y = 0;
        double px = 1;
        for (int i = 0; i < Coefficients.Length; i++)
        {
            y += px * Coefficients[i];
            px *= x;
        }
        return y;
    }

    // Create a string representation
    public override string ToString()
    {
        string result;
        if (Coefficients.Length)
            result = Double.ToString(Coefficients[0]);
        for (int i = 1; i < Coefficients.Length; i++)
        {
            if (Coefficients[i] < 0)
                result += " - ";
            else
                result += " + ";
            result += Double.ToString(Math.Abs(Coefficients[i]));
            result += "*x^";
            result += Int.ToString(i);
        }
        return result;
    }

}
 
Share this answer
 
Try
C#
static void Main(string[] args)
{
  int[] ar = new int[] { 1, 0, 2 };
  Polynomial p = new Polynomial(3, ar);
  Console.WriteLine(p);
}


with

C#
//displays a polynomial
public override string ToString()
{
  string s = string.Empty;

  for (int i = 0; i < intCoefficents.Length; i++)
  {
    if (intCoefficents[i] != 0)
      if ( s == string.Empty)
        s = string.Format("{0}x^{1}", intCoefficents[i], i);
      else
        s = string.Format("{0}x^{1} + ", intCoefficents[i], i) + " " + s;
  }
  if (s == string.Empty)
    s = "0";
  return s;
}
 
Share this answer
 
v2
Comments
Member 13194996 12-May-17 8:32am    
I looked at both solutions presented and tried putting them in the code and it hasn't worked but it looks to be close to the correct answer. The display still doesn't work and i'm confused how to do the adding of the polynomials.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestCH7ChrisShaw
{
class Program
{
static void Main(string[] args)
{
//create two polynomials that can be added together

int[] Coefficents = new int[] { 20, -5, 4, 5, 9 };
Polynomial polynomialone = new Polynomial(Coefficents.Length, Coefficents);
Coefficents.ToString();

Polynomial[] polCoefficents = new Polynomial[5]; //creates 5 spaces in array
polCoefficents.ToString();

//for (int x = 0; x < polCoefficents.Length; x++) //creates the coefficents
//{
// polCoefficents[x] = new Polynomial(x);
// // numbers in brackets represent degrees
// polCoefficents[0] = new Polynomial(0);
// polCoefficents[0].Coefficents = 20; //the coefficent for x ^ 0 is 20
// polCoefficents[1] = new Polynomial(1);
// polCoefficents[1].Coefficents = -5; //the coefficent for x is -5
// polCoefficents[2] = new Polynomial(2);
// polCoefficents[2].Coefficents = 4; //the coefficent for x ^ 2 is 4
// polCoefficents[3] = new Polynomial(3);
// polCoefficents[3].Coefficents = 5; //the coefficent for x ^ 3 is 5
// polCoefficents[4] = new Polynomial(4);
// polCoefficents[4].Coefficents = 9; //the coefficent for x ^ 4 is 9
// polCoefficents[x].ToString();
//}

Console.WriteLine("");
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();

}
}
class Polynomial: IComparable
{
int intDegree;
int[] intCoefficents = new int[] { 1, 5, 10, 4, 8};

//sorts
int IComparable.CompareTo(Object B)
{
int returnVal;

Polynomial nextvalue = (Polynomial)B;
if (this.intDegree > nextvalue.intDegree)
{
returnVal = 1;
}
else if (this.intDegree < nextvalue.intDegree)
{
returnVal = -1;
}
else
{
returnVal = 0;
}
return returnVal;
}

public int Degree
{
get
{
return intDegree;
}
set
{
intDegree = value;
}
}
public int Coefficents
{
get
{
return intCoefficents[4];
}
set
{
intCoefficents[4] = value;
}
}
public Polynomial()
{
//creates polynomial 0
// intDegree = intCoefficents.Length;
for (int i = 0; i < intCoefficents.Length; i++)
{
intCoefficents[i] = 0;
}
}

//adds polynomials
//public int Calculate(int x)
//{
// int inty = 0;
// int intpx = 1;
// for (int i = 0; i < intCoefficents.Length; i++)
// {
// inty += intpx * intCoefficents[i];
// intpx *= x;
// }
// return inty;
//}

//constructors
public Polynomial(int intDegree)
{
//this code will create the second polynomial (x^4)
this.intDegree = intDegree;
for (int x = 0; x < intCoefficents.Length; x++)
{

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900