Answer to Question #323275 in C++ for Magisk

Question #323275
  1. In the code editor, you are provided with a main() function that asks the user for two integers and calls the slowDisplay() function.
  2. This slowDisplay() function is a recursive function which should perform the functionality explained in the problem description above.
  3. This function is only partially implemented as it lacks a base case. Your task is to add the base case needed by this recursive function.

Example:

Starting num = 2

Next num = 3

It should then print 3 4 5


Given code:

#include <iostream>

using namespace std;


void slowDisplay(int, int);


int main(void) {

  int start, nextCount;


  cout << "Enter starting integer: ";

  cin >> start;


  cout << "Enter how many next integers: ";

  cin >> nextCount;


  slowDisplay(start + 1, nextCount);


  return 0;

}


void slowDisplay(int start, int nextCount) {

  // TODO: Add the base case here

  if(start == 0) {

    

    cout << start << " ";

    slowDisplay(start + 1, nextCount - 1);

  }

}



1
Expert's answer
2022-04-04T07:48:04-0400
#include <iostream>

using namespace std;

void slowDisplay(int, int);


int main(void)
{
	int start, nextCount;
	cout << "Enter starting integer: ";
	cin >> start;
	cout << "Enter how many next integers: ";
	cin >> nextCount;
	slowDisplay(start + 1, nextCount);
	return 0;
}

void slowDisplay(int start, int nextCount)
{
	if (start > 0&&nextCount>0)
	{
		cout << start<<" ";
		slowDisplay(start + 1, nextCount - 1);
	}
	if (start == 0)
	{
		cout << start << " ";
		slowDisplay(start + 1, nextCount - 1);
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS