Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Printing Index of Output Array

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Printing Index of Output Array

    Hi All,
    I want to Display the Index of output Array.

    here is the code:

     
    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>());
        }
    }
    Last edited by Norm; February 6th, 2014 at 08:43 AM. Reason: removed spaces in code tag


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Printing Index of Output Array

    Can you explain what you are trying to do and Give an example?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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????

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Printing Index of Output Array

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

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

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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) {
    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>());
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Printing Index of Output Array

    What is the current output from the simple version?
    What is the desired output from the program?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Printing Index of Output Array

    Have you looked at saving the indexes in an ArrayList?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Printing Index of Output Array

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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 ---

    I am new to Coding in JAVA.
    What computer languages do you know?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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???

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing Index of Output Array

    Thanks for your help....

Similar Threads

  1. Printing Output from an array in a gui form.
    By chizzo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 07:50 AM
  2. [SOLVED] Which sort method to use for a String Array to output the index?
    By DANGEROUSSCION in forum Algorithms & Recursion
    Replies: 11
    Last Post: December 8th, 2012, 09:25 PM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM