Reputation: 51
So I have an array of doubles, which represent the coefficients of a polynomial. I want to have a function that returns a string of the polynomial.
So coeffs = [1.3, 4.5, 6.0]
the function will produces a string
"1.3x^2 + 4.5x + 6.0"
I've been thinking about iteration to solve this, but I keep getting errors. Assume array coeffs has already been constructed.
public String toString()
{
int len = coeffs.length;
return
for(int i = 0; i < len ; ++i)
{
for(int j = len; len > 0; len--)
{
return this.coeffs[i] + "x^(" + len + ")" + ;
}
}
}
Upvotes: 1
Views: 121
Reputation: 6870
What about this
public static String polyToString() {
double[] coeffs = new double[] { 0.1, 0.2, 0.3, 1.1, 2.2, 1.3, 4.5, 6.0 };
int len = coeffs.length;
StringBuilder sb = new StringBuilder();
if(len==0)
return "";
if(len==1)
return ""+coeffs[0];
for (int i = 3; i <= len; i++) {
sb.append(coeffs[len - i] + "x^" + (len - i + 2) + " + ");
}
if (len >= 2) {
sb.append(coeffs[len - 2] + "x + " + coeffs[len - 1]);
}
return sb.toString();
}
Out
1.3x^7 + 2.2x^6 + 1.1x^5 + 0.3x^4 + 0.2x^3 + 0.1x^2 + 4.5x + 6.0
Upvotes: 0
Reputation: 6527
double [] coeff = {1.34,2.4,5.6};
String finalStr = "";
if(coeff.length>0){
for(int i=coeff.length-1,j=0;i>=0 && j<coeff.length;i--,j++){
finalStr = finalStr+coeff[j]+" "+"x^"+i+" + ";
finalStr = (i==0)?finalStr.replace("x^0 + ", ""):finalStr;
}
}
Generate finalStr
as 1.34 x^2 + 2.4 x^1 + 5.6
Upvotes: 0
Reputation: 381
Something along the lines of the following might be what you are looking for. Strictly speaking the code below does not use iteration. It is possible to iterate over the values array. However, having an integer for the current index value makes it easier to determine if 'x' is needed and the correct format for the exponent (if any).
public final class Test1 {
/*
* The main routine is where execution actually starts
*/
public static void main(String[] args) {
System.out.println((new Coeffs()).toString());
}
}
class Coeffs {
double[] values = new double[] {1.3, 4.5, 6.0};
public String toString() {
int valuesLen = values.length;
int exponent;
StringBuffer result = new StringBuffer();
for (int i = 0; i < valuesLen; i++) {
if (result.length() > 0)
result.append(" + ");
result.append(((Double) values[i]).toString());
exponent = valuesLen - i - 1;
if (exponent > 0)
result.append("x");
if (exponent > 1) {
result.append("^");
result.append(((Integer) exponent).toString());
}
}
return new String(result);
}
}
Upvotes: 0
Reputation: 25864
How about this?
public String toString(){
int len = coeffs.length;
StringBuilder result = new StringBuilder();
for(int i = 0; i < len; ++i){
int power = ((i-len+1)*-1)
result.append("" + coeffs[i] + "x^(" + power + ")");
if (i < (len - 1)){ result.append(" + "); }
}
return result.toString();
}
Gives
1.3x^(2) + 4.5x^(1) + 6.0x^(0)
I'm not sure if that's the format you want however(?)
Upvotes: 0
Reputation: 2052
You can try this as well.
float[] coeffs = new float[]{1.4f, 1.3f, 4.5f, 6.0f};
if (coeffs == null || coeffs.length == 0) {
return;
}
int power = coeffs.length - 1;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < coeffs.length; i++) {
if(power == 0) {
stringBuilder.append(" + " + coeffs[i]);
} else if(power == 1) {
stringBuilder.append(" + " + coeffs[i] + "x");
} else {
stringBuilder.append(" + " + coeffs[i] + "x^" + power);
}
power--;
}
System.out.println(stringBuilder.substring(3));
Upvotes: 0
Reputation: 11763
public String toString(){
int len = coeffs.length;
StringBuilder return_value =new StringBuilder();
// Add all the x to the power of something
for(int i = 0; i < len-1 ; i++)
return_value += this.coeffs[i]+"x^"+len-i-1+ " + ";
// last one has no x.
return_value += this.coeffs[len-1];
}
You can also put another case if you don't want to have 2x^2 + 3x^1 + 5; (so, the x will be a special case as well)
Edit:
Like Kewin suggested, this will throw an exception is length is 0.
Note you'll have to handle extreme cases, and think about what happens if one of the coeffs is 0 ( you should just skip that iteration ) :)
Upvotes: 2
Reputation: 3197
You can try this:
double[] coeffs = { 1.3, 4.5, 6.0 };
StringBuilder sb = new StringBuilder();
int len = coeffs.length;
for (int i = 0; i < len - 1; i++) {
sb.append(coeffs[i]).append('x');
if (len - 1 - i == 1) {
sb.append('+').append(' ');
} else {
sb.append('^').append(len - i - 1).append('+').append(' ');
}
}
sb.append(coeffs[len - 1]);// Append last element
System.out.println(sb.toString());// Print "1.3x^2 + 4.5x + 6.0"
Output result in Console:
1.3x^2+ 4.5x+ 6.0
Upvotes: 0
Reputation: 674
How about this?
public String toString(){
int len = coeffs.length;
String equation = '';
String power = '';
for(int i = len - 1; i > 0; i--) {
power = (i > 1 ? 'x^' + i : 'x');
equation += (coeffs[len - 1 - i] + variable + " ");
}
equation += coeffs[0];
}
Upvotes: 0
Reputation: 5637
As mentioned you cant return a loop. you would have to do something like this
public String toString()
{
int len = coeffs.length;
String result = "";
int j = coeffs.length-1;
for(int i = 0; i < len ; ++i)
{
result += coeffs[i]+"x^("+j+")+" ;
j--;
}
return result.substring(0,result.length-1);
}
Upvotes: 0
Reputation: 4972
try below code:
public String toString(){
int len = coeffs.length;
StringBuilder sb=new StringBuilder();
return
for(int i = 0; i < len ; ++i){
for(int j = len; len > 0; len--){
return sb.append(this.coeffs[i] + "x^(" + len + ")");
}
}
}
Upvotes: 0