# Problem with array manipulation

Discussion in 'Programming' started by channel, Apr 2, 2009.

1. ### channelNew Member

Joined:
Mar 30, 2009
Messages:
2
0
Trophy Points:
0
Occupation:
student
Location:
Botswana
I'm trying to solve a cubic equation. A cubic equation has three solutions for x and I want the values of x to be stored in an array int_array. The problem that I'm having is that I'm getting double results for each x. I want to access the first value of x by writing the following: int_array, the second value by int_array and the third value by int_array. The other problem is that if I try to access int_array, I get [3,0,0], the same result that I get when I try to access int_array and int_array.

Here is the code that I wrote to solve the problem:

Code:
```public class Equation {

public void doCalculations(int a, int b, int c, int d){
int j;
for (int i=-10;i<=10;i++){
if((a*(i*i*i)+ b*(i*i)+ c*(i)+ d)==0){
j=i;
int int_array[] = new int;
for(int k=0;k<int_array.length;k++)
int_array[k]=j;

System.out.println("The value of X1 is :" + int_array);
System.out.println("The value of X2 is :"+ int_array);
System.out.println("The value of X3 is :"+ int_array);

}
}
}

public static void main(String[]args){
Equation equation = new Equation();
equation.doCalculations(1, -6, 11, -6);
}

}```

Here are sample results that I get after running the program:

The value of X2 is :1
The value of X3 is :1
The value of X1 is :2
The value of X2 is :2
The value of X3 is :2
The value of X1 is :3
The value of X2 is :3
The value of X3 is :3

May anyone out there help me, I'm stuck and I dont know where to begin now. I also accept direct postings to channel.zhou@yahoo.com. Thank you in advance.​

Last edited by a moderator: Apr 2, 2009
2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
Please use code blocks when posting code.

What do you think the output should be and why (i.e. what do you think you are displaying)?

What are you trying to do with this code? :
Code:
```for(int k=0;k<int_array.length;k++)
int_array[k]=j;
```

Joined:
Aug 6, 2004
Messages:
3,009