Results 41 to 60 of 136
Thread: Help with arrays
- 03-07-2012, 12:49 AM #41
- 03-07-2012, 12:56 AM #42
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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]; } }
- 03-07-2012, 01:00 AM #43
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 3Last edited by Norm; 03-07-2012 at 01:27 AM.
- 03-07-2012, 01:05 AM #44
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
shouldnt the output be 6 and 3?
- 03-07-2012, 01:26 AM #45
Re: Help with arrays
Write the program, compile it, execute it and see what it prints out.
- 03-07-2012, 01:32 AM #46
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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
- 03-07-2012, 01:35 AM #47
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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
- 03-07-2012, 01:42 AM #48
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.
- 03-07-2012, 01:46 AM #49
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
Heres what I got...
with an output ofJava 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++) { } } }
Java Code:356
Last edited by adjit; 03-07-2012 at 01:49 AM.
- 03-07-2012, 01:50 AM #50
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.
- 03-07-2012, 01:52 AM #51
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
sorry, messed up the code a bit. and how?
- 03-07-2012, 01:54 AM #52
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
It should be 6 every time?
- 03-07-2012, 01:55 AM #53
Re: Help with arrays
What is the sum of 3 2 and 1?It should be 6 every time?
Fix the code and try again.
- 03-07-2012, 02:13 AM #54
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
I can't seem to get it... I've tried a bunch of things. It is never the same number
- 03-07-2012, 02:20 AM #55
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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); } }
- 03-07-2012, 02:27 AM #56
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.
- 03-07-2012, 02:42 AM #57
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
now I have the output 3,4,5...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 + ","); } }
I get that, but what good does that output do me?
- 03-07-2012, 02:49 AM #58
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.
- 03-07-2012, 03:09 AM #59
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Re: Help with arrays
Right, so thats easy now. But, now how do I print out the new 5 digit number without that number?
- 03-07-2012, 03:09 AM #60
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Similar Threads
-
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
help with arrays
By droidus in forum New To JavaReplies: 20Last Post: 04-19-2011, 06:29 AM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks