Results 1 to 3 of 3

Thread: Values in Array

  1. #1
    jazob is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Values in Array

    I need to write a program that will look through a given array and find how many numbers can be expressed as the sum of 3 other numbers in the array.

    Only 6 can be expressed as 3+2+1, so my program should print 1, but it prints 20.

    Java Code:
    public class AddArray
    {
      public static void main ( String[] args )
      {
        int [] array = {1,2,3,4,5,6};
        int count = 0;
        
        /* look through array four times*/
        for (int i =0; i < array.length; i++)
        {
          for (int j = 0; j < array.length; j++)
          {
            for (int a = 0; a < array.length; a++)
            {
              for (int b = 0; b< array.length; b++)
              {
                if (array[i] == array[j] + array[a] + array[b])
                {
                  count++;
                }
              }
            }
          }
        }
        System.out.println(count);
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Values in Array

    Your loops allow for duplicates, meaning it allows for 1 + 1 + 1. Try fiddling with your loop constants to eliminate this. Working out the logic on paper can help.

  3. #3
    jazob is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Values in Array

    I've fixed that problem by changing int a and int b.
    There's another duplicate problem also.
    The counter will increase if the same number is expressed in more than one way.

    I decided to set a, b, and j to the exit conditions whenever the counter increases by one so that I exit out of the three inner loops. That way, I can start from the outermost loop all over again.

    Thanks for pushing me in the right direction.
    Is there any other way to go around this problem?
    Java Code:
    public class AddArray
    {
      public static void main ( String[] args )
      {
        int [] array = {1,2,3,4,5,6,7,8,9,10};
        int count = 0;
        for (int i =0; i < array.length; i++)
        {
          for (int j = 0; j < array.length; j++)
          {
            for (int a = (j+1); a < array.length; a++)
            {
              for (int b = (a+1); b< array.length; b++)
              {
                if (array[i] == array[j] + array[a] + array[b])
                {
                  count++;
                  b = array.length;
                  a= array.length;
                  j = array.length;
                }
              }
            }
          }
        }
        System.out.println(count);
      }
    }
    Last edited by jazob; 12-10-2011 at 02:40 AM.

Similar Threads

  1. Array null values
    By blug in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 07:48 AM
  2. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  3. Pass Values into 2D Array?
    By noble in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 08:30 AM
  4. Multiplying Values of an Array
    By brmcdani in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 05:00 AM
  5. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •