bug

i want to create a program that has an output like:

enter the number of asterisks: 9
*********
*******
*****
***
*
***
*****
*******
*********


this is my code:
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
#include <iostream>
using namespace std;

void print (int n_ast);

int main () {
	int ast = 0;
	cout << "enter the number of asterisks: ";
	cin >> ast;
	print (ast);
	cin.get();
	cin.get();
	return 0;
}

void print (int n_ast) {
	if (n_ast <= 1) {
		cout << "*" << endl;
		return;
	}

	else {
		for (int i = 1; i <= n_ast; ++i)
			cout << "*";
		cout << endl;
		print (n_ast -= 2);
	}
	
	for (int i = n_ast; i >= 3; i -= 2) {
		for (int j = 1; j <= i; ++j)
			cout << "*";
		cout << endl;
	}
}


but the output is:

enter the number of asterisks: 9
*********
*******
*****
***
*
***
*****
***
*******
*****
***


i tried to find the logic error, but i can't make it. is it because the recursive function?
Your code is almost correct, it just needs some slight re-arrangement.
I'd start by changing line 26 from this: print (n_ast -= 2);
to this:print (n_ast - 2);

Also, at line 22 else { isn't needed. The return at line 19 handles that.
Last edited on
print (n_ast -= 2);
to this:print (n_ast - 2);


my mistake... but what is actually the compiler doing until it mess the output?
what is actually the compiler doing

I don't understand the question.
The compiler translates your code into executable machine instructions.

Sorry if that sounds very pedantic. What I was trying to emphasise is that the program was doing exactly what you told it to do.
Consider this code, it may give you a clue.
Note, all four functions are identical apart from the character output.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>

using namespace std;

void John(int n);
void Paul(int n);
void George(int n);
void Ringo(int n);

int main () {

    int ast = 9;

    John(ast);

    return 0;
}

void John(int n) {
    if (n <= 1) {
        cout << "*" << endl;
        return;
    }

    for (int i = 1; i <= n; ++i)
        cout << "*";
    cout << endl;
    Paul(n -= 2);

    for (int i = n; i >= 3; i -= 2) {
        for (int j = 1; j <= i; ++j)
            cout << "*";
        cout << endl;
    }
}
void Paul(int n) {
    if (n <= 1) {
        cout << "x" << endl;
        return;
    }

    for (int i = 1; i <= n; ++i)
        cout << "x";
    cout << endl;
    George(n -= 2);

    for (int i = n; i >= 3; i -= 2) {
        for (int j = 1; j <= i; ++j)
            cout << "x";
        cout << endl;
    }
}
void George(int n) {
    if (n <= 1) {
        cout << "#" << endl;
        return;
    }

    for (int i = 1; i <= n; ++i)
        cout << "#";
    cout << endl;
    Ringo(n -= 2);

    for (int i = n; i >= 3; i -= 2) {
        for (int j = 1; j <= i; ++j)
            cout << "#";
        cout << endl;
    }
}
void Ringo(int n) {
    if (n <= 1) {
        cout << "@" << endl;
        return;
    }

    for (int i = 1; i <= n; ++i)
        cout << "@";
    cout << endl;
    John(n -= 2);

    for (int i = n; i >= 3; i -= 2) {
        for (int j = 1; j <= i; ++j)
            cout << "@";
        cout << endl;
    }
}

Output:
*********
xxxxxxx
#####
@@@
*
###
xxxxx
xxx
*******
*****
***
Last edited on
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
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int n,i,j;
    cout<<"Enter the number of * :";
    cin>>n;
    for(i=n;i>=1;i=i-2)
    {
        for(j=1;j<=i;j++)
        {
            cout<<'*';
        }
        cout<<endl;
    }
    for(i=3;i<=n;i=i+2)
    {
        for(j=1;j<=i;j++)
        {
            cout<<'*';
        }
        cout<<endl;
    }
    cout<<endl;
    system("PAUSE");
}


This is my code.exactly what u wanted.

Enjoy Programming : )
Topic archived. No new replies allowed.