C++ I have a rectangle object with two sides and need a function to recursivly i
ID: 3663685 • Letter: C
Question
C++ I have a rectangle object with two sides and need a function to recursivly increase each side member variable 'n' times.
void incSides(int n);
will increase the length of each side n times recursively (see description below)
For the function incSides(int n) you must come up with a recursive solution to increase each side n times. Yes, this is the same as multiplying each side by n. Yes, this could be done in a loop very easily. No, you may not do either of those things. This is to practice recursion so your solution must use recursion. Remember that each recursive function needs to have a base case(s) and a recursive step.
Explanation / Answer
Let's say , Rectangle is the name of class and 'rectangle' is a object.
class Rectangle {
public:
int length;
int width;
};
public void incSide(int n) {
if( n ==0){ // Base Case
return;
}else{
rectangle.length += 1;
rectangle.width += 1;
incSide(n-1); // this is recursive call
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.