program using recursive

I'm still new in using recursive. I tried to make a program using recursive but i don't get it right.

Here is my coding :

// lab 2, exercise 4, num 1
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

int main()
{
int n;

cout << "Enter the input : ";
cin >> n;

char *counter="#";
int i=1;
for (i = 1; i <= n; i++) {
cout << "\n#" << counter;
counter++;

}
getch();
return 0;
}

My output :

Enter the input : 6
##
#
#
#
##
#
#

Actually that's not the output that i wanted. I want to generated output like below when i input = 6.

Exact output :

Enter the input : 6
# # # # # #
# # # # #
# # # #
# # #
# #
#
# #
# # #
# # # #
# # # # #
# # # # # #
I tag the code for easy reference to line numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

int main()
{
int n;

cout << "Enter the input : ";
cin >> n;

char *counter="#";
int i=1;
for (i = 1; i <= n; i++) {
cout << "\n#" << counter;
counter++;

}
getch();
return 0;
}


It's a for loop. Recursion refers to the action a function calls itself (or, indirectly, calls another function which in turns calls the first function).

Let's see why you're getting weird results.
The main problem is the counter++; on line 16.
You declare on line 12 that counter to be a char* with "value" "#". A char* ends with the null character '\0', so counter is basically { '#', '\0' } (Note that they are chars not strings).

Now what does counter++ do?
counter originally points to the '#', and it's incremented, so it now points to the '\0'.

The null character is not "visible" in the console output, so it looks like some # are missing.



A quick example would be
1
2
3
4
5
6
7
8
for (int i = 1; i < 7; i++)
{
   for (int j = 0; j < i; j++)
   {
      cout << '#';
   }
   cout << '\n';
}
Last edited on
Also, it makes it easier for people to read your code and help if you put code tags around it.

And char *counter; does not need to be a pointer, there is only one char, so do it like this: char counter = '#';. Note the single quotes. To keep it short, we can say that you use those when you have only one character.

But to make it long, single quotes represent the numerical form of the character encapsulated in the quotes. chars are really just small numbers and all characters have a numerical equivalent. When you say char *string = "lalala";, you don't have to worry about that, but this is equivalent to char string[] = {'l', 'a', 'l', 'a', 'l', 'a', '\0'};. Basically an array of numbers that can be represented as characters. If you get that, great. If I'm confusing you, just ignore it, it's not important at this point.

Anyways, the person above me gave some really good examples and explanations. I was going to give some, but I realized they were going to be really c-ish, so I didn't (chars and pointers remind me of the K&R book).
Thanks.. I understand bout the char and string.
Here is my new coding.. i try to combine it from wmheric example and mine.

// lab 2, exercise 4, num 1
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

void printTriangle (int , char );

int main()
{
int size;

cout << "Enter the input : ";
cin >> size;
printTriangle(size,'#');

for (int i = 1; i < 7; i++)
{
for (int j = 0; j < i; j++)
{
cout << '#';
}
cout << '\n';
}

getch();
return 0;
}

void printTriangle (int size, char ch)
{
for(int row = size; row > 0; row--)
{
for(int column = 0; column < row; column++)
cout<<ch;
cout<<endl;
}
}

Output =

Enter the input : 6
# # # # # #
# # # # #
# # # #
# # #
# #
#
# #
# # #
# # # #
# # # # #
# # # # # #

I already get it but if i want to make it center,what should i do? I already tried using setw() but what i got was something else. Not like what I want.

Enter the input : 6
# # # # # #
# # # # #
# # # #
# # #
# #
#
# #
# # #
# # # #
# # # # #
# # # # # #


Your code is ok. This is a "cleaner" way:
1
2
3
4
5
6
7
8
for (int i = 0; i < 11; i++)   // think how you get 11 from 6 ;)
{
   for (int j = 0; j < abs(i-5)+1; j++)   // the '5' depends on the input '6' of course
   {
      cout << "# ";   // it looks like you need a space between #s in the patterns
   }
   cout << "\n";
}

where abs() is from <cstdlib>.
Also note that cout << can take string or char, it's up to you here.


To get the second pattern, considering adding a certain amount of spaces at the front and at the end of each line.
[edited] well, spaces at the ends are unnecessary
Last edited on
Thanks but it seems i still don't get it. I tried make a coding using that "abs" but it seems there a some errors says that it unable to open file <cstdlib>.
some errors says that it unable to open file <cstdlib>

Check the #include directory.
Btw, I see you're #including <iostream.h>, the new standard should be <iostream> instead.

List the characteristics of the pattern.
For the first one:

Line| 0 1 2 3 4 5 6 7 8 9 10
#'s | 6 5 4 3 2 1 2 3 4 5 6

Note that it's symmetric about line 5, that's how I get the "formula".
Last edited on
Thank you. You really do help me in many ways.
Now I'm learning 2 languages, java and C++. So, sometimes I got mixed up.
Topic archived. No new replies allowed.