1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kobusy [5.1K]
3 years ago
14

Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an arr

ay and print the array. Then reverse the array elements so that the first element becomes the last element, the second element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in which they are printed; actually change the way they are stored in the array. Do not create a second array; just rearrange the elements within the array you have.
Computers and Technology
1 answer:
Svetllana [295]3 years ago
3 0

Answer:

//The Scanner class is imported to allow the program receive user input

import java.util.Scanner;

//The class Solution is defined

public class Solution {

   //The main method is defined here and signify the beginning of program execution

   public static void main(String args[]) {

       

       //Scanner object 'scan' is created to receive user input

       Scanner scan = new Scanner(System.in);

       //Prompt display to the user to enter size of array

       System.out.print("Enter the range of array: ");

       //User input is assigned to arraySize

       int arraySize = scan.nextInt();

       //userArray is initialized with arraySize as its size

       int[] userArray = new int[arraySize];

       

       //counter to count number of array element

       int count = 0;

       //while loop which continue executing till the user finish entering the array element

       while (count < arraySize){

           System.out.print("Enter each element of the array: ");

           userArray[count] = scan.nextInt();

           count++;

       }

       

       //A blank line is printed for clarity

       System.out.println();

       

       //for loop to print each element of the array on straight line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

       

       //A blank line is printed for clarity

       System.out.println();

       

       //for loop is use to reverse the array in-place

       for(int i=0; i<userArray.length/2; i++){

           int temp = userArray[i];

           userArray[i] = userArray[userArray.length -i -1];

           userArray[userArray.length -i -1] = temp;

       }

       

       //for loop to print each element of the reversed array on straight line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

     

   }

}

Explanation:

The program is commented to give detailed explanation.

The for-loop use in reversing the array works by first dividing the array into two half and exchanging the first half elements with the second half elements. The element from the first half is assigned to temp variable on each loop, then the element is replaced with the equivalent element from the second half. And the element from the second half is replaced with the value of temp.

You might be interested in
____ is a style of programming that focuses on the step-by-step sequence of instructions and operations.
mario62 [17]

Answer: Algorithm

Explanation: An algorithm is a plan, a set of step-by-step instructions to solve a problem. it involves three basic building blocks to use when designing algorithms. these are.,

1. Sequencing

2. Selection

3. Iteration

7 0
3 years ago
Read 2 more answers
Why might a variable used for output have to be of a different type then a variable used for input?​
telo118 [61]

Answer:

You may use a different variable type for input in order to process the data appropriately and may use a different variable type to accommodate your program.

Explanation:

Your input may have to be different then output varying on what data you are processing.

For example, just like the last question you asked about calculating the area of the rectangle, your input MUST be converted to a different a numerical data type (i.e int or float) in order to do the math.

Based on your last question, if we didn't convert your input into a string your results wouldn't add the numbers together but only concatenate them. If I typed 3 & 5 as length & width, I would get 35 instead of 15.

Now another example is using functions (or methods whatever you want to call them), your input can be one thing and outputs another.

Let's say I want a function to tell me if I am smart. The way I want it to determine if its smart is by inputting my GPA into it, which is a numerical value. So the function would look something like this:

<u>Code (Python)</u>

def IsSmart(gpa):

  if (gpa >= 4):

       return True

  else

       return False

As you can see I am inputting a numerical value and it is outputting a boolean value. I can use this in a if else chain to spit out an output to the user.

7 0
3 years ago
how can a user open a blank presentation? 1.on the file menu, click new, and then click recent templates 2.on the file menu, cli
4vir4ik [10]

2.  on the file menu, click new, and then click blank presentation.

5 0
3 years ago
Complete the sentence. Assigning a data value to a variable in a program is referred to as______.
luda_lava [24]

Answer:

The answer to this question is given below in the explanation section.

Explanation:

The correct answer to this question is initialization.

When you write a variable in a program,  for example int firstNumber. The integer variable firstNumber is declaring in the program.

when you assign a data value to a variable in a program is referred to as initialization. for example:

firstNumber=10;

You can declare and initialize the variable in one statement such as:

int firstNumber=10;

6 0
3 years ago
Which of the following tool tab have more than one related subordinate tab??
Juliette [100K]
Table tool? i think because it gives more info
8 0
3 years ago
Read 2 more answers
Other questions:
  • toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the
    6·1 answer
  • Which task is a part of the analysis phase of the SDLC
    9·2 answers
  • Crop marks are used on an illustration to indicate to the printer the
    10·1 answer
  • Kyle asked his supervisor which type of computing model was used when the enterprise first started. She explained that the organ
    14·1 answer
  • Write a programme with C++ language wich print the biggest number in between three numbers , whith INT
    14·1 answer
  • You are given the task of reading in n numbers and then printing them out in sorted order. Suppose you have access to a balanced
    12·1 answer
  • 1. Do our shared social experiences lead us to think<br><br> communication is a cure-all?
    13·1 answer
  • Consider this binary search tree:______.
    12·1 answer
  • The idea that an object can exist separate from the executing program that creates it is called
    13·1 answer
  • I need help on this, need answer asap.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!