answer.
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
iogann1982 [59]
1 year ago
8

g 18.6 [Contest 6 - 07/10] Reverse an array Reversing an array is a common task. One approach copies to a second array in revers

e, 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 function 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. Write a method to reverse the input array and call the method in the main. Note: The first item in the input is the length of the input array. The rest of the items in the input are the elements stored inside of the input array. 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.

Computers and Technology
1 answer:
Blababa [14]1 year ago
3 0

Answer:

I am writing C++ program. Let me know if you want the program in some other programming language.  

#include <iostream> //to use input output functions

using namespace std; //to identify objects like cin cout

void reverse(int array[], int size){ //method to reverse an array  

int i, j, temp; //declare variables  

for (i = 0; i < size / 2; i++) { /* the loop swaps the first and last elements, then swap the second and second-to-last elements and so on until the middle of the array is reached */  

temp = array[i];  

array[i] = array[size - i - 1];  

array[size - i - 1] = temp; }  

cout<<"Reversed array: "; //prints the resultant reversed array after swapping process  

for (j = 0; j < size; j++) { //loop to print the reversed array elements  

cout<<array[j]<<" "; } }    

int main() { //start of main() function body  

cout<<"Enter the length of array: "; //prompts user to enter the length of the array

       int n; //stores the value of size of array

       cin>>n; //reads the input size of array

       int A[n]; //declare array A of size n

       cout<<"Enter the elements: "; //prompts user to enter array elements

       for(int i=0;i<n;i++){ //loop for reading the elements of array

           cin >> A[i];        }

         reverse(A,n); } //calls reverse function to reverse the elements of the input array A by passing array A and its length n to function reverse

Explanation:  

The reverse function takes an array and its length as parameter. In the body of reverse function three integer type variables i,j and temp are declared.

for (i = 0; i < size / 2; i++) The for loop has a variable to iterate through the array. The for loop checks if the value of i is less than the array length. Lets say we have an array of elements 1, 2, 3, 4. As i=0 and size/2 = 4/2= 2. So the condition checked in for loop is true as 0<2 which means the body of for loop will be executed.

In the body of the loop there are three swap statements.

temp = array[i] This statement stores the element of array at i-th index to temp variable.

array[i] = array[size - i - 1] statement stores the element of array at size-i-1 to array index i.  

array[size - i - 1] = temp statement stores the the value in temp variable to the array index size-i-1

In simple words first, the first and last elements of array are swapped, then the second and second last elements are swapped. This process keeps repeating and for loop keeps executing these swap statements until the middle of the array is reached. In this program no extra array is used for reversing procedure but just a variable temp is used to hold the array elements temporarily.

array[4] = { 1 , 2 , 3 , 4}  

At first iteration  

i < size/2 is true as 0<2  

So the program control moves in the body of the loop where three swap statement works as following:  

temp = array[i]  

temp = array[0]  

As element at 0-th index which is the first element of array is 1 so

temp= 1  

array[i] = array[size - i - 1];  

array[0] = array[4-0-1]  

           = array[3]  

As the element at 3rd index of array is 4 which is the last element of the array so

array[0] = 4

This means that 4 becomes the first element of array now.

array[size - i - 1] = temp  

As the value in temp was set to 1 so  

array[4-0-1] = 1  

array[3] = 1  

This means 1 is assigned to the 3rd index of array. Which means 1 becomes the last element of the array now.

This is how the first and last elements of array are swapped.  

2nd iteration: value of i is incremented to 1 and now i = 1  

Again for loop condition is checked which evaluates to true as 1<2

temp = array[i]  

temp = array[1]  

As element at 1st index which is the second element of array is 2 so

temp= 2  

array[i] = array[size - i - 1];  

array[1] = array[4-1-1]  

           = array[2]  

As the element at second index of array is 3 which is the last element of the array so

array[1] = 3  

This means that 3 becomes the second element of array now.

array[size - i - 1] = temp  

As the value in temp was set to 2 so  

array[4-1-1] = 2  

This means 2 is assigned to the 2nd index of array. Which means 2 becomes the second last element of the array now.

This is how the second and second to last elements of array are swapped.  

3rd iteration: value of i=2 The for loop body will not execute now as the for loop condition i<size-2 evaluates to false because 2=2. So the loop stops and next for (j = 0; j < size; j++) loop iterates through array[] which has now been reversed and print the elements of this reversed array.

Next the main() method passes length of the array and the array elements to the   reverse function and prints these elements in reverse.  

The output is:

4  3 2  1  

The program and output according to the example 2,5,9,7 given in the question is attached as a screenshot.

You might be interested in
Which of the following are examples of algorithms? (Select all that apply, if any do.)
Travka [436]

Answer:

A. The mathematical formula for finding the area of a circle.

D. Instructions for setting up a new DVD player

E. A series of steps that moves a "lighting bot" around to turns on lights.

Explanation:

Algorithm is a systematic procedure (or process) that produces the answer to a question or the solution of a problem in a finite number of steps.

Option A is an algorithm. The mathematical formula for finding the area of a circle gives a solution to a problem. The problem is finding area of a circle and the formula gives the step by step procedure finding the area of a circle.

Option B is not an algorithm. Option B just provide a list of your favourite animals and it doesn't answer a question we know about.

Option C is not an algorithm. A problem statement is still a problem with no solution whereas algorithm provides solution

Option D is an algorithm. It provides the step by step procedures for setting up a new DVD player

Option E is an algorithm. It provides a series of steps that moves a lighting bot around to turns on lights, so it provides a solution.

4 0
2 years ago
Read 2 more answers
Jason has decided to use his name on all the images to protect them from misuse. He has also decided to use the logo of his comp
Mila [183]

Answer:

Text watermarking involves adding the photographer's name at the corner of the image.

Explanation:

This was one of the correct answers when I took the test, I got it wrong because I didn't pick all the correct answers. I hope that this was helpful to you! If you know the other correct answer(s) please comment and let me know and i'll edit this answer! :)

6 0
2 years ago
Read 2 more answers
Which technology can be used to protect the privacy rights of individuals and simultaneously allow organizations to analyze data
iren [92.7K]

Answer:

De-identification or data anonymization.

Explanation:

Privacy rights are fundamental right of individuals to privatise all personal information, when creating an account.

The de-identification and data anonymization technology is provided by the organisation to user, to prevent their information to be viewed by others. It commonly used in cloud computing, communication, internet, multimedia etc. Reidentification is the reversing of the de-identification effect on personal data.

4 0
2 years ago
Which of the following would not be considered metadata for a spreadsheet file?
Genrish500 [490]

Answer:

B.

Explanation:

Metadata is a type of data that dispense details of other data. In simple terms, metadata can be defined as information of a file such as file name, attributes, etc.

<u>In excel sheet, metadata works the same and helps to provide information about </u><u>file name, author name, file size, attributes such as read-only, archieve, hidden, system, location of the file, date and time of creation, modification, or accessed, type of file, etc</u><u>.</u>

From the given options, the information that is not considered or included in metadata is calculation inside the file. Metadata in excel sheet does not include calculations inside the file. Thus option B is the correct answer

7 0
2 years ago
The data in a data warehouse have which of the following characteristics?
Burka [1]

Answer: Option(d) is correct

Explanation:

Data warehouse is the storage that holds collected information and data for making acknowledged decision through analyzing the data. Data present in data warehouse is as per subject which contains history and sources of data for understanding and perceiving it.

  • Other options are incorrect because data is not coded in various forms,retrieved for certain period of time, real-time update and arrangement in hierarchical form.
  • Thus, the correct option is option(d).
6 0
2 years ago
Other questions:
  • A circuit contains three resistors connected in parallel. The value of R1 is 2 K , the value or R2 is 6 K , and the value of R3
    5·1 answer
  • Multiple boolean expressions can be combined by using a logical operator to create ________ expressions.
    10·1 answer
  • There are no breakpoints in the "Access Customer Account" subpage however there is an error. What will happen if you choose to s
    11·1 answer
  • Write a loop that counts the number of space characters in a string. Recall that the space character is represented as ' '.
    11·1 answer
  • A news channel runs a two-minute story on phishing scams. The report features the testimonies of a phishing victim and a compute
    7·1 answer
  • Problem 2 - K-Best Values - 30 points Find the k-best (i.e. largest) values in a set of data. Assume you are given a sequence of
    13·1 answer
  • Assume that machines A and B are on the same network 10.3.2.0/24. Machine A sends out spoofed packets, and Machine B tries to sn
    14·1 answer
  • Select the correct navigational path to freeze the top row of your worksheet. Select the panes you wish to freeze. Click the tab
    15·2 answers
  • Identify a data type applied in an input element to create slider controls.
    13·1 answer
  • Write a static generic method PairUtil.minmax that computes the minimum and maximum elements of an array of type T and returns a
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!