# Processing Arrays

• June 26th, 2011, 03:57 PM
av8
Processing Arrays
I have an int array called oddNumbers: int [] oddNumbers = {1,3,5,7,9}

I want to process oddNumbers in the following way;

For each element of oddNumbers (except the last element at index 4 i.e 9 ) to copy into that element
the value from the element to its immediate right.

This means that the value in index 1 (i.e 3) is copied into index 0, the value in index 2 is
copied into index 1 and so on, until the element at index 4 is copied into index 3.
The contents of the rightmost index 4 (i.e 9) will remain unaffected.

After processing the array oddNumbers, it should have this appearance: int [] oddNumbers = {3,5,7,9,9}

I would be grateful for some help in coding to achieve this.

Best wishes

av8.
• June 26th, 2011, 07:34 PM
copeg
Re: Processing Arrays
Please do not post the same thing twice to the forums - your other post has been removed.

If you are looking for precise help, I recommend you give this a try (hint, a for loop might get you started), and when your stuck post the code you have with any and all errors messages and a precise description of the problem. Hoping someone will answer this without showing you've made an attempt is typically quite unproductive.
• June 26th, 2011, 07:34 PM
Norm
Re: Processing Arrays
For this kind of problem, take a piece of paper and a pencil. Draw the array with the numbers.
Put a pointer under the source element, note the position/index.
Put a pointer under the target element, note its position.
Move the element at the source to the target by erasing the target value and writing in a copy of the source element.
Change the values of the indexes to do the next step.

Now look at how you had to change the index values to get to the next step.
That will tell you how to write a program to do this.
• June 27th, 2011, 03:22 AM
av8
Re: Processing Arrays
I appologise copeg. Having posted my problem on what's wrong with my code I thought the arrays/collections might be a more appropriate forum.

Looking at the paper/pencil sketch Norm the index/position decrements by -1 ?

Best regards

av8
• June 27th, 2011, 06:19 AM
Norm
Re: Processing Arrays
After you get the idea of how to move the index, have you tried writing the code?