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
Modify the closest pair of points algorithm so that the separating line L now separates the first n/4 points (sorted according t
Nutka1998 [239]

Answer:

Check the explanation

Explanation:

Kindly check the attached images below to see the step by step explanation to the question above.

3 0
2 years ago
A company with a large number of hosts creates three subdomains under a main domain. For easier management of the host records,
REY [17]

Answer:

4

Explanation:

For easier management of the host records of the company 4 zones should be used because

subdomain is part of a larger domain and the main domain which is the primary domain is the name which the company have decide to use which will represent the company website address and in a situation where the company

have different domain names in which they had registered, they will need to choose one among the domain which will inturn be their main domain.

Therefore for easier , efficient and effective management of the host records 4 zones will be the best zones to be used.

Example of sub domain is north.example.com

4 0
2 years ago
I need to write a really simple python program to solve this issue. I have most of the program written but, when I run the secon
denis-greek [22]

Answer:

hour = float(input("Enter hour:"))

minute = float(input("Enter minute:"))

second = float(input("Enter second:"))

am_pm = input ("Enter AM or PM:")

if am_pm == "AM":

   if hour == 12:

       hour = 0

   seconds_since_midnight = ((3600 * hour) +(minute *60) + second)

elif am_pm == "PM":

   seconds_since_midnight = ((3600 * (hour+12)) +(minute *60) + second)

print("Seconds since midnight:", int(seconds_since_midnight))

Explanation:

The point here is when PM is chosen, you need to add 12 to the hour. I added <em>elif</em>  part to satisfy this. Moreover, since the output is in integer format, you may need to apply type casting for <em>seconds_since_midnight</em> variable.

4 0
1 year ago
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integer
Naya [18.7K]

Answer:

The program to this question as follows:

Program:

#include<iostream> //include header file.

using namespace std; //using name space.

int main() //main function.

{

int n,i,key; //define variable.

int a[n]; //define array

cout<<"Enter size of array :"; //message.

cin>>n; //input number from user.

cout<<"Enter array elements :"; //message

for(i = 0;i<n;i++) //loop

{

cin>>a[i]; //input array elements

}

cout<<"Enter a number:"; //message

cin>>key; //input number.

for(i = 0;i<n;i++) //loop

{

if(a[i]<=key) //if block

{

cout<<a[i]<<"\n "; //print array elements

}

}

return 0;

}

Output:

Enter size of array :7

Enter array elements :5

50

50

75

100

200

140

Enter a number:100

5

50

50

75

100

Explanation:

The description of the above program as follows:

  • In this program first, we include a header file and define the main method in method we define variables and array that are "n, i, key and a[]". In this function, all variable data type is "integer".
  • The variable n is used for the size of array and variable i use in the loop and the key variable is used for comparing array elements. Then we use an array that is "a[]" in the array, we use the for loop to insert elements form user input.  
  • Then we define a loop that uses a conditional statement in if block we check that array elements is less than and equal to a key variable. If this value is true so, we will print the remaining elements.  

4 0
1 year ago
When a switch configuration includes a user-defined error threshold on a per-port basis, to which switching method will the swit
nikklg [1K]
STORE-AND-FORWARD is the switching method that the switch will revert to when the error threshold is reached. This happens when the switch configuration includes a user-defined error threshold on a per-port basis. 


7 0
2 years ago
Other questions:
  • The memory allocated for a float value is ____ bytes.
    9·1 answer
  • Refer to the exhibit. pc1 issues an arp request because it needs to send a packet to pc3. in this scenario, what will happen nex
    9·1 answer
  • What is a typical grace period for a credit card...
    15·2 answers
  • python Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value i
    7·1 answer
  • _____________ involves the deployment of malware that secretly steals data in the computer systems of organizations, such as gov
    13·1 answer
  • The following checksum formula is widely used by banks and credit card companies to validate legal account numbers: d0 + f(d1) +
    13·1 answer
  • Define a method named roleOf that takes the name of an actor as an argument and returns that actor's role. If the actor is not i
    8·1 answer
  • The PictureBook class is a subclass of the Book class that has one additional attribute: a String variable named illustrator tha
    9·1 answer
  • Checkpoint 10.43 Write an interface named Nameable that specifies the following methods: _______{ public void setName(String n)
    12·1 answer
  • Which business case is better solved by Artificial Intelligence (AI) than conventional programming
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!