I do not understand why the second line prints only one star. I am missing something that is probably right in front of my face. Can anyone offer guidance? Here is my code so far:
/**
Tricia Denning
CMPSC 205-2967, Spring 2014
Assignment 10
Write a recursive function that takes as a parameter a nonnegative
integer and generates the following pattern of stars. If the
nonnegative integer is 4 then the pattern generated is:
****
***
**
*
*
**
***
****
also, write a program that prompts the user to enter the number
of lines in the pattern and uses the recursive function to generate the pattern.
**/
#include <iostream>
usingnamespace std;
void printStars( int n, char s);
int main()
{int n;
char s;
//int s;
cout<<"Please Enter The Number of Lines:"<<endl;
cin>>n;
printStars(n,'*');
return 0;
}//end main
void printStars(int n, char s){
int i;
for (i=0; i<n; i++){
cout<<s;
}
n=n-1;
cout<<endl;
if(i < n){
printStars(n,s);}
cout<<s;
}
void printStars(int n, char s)
{
int i;
//i = 0, n=4, for loop will run 4 times
for (i=0; i<n; i++)
{
cout<<s;
}
//at this point, i=4, n=4
n=n-1; // now n=3
cout<<endl;
if(i < n) // if 4<3 - false, this will not run
{
printStars(n,s);
}
cout<<s; //output s once
}