Question

Reversing an array is a common task. One approach copies to a second array in reverse,...

Reversing an array is a common task. One approach copies to a second array in reverse, then copies the second array back to the first. However, to save space, reversing an array without using a second array is sometimes preferable. Write a code in java that reads in an integer representing the length of the array. Then read in and reverse the given array, without using a second array. If the original array's values are 2 5 9 7, the array after reversing is 7 9 5 2.

Hints:

  • Use this approach: Swap the first and last elements, then swap the second and second-to-last elements, etc.

  • Stop when you reach the middle; else, you'll reverse the vector twice, ending with the original order.

  • Think about the case when the number of elements is even, and when odd. Make sure your code handles both cases.

Homework Answers

Answer #1

Please find below code and don't forget to give a Like.

Please refer below screenshot for code and output

Code:

public class Main
{
   public static void main(String[] args) {
       int[] array={10,20,30,40,50,60};
       int temp;
       int rev=array.length-1;//from last element we decrement by 1
       int middle_element=array.length/2;
       //printing the original elements
       System.out.println("The original elements in array are:");
       for(int i=0;i<array.length;i++){
       System.out.println(array[i]);
       }
       for(int i=0;i<=middle_element;i++){
       temp=array[i];//storing 1st element in temporary varible
       array[i]=array[rev];//assigning last element into first
       //swaping last element with temporary value which is saved
       array[rev]=temp;
       rev-=1//starting from last element will be decrement by 1
       }
       //printing thr reverse array
       System.out.println("The reverse elements are:");
       for(int i=0;i<array.length;i++){
       System.out.println(array[i]);
       }
   }
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Each person in the group will be responsible for rewriting the code to solve ​one​ of...
Each person in the group will be responsible for rewriting the code to solve ​one​ of the original parts (celebrity names), ​without using arrays or loops.​ The purpose of this assignment is to practice Strings manipulation and String methods so full credit will only be given if you are utilizing the code for Strings (substring, charAt, compareTo, equals, etc). program Java this was my last assigment Take the 4th and the 5th words from part 1 and print out both...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
QUESTION 1 Advanced Security Inc. was hired by the Treasury Bank Inc. for securing their systems....
QUESTION 1 Advanced Security Inc. was hired by the Treasury Bank Inc. for securing their systems. The first thing they did was implement the best practice if separation of domains. As a result of this The bank had to get a new domain name. any change made in the records points to only one party who could have made that change. If you are a technical person, you must have office in a particular area of the building. accessing outside...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
Exploring Innovation in Action Power to the People – Lifeline Energy Trevor Baylis was quite a...
Exploring Innovation in Action Power to the People – Lifeline Energy Trevor Baylis was quite a swimmer in his youth, representing Britain at the age of 15. So it wasn’t entirely surprising that he ended up working for a swimming pool firm in Surrey before setting up his own company. He continued his swimming passion – working as a part-time TV stuntman doing underwater feats – but also followed an interest in inventing things. One of the projects he began...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT