Help on arrays!!

Reply

Join Date: May 2009
Posts: 5
Reputation: ramborambo is an unknown quantity at this point 
Solved Threads: 0
ramborambo ramborambo is offline Offline
Newbie Poster

Help on arrays!!

 
0
  #1
May 11th, 2009
Hey need lots of help on arrays!

this is the exercise...

1. Write a Java application that creates an array of int initialised with the following numbers:

1 4 9 16 17 21 25 33 37 40

and reverses the contents of the array such that the value in the first element is the value that was at the last element and the value in the last element is the value that was in the first element. Below is an example of what the application should produce.

40 37 33 25 21 17 16 9 4 1



public class ArrayExercise1 {
public static void main (String[] args) {


int[] values = new int[10];
values[ 10 ] = 0;

thats my code at the moment, its not finished because im stuck and i really am not sure what to do! please help, i have to use int for it to work!

get back to me! thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Help on arrays!!

 
0
  #2
May 11th, 2009
Originally Posted by ramborambo View Post
Hey need lots of help on arrays!

this is the exercise...

1. Write a Java application that creates an array of int initialised with the following numbers:

1 4 9 16 17 21 25 33 37 40

and reverses the contents of the array such that the value in the first element is the value that was at the last element and the value in the last element is the value that was in the first element. Below is an example of what the application should produce.

40 37 33 25 21 17 16 9 4 1



public class ArrayExercise1 {
public static void main (String[] args) {


int[] values = new int[10];
values[ 10 ] = 0;

thats my code at the moment, its not finished because im stuck and i really am not sure what to do! please help, i have to use int for it to work!

get back to me! thanks!
For starters, you can initialize your array like this:
int[] values = {1,4,9,16,17,21,25,33,37,40};

You can use "values.length" to get the number of elements in the array. Use that in conjunction with a FOR loop and a temp variable to swap the values in the array in reverse order.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help on arrays!!

 
0
  #3
May 11th, 2009
You could also use a stack to reverse the order. Put them in a stack, then pop them, and for each pop, put the element that was popped into an array. The array you end up with is the original one but in reverse order....
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Help on arrays!!

 
0
  #4
May 12th, 2009
Originally Posted by ramborambo View Post
int[] values = new int[10];
values[ 10 ] = 0;
Huge amount of code so far ^_^.

IndexOutOfBoundsException You get here. Remember that, then int You pass in constructor is the number of elements, not the maximum index You can put element on.

Remember that:
new int[ length ] => index' from 0 to length-1.
Last edited by Zibo; May 12th, 2009 at 2:39 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: abbie is an unknown quantity at this point 
Solved Threads: 1
abbie abbie is offline Offline
Newbie Poster

Re: Help on arrays!!

 
0
  #5
May 12th, 2009
Well, it clearly seems to me that you haven't put any effort yourself to solve the problem and hence you've written virtually nothing. I mean, you haven't figured out that you need to use a loop in order to change the whole array, let alone what to do in the loop.

Here's a hint: (think about it). It's just one approach. simple problems can be solved in many different and correct ways.
use a loop which goes from 0 to some index. (that some index is not the last element of the array). In each iteration of the loop, swap the first and last element.
You need to UNDERSTAND two things:
* if you assign the value of the first to last or vice versa, you'd lose data of one of them which you'd need later. So, you need to devise some technique to temporarily save the current element somewhere and yet change the values.
* if you change the value of the first and last and go till the last index, you would end up swapping the first with the last and the last with the first, which will be the original array again.

Please try it now. I hope I helped.

P.S.: Sorry for making my post a bit too lengthy.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: ramborambo is an unknown quantity at this point 
Solved Threads: 0
ramborambo ramborambo is offline Offline
Newbie Poster

Re: Help on arrays!!

 
0
  #6
May 12th, 2009
right guys, ive made some progress on it.

the only problem i have now is how to reverse the numbers in descending order.
public class ArrayExercise1 {
public static void main(String[] args) {
int[] values = new int[10];

values[ 0 ] = 1;
values[ 1 ] = 4;
values[ 2 ] = 9;
values[ 3 ] = 16;
values[ 4 ] = 17;
values[ 5 ] = 21;
values[ 6 ] = 25;
values[ 7 ] = 33;
values[ 8 ] = 37;
values[ 9 ] = 40;


for(int count = 0; count < values.length; count++) {

System.out.println(values[ count ]);

}
}

I have that so far and it shows the number from smallest to largest, now im stuck on how to get them reversed.

Please get back to me! thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help on arrays!!

 
0
  #7
May 12th, 2009
Abbie - try to be nice.

Ramborambo:

Use another for loop, but iterate backwards through the array. As you iterate backwards, add each element to a different array. Use this for loop, and inside the for loop, add code that will put the element at the current index into a different array. If you do a search in the Java forum, you'll find that someone asked an almost identical question to the one you asked.
  1. for (int i = array.length-1; i >=0; i--)
Last edited by BestJewSinceJC; May 12th, 2009 at 9:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC