1. ## Re: Help with arrays

skip over that one element.
Use an if statement

2. 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];
}
}```

3. ## 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 02:27 AM.

4. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

shouldnt the output be 6 and 3?

5. ## Re: Help with arrays

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

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

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

8. ## 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## 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 02:49 AM.

10. ## 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

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

12. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

It should be 6 every time?

13. ## 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. 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

15. 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);
}
}```

16. ## 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## 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. ## 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. 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?

20. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## 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 First 12345 ... Last

#### Posting Permissions

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