# Printing Index of Output Array

• February 6th, 2014, 03:31 AM
mouli
Printing Index of Output Array
Hi All,
I want to Display the Index of output Array.

here is the code:

Code JAVA:

```  import java.util.ArrayList; import java.util.List;   public class selection { private static int[] numbers= { 1, 2, 4, 8, 16, 32, 64, 128, 250, 250, 250, 250, 250, 250, 250 }; private static int[] sumsum= new int[numbers.length];   private static int sum= 1759;   private static void solution(List<Integer> solution) {   System.out.println(solution); }   private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {   if (sum == 0) solution(solution); else for (; i < numbers.length && sumsum[i] >= sum; i++) { if (numbers[i] <= sum) { solution.add(0, numbers[i]); solve(numbers, i+1, sum-numbers[i], solution); solution.remove(0); } } }   public static void main(String[] args) {   for (int s= 0, i= numbers.length; --i >= 0; ) { sumsum[i]= numbers[i]+s; s+= numbers[i]; } solve(numbers, 0, sum, new ArrayList<Integer>()); } }```
• February 6th, 2014, 05:58 AM
Norm
Re: Printing Index of Output Array
Can you explain what you are trying to do and Give an example?

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 6th, 2014, 07:43 AM
mouli
Re: Printing Index of Output Array
I am Just trying to Display all Possible Combinations of given Numbers given on the array which equals the sum value declared as "Sum". Its similar to Coin Change Problem. Now, I want to know which array index is selected in the output. How do i Print that too????
• February 6th, 2014, 08:33 AM
Norm
Re: Printing Index of Output Array
Quote:

I want to know which array index is selected
How is an index selected? Have the method return the index's value.

Quote:

How do i Print that too
System.out.println("theIndex= "+ theIndex);

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 6th, 2014, 08:49 AM
mouli
Re: Printing Index of Output Array
If u Run the above Code The Output will be like this :
"[250, 250, 250, 250, 250, 250, 250, 8, 1]"
Now i want code modified such that it prints like :
"[250, 250, 250, 250, 250, 250, 250, 8, 1]" & index similar to "[numbers[14], numbers[12]....]". help me with coding...
• February 6th, 2014, 08:57 AM
Norm
Re: Printing Index of Output Array
If the code saves the the index values in an ArrayList, they could be printed when desired.

Can you make a simpler example with a small array and a small sum so it is very easy to see what the code is supposed to do.
Then show what the desired output is supposed to be for that simple example.
Using ... for desired output does not say enough.
• February 6th, 2014, 09:02 AM
mouli
Re: Printing Index of Output Array
OK, i have made it simple... Now get me the code for printing the index of output Array...

import java.util.ArrayList;
import java.util.List;

public class selection {
private static int[] numbers= { 1, 2, 2, 4 };
private static int[] sumsum= new int[numbers.length];

private static int sum= 4;

private static void solution(List<Integer> solution) {

System.out.println(solution);
}

private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {

if (sum == 0)
solution(solution);
else
for (; i < numbers.length && sumsum[i] >= sum; i++) {
if (numbers[i] <= sum) {
solve(numbers, i+1, sum-numbers[i], solution);
solution.remove(0);
}
}
}

public static void main(String[] args) {

for (int s= 0, i= numbers.length; --i >= 0; ) {
sumsum[i]= numbers[i]+s;
s+= numbers[i];
}
solve(numbers, 0, sum, new ArrayList<Integer>());
}
}
• February 6th, 2014, 09:30 AM
Norm
Re: Printing Index of Output Array
What is the current output from the simple version?
What is the desired output from the program?

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 6th, 2014, 09:36 AM
mouli
Re: Printing Index of Output Array
My Current Output is

[2, 2]
[4]

Desired Output is

[2, 2]
[numbers[2], numbers[1]]

[4]
[numbers[3]]

This is how my output should be...
• February 6th, 2014, 09:47 AM
Norm
Re: Printing Index of Output Array
Have you looked at saving the indexes in an ArrayList?

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 6th, 2014, 10:09 AM
mouli
Re: Printing Index of Output Array
Yes i saw it. Is there any way i can get the array index of the output. I am new... help me with the coding.
• February 6th, 2014, 10:13 AM
Norm
Re: Printing Index of Output Array
Quote:

help me with the coding
Before starting coding, do some analysis to find out when and where the index values are determined.
Start by finding where the index is available. Add some println() statements to print out an index when it is found.
• February 6th, 2014, 10:18 AM
mouli
Re: Printing Index of Output Array
I am new to Coding in JAVA. It would be so helpful if u send the modified code which prints the indexes of the output.
• February 6th, 2014, 10:22 AM
Norm
Re: Printing Index of Output Array
Sorry, I don't write code for students.
If you did not write the code that you have posted, I suggest that the first thing you need to do is figure out how the code works and add some comments to the code describing how it solves the problem. For example what is in the sumsum array and what is it used for?

--- Update ---

Quote:

I am new to Coding in JAVA.
What computer languages do you know?
• February 6th, 2014, 10:33 AM
mouli
Re: Printing Index of Output Array
I am not a Student... I am an Electronics Engineer. We Have a concept called Load Bank related to Automation Field. In Load Bank we to check whether a generator can withstand huge amount of load supplied. In order to check the load we have give different amounts of load. here is where this coding works, the array field "numbers" actually represents circuit breaker like 1Kw,2Kw,4Kw,250kw etc. and integer "sum" refers to the actual load to be given.

while displaying these combinations, i want to knw which is selected thats why i was asking about printing the array index.

I have made a clear picture on my requirement. Now, can u help me out???
• February 6th, 2014, 10:36 AM
Norm
Re: Printing Index of Output Array
I'm here to help students learn programming. If you are not a student and are not trying to learn programming, I suggest that you hire a programmer to do the programming work you need done.
• February 6th, 2014, 10:40 AM
mouli
Re: Printing Index of Output Array