Page 3 of 7 FirstFirst 12345 ... LastLast
Results 41 to 60 of 136
  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    skip over that one element.
    Use an if statement

  2. #42
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    still not seeing how to do this.. it would help a lot of you could show me some example code...
    right now I am thinking like this...

    Java Code:
    int sum;
    for(int i = 0; i <x.length; i++)
     {
    sum += x[i];
     if(sum%3 != 0)
     {
    sum -= x[i];
     }
     }

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    What is the %3 for? This program has nothing to do with %3. It is supposed to add all the elements in an array except for one element that is not to be added into the total.

    Make a complete program with an array and add a println to show the value of the sum after each execution of the inner loop.
    For example if the array has 3,2,1 the output should be 5, 4 and 3
    Last edited by Norm; 03-07-2012 at 01:27 AM.

  4. #44
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    shouldnt the output be 6 and 3?

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    Write the program, compile it, execute it and see what it prints out.

  6. #46
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    I am sorry, but I have no idea... At a complete loss here... I don't even know what I am supposed to be writing... Just doesn't make sense to me

  7. #47
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Should it go like this... I have an array of 3,2,1... add up the first 2 elements less the first one. So, 5. I got that... but I don't know how to do that so that it will continue to work for the next two... Next add up 3 and 1, then add up 2 and 1. I got that... I just have no idea how to put that down into code

  8. #48
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    See post #39.
    Write a testing program with an array the sums the elements in the array and prints out the sum.
    You have most of that
    Now add an outer loop that surrounds the loop from above and have it loop the same number of times as the inner loop.
    It should print out the sum each time so with an array of 5 elements it will print out the sum 5 times.
    When you get that to work and post the code and the print out from its execution, we'll go to the next step.

  9. #49
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Heres what I got...

    Java Code:
    public static void print()
    		{
    			int[] x = new int[3];
    			int sum = 0;
    			x[0] = 3;
    			x[1] = 2;
    			x[2] = 1;
    			for(int i = 0; i < x.length; i++)
    			{
                                    sum += x[j];
    				System.out.print(sum);
    				for(int j = 0; j <x.length; j++)
    				{
    					
    				}
    			}
    		}
    with an output of
    Java Code:
    356
    Last edited by adjit; 03-07-2012 at 01:49 AM.

  10. #50
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    Your output is very hard to understand. Change it so that you can tell each item that is printed.
    You need to reset the value of sum to 0 for each time you go thru the elements in the array. The output should be 6 every time.

  11. #51
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    sorry, messed up the code a bit. and how?

  12. #52
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    It should be 6 every time?

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    It should be 6 every time?
    What is the sum of 3 2 and 1?
    Fix the code and try again.

  14. #54
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    I can't seem to get it... I've tried a bunch of things. It is never the same number

  15. #55
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Got it!!
    Java Code:
    public static void print()
    		{
    			int[] x = new int[3];
    			int sum = 0;
    			x[0] = 3;
    			x[1] = 2;
    			x[2] = 1;
    			for(int i = 0; i < x.length; i++)
    			{
    				sum = 0;
    				for(int j = 0; j <x.length; j++)
    				{
    					sum += x[j];
    				}
    				System.out.print(sum);
    			}
    		}

  16. #56
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    Now change the code to skip adding in the element when the inner index is the same as the outer index. This skips adding one of the elements tn the array each loop. The printed output should now change.

    Change the print statement to have something between every output, instead of having everything together.

  17. #57
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Java Code:
    public static void print()
    		{
    			int[] x = new int[3];
    			int sum = 0;
    			x[0] = 3;
    			x[1] = 2;
    			x[2] = 1;
    			for(int i = 0; i < x.length; i++)
    			{
    				sum = 0;
    				for(int j = 0; j <x.length; j++)
    				{
    					if(j == i)
    					{
    					}
    					else{
    						sum +=x[j];
    					}
    				}
    				System.out.print(sum + ",");
    			}
    		}
    now I have the output 3,4,5...

    I get that, but what good does that output do me?

  18. #58
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Help with arrays

    Now go back to the original problem and work on that, You now know how to add the elements in an array and skip one each time.

  19. #59
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Right, so thats easy now. But, now how do I print out the new 5 digit number without that number?

  20. #60
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Also, how do I check to see which number is the highest and then print out that set of numbers in the array.

Page 3 of 7 FirstFirst 12345 ... LastLast

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  3. help with arrays
    By droidus in forum New To Java
    Replies: 20
    Last Post: 04-19-2011, 06:29 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

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