How would I create a polynomial function using an array? Basically I want to display the polynomial (ex. x^2 + 3x + 5). Then, I want to show what the highest polynomial degree is. For example, x^2 + 3x + 5 = 2 (highest degree).
Asked
Active
Viewed 741 times
-3
-
What code are you using to express your polynomials in your program, `Math.Pow()` I imagine since it's C#? Let's see your code... – salad_bar_breath Nov 29 '15 at 00:55
-
Yes, I was think of using Math.Pow() and a loop that will fill the exponent part. – CharleBarkely123 Nov 29 '15 at 00:58
-
I have been trying to create the polynomial using an array. – CharleBarkely123 Nov 29 '15 at 01:06
2 Answers
1
// initial highest exponent
int exponent = 0;
// We're assuming [in] is a stream such as istream in C++.
if (char == ^)
{
in >> char;
if (char > initial)
{
initial = char)
}
}
We also have to check if the char
after x
is a +
, -
, or white space. We want to also check for that exponent.

Will
- 24,082
- 14
- 97
- 108

White Lotus
- 353
- 2
- 6
- 16
1
I might recommend doing it like this.
string polynomial = "x^2 + 3x + 5";
int index = 0,highestdegree = 0;
foreach (char character in polynomial) {
if(character == '^')
{
index++;
try{
int test;
int.TryParse(polynomial[index],out test);
if(test >highestdegree)
highestdegree = test;
index--;
}
catch{
index--;
}
}
index += 1;
}
if(highest degree == 0)
{
highestdegree == 1;
}
return highest degree;
You would have to turn your polynomial to a string first though.

Mindstormer
- 299
- 3
- 16