User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,068 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,236 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 200 | Replies: 2
Reply
Join Date: Jul 2008
Posts: 1
Reputation: Da24K is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Da24K Da24K is offline Offline
Newbie Poster

Array Reverse -need help pls

  #1  
30 Days Ago
Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value

So far I got:
 public void reverse(int[] backward)
{
for(int i = (backward.length -1); i >=0;i--){
}
}

I think I'm suppose to swap forward array and reverse arrays some how but I don't how to go about doing that like in this one

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , k , and temp .

so far i got:
int temp = a.length;
for (k = 0; k < temp; k++)
{
a[temp - 1] = a[k];
}

Please help me with these two thank you!
Last edited by Tekmaven : 30 Days Ago at 3:34 am. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: Washington
Posts: 606
Reputation: Alex Edwards will become famous soon enough Alex Edwards will become famous soon enough 
Rep Power: 3
Solved Threads: 50
Alex Edwards Alex Edwards is online now Online
Practically a Master Poster

Re: Array Reverse -need help pls

  #2  
30 Days Ago
Originally Posted by Da24K View Post
Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value

So far I got:
[code=java]
public void reverse(int[] backward)
{
for(int i = (backward.length -1); i >=0;i--){
}
}
[\code]

I think I'm suppose to swap forward array and reverse arrays some how but I don't how to go about doing that like in this one

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , k , and temp .

so far i got:
[code=java]
int temp = a.length;
for (k = 0; k < temp; k++)
{
a[temp - 1] = a[k];
}
[\code]

Please help me with these two thank you!


Don't forget to add "=java" without quotes, when declaring a code block.

This isn't too hard of an assignment. If you think about the commands of an array and the hint the Instructor gave you, you should be capable of doing this.

Keep in mind that you have to reverse values (the first and the last) until you reach the middle of the array.

By deduction, you can determine that it should take half as many iterations to reverse the values than to iterate through the entire array--

  1.  
  2. for(int i = 0; i < a.length/2 ; i++) // iterating through the array array.length/2 times
  3.  

As for swapping, you are always reversing the value from the front to the back, then the 2nd one with the 2nd to last, then the third one from the third to last...

  1.  
  2.  
  3. // displays the ith element and the ith-to-last element in the array
  4. for(int i = 0; i < a.length; i++)
  5. System.out.println("Value at ["+ i + "] is " + a[i] + ". Value at [" + ((a.length - 1) - i)
  6. + "] is " + a[((a.length - 1) - i)]);
  7.  

Put these two concepts together and you should be able to solve the problem.
Reply With Quote  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Array Reverse -need help pls

  #3  
30 Days Ago
and for tags it is /code at the end, not \code
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC