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
Alja [10]
3 years ago
11

Write a recursive function that has an argument that is an array of characters and two arguments that are the bounds (inclusive)

on array indices. The function should reverse the order of those entries in the array whose indices are between the two bounds. For example, if the array is a[0]: ‘A’ a[1]: ‘B’ a[2]: ‘C’ a[3]: ‘D’ a[4]: ‘E’ and the bounds are 1 and 4, then, after the function is run the array elements should be a[0]: ‘A’ a[1]: ‘E’ a[2]: ‘D’ a[3]: ‘C’ a[4]: ‘B’ Consider the following declaration for your function: void flipArray( char arr[], unsigned int from, unsigned int to ); Test your code before writing it out. You may safely assume that from and to are valid indices with respect to arr.

Computers and Technology
1 answer:
diamong [38]3 years ago
7 0

Answer:

Here is the C++ program:

#include <iostream>   // for input output functions

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

/* recursive function to reverse the order of array elements between two bound of array indices */

void flipArray(char arr[], unsigned int from, unsigned int to)  {  

       if (from >= to)  // base case

         return;  

   swap(arr[from], arr[to]);  

//function to swap characters according to specified values in from and to

   flipArray(arr, from + 1, to - 1);  } // recursive function call

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

   int size;  //stores the number of elements in array

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

   cin>>size;  // reads array size from user

   

   char array[size];  // array of size specified by user

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

// loop to take input characters in array according to specified size

   for (int i = 0; i < size; ++i)  

   {cin >> array[i];}  // reads array elements from user

       

   unsigned int i,j;  // stores values of two bounds of indices

   cout<<"Enter the two bounds of array indices:";

// prompts user to enter two bounds of array indices

   cin>>i>>j;  //reads array indices from user

//displays original array elements    

   cout << "The original array is: "<<array<< endl;

   flipArray(array,i,j);  //calls flipArray() function

   cout << "Array after flip from "<< i<<"to "<<j<<" is: "<<array << endl;  }

/*displays elements of array after reversing array characters according to bounds of array indices */

Explanation:

The program is explained in the given comments with every statement. Lets understand the logic of the recursive function for the input array that has characters: A B C D E and two bounds 1 and 4 i.e. from B to E

  • First the base condition if (from >= to) is checked. Here the value of from is 1 and value of to is 4. So the base condition evaluates to false as 1 < 4

So the program control moves to the next statement: swap(arr[from], arr[to]);  which swaps the array characters from the two indices 1 and 4.

arr [1] = B, arr [4] = E

So B and E are swapped.

A E C D B

Next statement: flipArray(arr, from + 1, to - 1);  is a call to recursive function flipArray,

from + 1 = 1+1 = 2 So now the from index moves one position ahead

to - 1 = 4 - 1 = 3 So now the to index moves one position behind

So new value of from = 2 and to = 3

  • The base condition is check again which evaluates to false as 2<3

The swap() function swaps the elements at indices 2 and 3 which means:

arr[2] = C

arr[3] = D

So C and D are swapped.

A E D C B

Next is the call to recursive function flipArray()

from + 1 = 2 + 1 = 3

to - 1 = 3 - 1 = 2

So the value of from = 3 and value of to = 2

  • Now the base condition is checked again and it evaluates to true as 3>2

So this is the stop condition.

Hence the array after flip is:

A E D C B

The program along with its output is attached in the screen shot.

You might be interested in
1. The running configuration is also known as the _____________ (Select Two) a. Startup config b. Working configuration c. Curre
Shtirlitz [24]

Answer:

1. The running configuration is also known as the <u><em>b. working configuration</em></u>

<u><em>c. current configuration</em></u>

Explanation:

hopes this help (:

6 0
2 years ago
Citing the recent increase in earnings by several computer companies, economists feel that a cycle has begun in which personal c
VashaNatasha [74]

Answer:

A) a cycle has begun in which personal computer users

Explanation:

Option A as already used in the question fits and and completes the question correctly, this is because the because the writter is trying to communicate a new 'era', 'dispensention', 'period' or 'season'.... this is appropriately communicated with the word 'cycle'.

5 0
4 years ago
The acronym is used to define the process that allows multiple devices to share a single routable ip address.
blondinia [14]
The answer is Mac Address
media access control address<span> </span>
4 0
4 years ago
Which of the following commands would instruct OSPF to advertise ONLY the 192.168.10.0/24 network in Area 0?
xz_007 [3.2K]

Answer:

First, you need to start the OSPF process by executing the command:

<em>Router (config)# router ospf {process-ID#}</em>

Then, you have to instruct the router to advertise the networks that are directly linked to it by entering this network command with the area ID number for that network:

Router (config-router)# network {192.168.10.0} {255.255.255.0} area {0}

8 0
4 years ago
Which source would provide the best way to find valid information about climate change
igor_vitrenko [27]
Primary sources would be best
4 0
3 years ago
Other questions:
  • Create and apply a css class named bigblue, which selects a large, blue-colored font.
    11·1 answer
  • Match the number in the picture to the correct Excel interface part. Each number is only used one time.
    11·1 answer
  • PLS ANSWER ASAP!
    9·1 answer
  • How can a professional association help you reach your career goals?
    5·1 answer
  • Copy the following code and run it. You should break it into the following 3 functions
    6·1 answer
  • i set up an account and paid the yearly fee, now it's asking me to join. i've tried to log in and brainly isn't accepting my ema
    8·1 answer
  • Why is it important to have at least one backup stored off-site?
    8·1 answer
  • What happens to a data table when the formulas or variables used to create it are changed?
    9·2 answers
  • What can provides access to the only menu in office 2007​
    13·1 answer
  • How would you assign roles to your group members?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!