1. chmod: WARNING: can\'t access prog.cpp chmod: WARNING: can\'t access ans.cpp
ID: 3733648 • Letter: 1
Question
1.
chmod: WARNING: can't access prog.cpp
chmod: WARNING: can't access ans.cpp
#include <iostream>
#include <iomanip>
using namespace std;
// ------------------------------------------------------------------
// Function: Complete the following RECURSIVE function to reverse the
// elements stored in array A, at subscripts first to last.
//
// Example: Arguments: {2,3,4}, 0 2 ==> {4,3,2}
// Arguments: {12,4,8,5,8}, 0 4 ==> {8,5,8,4,12}
// Arguments: {4,8,5,8}, 0 3 ==> {8,5,8,4}
// Arguments: {}, 0 0 ==> { }
// ------------------------------------------------------------------
int _t = 0; // DO NOT TOUCH!
int _T(int q){return _t>= q ? 1 : 0;} // DO NOT TOUCH!
void Swap(int & x, int & y){int temp=x; x=y; y=temp;}
void Reverse(int A[], int first, int last)
{ _t++; // DO NOT TOUCH!
}
#include "mainptalgm9500rec.cpp"
Explanation / Answer
// method reverse the array elements form first to last index.
//here first shoud be less than last else it return the same array
void Reverse(int A[], int first, int last){
_t++; // DO NOT TOUCH!
if(last - first <= 0) return; // check for first to last is already reversed
Swap(*(A+first),*(A+last)); // swapping function call for swappinf first and last elements
if(_T( (last - first + 1)/2 ) ) // check for reverse is done or not. For reversing four elemnets we need only
return; //two swaps. for 3 elements reverse we need only swap.i.e no.of elements/2 swaps.
Reverse(A,first+1, last-1); // recursive call for swapping first+1 and last-1 elements.
} // end method
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.