•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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:
Please help me with these two thank you!
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
•
•
Join Date: Jun 2008
Location: Washington
Posts: 606
Reputation:
Rep Power: 3
Solved Threads: 50
•
•
•
•
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--
java Syntax (Toggle Plain Text)
for(int i = 0; i < a.length/2 ; i++) // iterating through the array array.length/2 times
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...
java Syntax (Toggle Plain Text)
// displays the ith element and the ith-to-last element in the array for(int i = 0; i < a.length; i++) System.out.println("Value at ["+ i + "] is " + a[i] + ". Value at [" + ((a.length - 1) - i) + "] is " + a[((a.length - 1) - i)]);
Put these two concepts together and you should be able to solve the problem.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- Pls Help (C++)
- PLS Help newbie (Java)
Other Threads in the Java Forum
- Previous Thread: unable to test JUnit installation
- Next Thread: quick java question(chars)


Linear Mode