Write an iterative version of the function defined in Self-Test Exercise 2.
Before start the program, see the recursion function of 'printAsterisks' in Self-Test Exercise 2 . Now need to convert from recursive function to iterative function.
Iterative function of 'printAsterisks':
//Iterative version of the function 'printAsterisks':
//It takes one parameter which is a positive integer and
//that writes out that number of asterisks '*'.
//This asterisks display to the screen all
//on one line.
void printAsterisks(int numOfAsterisks)
{
int count;
for (count = 1; count <= numOfAsterisks; count++)
cout << '*';
}
Complete program by using above Iterative function:
//Header section
#include <iostream>
using namespace std;
//Function prototype
void printAsterisks(int n);
//main program
int main( )
{
//Call the method
printAsterisks(10);
return 0;
}
//Iterative version of the function 'printAsterisks':
//It takes one parameter which is a positive integer and
//that writes out that number of asterisks '*'.
//This asterisks display to the screen all
//on one line.
void printAsterisks(int numOfAsterisks)
{
int count;
for (count = 1; count <= numOfAsterisks; count++)
cout << '*';
}
Output of the program: