Homework Help Please(again)

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /**
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>

using namespace 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;
}



54 entered for n in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
}


****
*
Last edited on
ok..thank you. At least I get why it's wrong now. Greatly appreciated!!
ok..so I got it to work but not sure I am happy about all the space in between. Is there a better way to write this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace 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;
       }
       cout<<endl;
   if(i<=n){
    printStars(n-1,s);
   }
   for(i; i>0; i--){
    cout<<s;
    if(i<=0){
        printStars(n+1,s);
    }

   }
cout<<endl;
}

Topic archived. No new replies allowed.