473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem printing pyramid of asterisks recursively in UNIX system

Kahlia
3
Hi. I am trying to print a pyramid of asterisks using recursion so that the recursive function takes in an integer and prints that number of lines of asterisks and then reverses, eg:

Expand|Select|Wrap|Line Numbers
  1.  
  2.    *   
  3.   * *  
  4.  * * *
  5. * * * *
  6.  * * *
  7.   * *
  8.    *
  9.  
if you call print(4).

I understand the general idea and have managed to put together the following code.

Expand|Select|Wrap|Line Numbers
  1. void print(int n)
  2. {
  3.         if (n == 0)
  4.         {
  5.                 return;
  6.         }
  7.         else
  8.         {
  9.                 print(n-1);
  10.                 cout << '*' * n << endl;
  11.         }
  12. }
  13.  
I realise I haven't provided the second half or the spaces as I was trying to get the asterisks printing first.

This source code compiles, however it prints:

42
84
126
168

which sounds like the ASCII codes multiplied by n. I'm guessing this is because when I am multiplying by an integer it converts the char to an integer as well.

Firstly, is there any way to stop this happening, and is there anyway to create the spaces without using a loop ?

Thanking anyone who responds.
Oct 17 '07 #1
5 9699
DumRat
93
The problem is in this line :

Expand|Select|Wrap|Line Numbers
  1. cout << '*' * n << endl;
  2.  
Here, '*' is of the type char, and n is of the type int. Now, when you say '*'*n, the result is taken as an int. That is, the ascii value of '*' is multiplied by n. That is why you get 42, 84.... (42 is the ascii of '*' I guess. )

What you have to do is print the '*' character n times. You can do it like this:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < n; i++)
  2. {
  3.      cout << '* ';
  4.  
This will print one star and space for each iteration. You'd be able to enhance this to get the pattern you want.
Oct 17 '07 #2
Kahlia
3
Here, '*' is of the type char, and n is of the type int. Now, when you say '*'*n, the result is taken as an int. That is, the ascii value of '*' is multiplied by n. That is why you get 42, 84.... (42 is the ascii of '*' I guess. )
Yeah I'd figured that.
What you have to do is print the '*' character n times. You can do it like this:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < n; i++)
  2. {
  3.      cout << '* ';
  4.  
This will print one star and space for each iteration. You'd be able to enhance this to get the pattern you want.
The problem I can see with doing it this way is that it uses iteration instead of recursion - I don't know how to implement the for loop inside a recursive function.

I have tried implementing the loop one way and got a warning:
warning: multi-character character constant
and it printed:
Expand|Select|Wrap|Line Numbers
  1.        10784
  2. 1078410784
  3. 10784
  4. 107841078410784
  5.  10784
  6. 1078410784
  7. 10784
  8. 10784107841078410784
  9.    10784
  10. 1078410784
  11. 10784
  12. 107841078410784
  13.  10784
  14.      10784
Oct 17 '07 #3
DumRat
93
A for loop can be inside a recursive function.
Anyway, do post your code.
Oct 17 '07 #4
Kahlia
3
The current code I have (not allowing for spaces) is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void print(int n);
  7.  
  8. int main()
  9. {
  10.         print(4);
  11.  
  12.         return 0;
  13. }
  14.  
  15. void print(int n)
  16. {
  17.         if (n == 0)
  18.         {
  19.                 return;
  20.         }
  21.         else
  22.         {
  23.                 print(n-1);
  24.                 for (int i = 0; i < n; i++)
  25.                         cout << "* ";
  26.                 cout << endl;
  27.                 print(n-1);
  28.         }
  29. }
  30.  
which prints a very interesting pattern of stars, but not quite what I am after. I did realise that the problem with my multiple characters was that i had still used single quotation marks instead of changing to double quotation marks when adding the spaces.

It now prints
Expand|Select|Wrap|Line Numbers
  1. *
  2. * *
  3. *
  4. * * *
  5. *
  6. * *
  7. *
  8. * * * *
  9. *
  10. * *
  11. *
  12. * * *
  13. *
  14. * *
  15. *
  16.  
Oct 17 '07 #5
DumRat
93
Work on it a little and I think you'd get the answer.
Oct 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ruby Tuesday | last post by:
Is there a fast way to read files/directory recursively? Instead of inspecting each file(s)/dir(s), is there a way to know that its a file or a directory from its hidden attribut both for windows...
2
by: David Isaac | last post by:
I'd like to try personal financial management using Python. I just found PyCheckbook, but it does not support check printing. Is there a Python check printing application kicking around? Thanks,...
8
by: virtualadepts | last post by:
I spent quite a bit of work trying to get this to format on google groups, so let me know if it needs explination. Basicly the tree sorts randomly, and can start from any number on the pyramid. ...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
4
by: foal11 | last post by:
Hi all, My problem is that I have generated a text file on unix which I want to print on a neoware(Neolinux) box. The problem is that the line length is 2 big so when I print it the text is...
12
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
4
by: process | last post by:
I am trying to solve project euler problem 18 with brute force(I will move on to a better solution after I have done that for problem 67). http://projecteuler.net/index.php?section=problems&id=18 ...
2
by: cmb3587 | last post by:
My task is to take a user input that ranges from 1-50 and use it to create a triangle of asterisks. I.E: Enter number: 5 * ** *** **** ***** ****
2
by: Gn02 | last post by:
Hi im noob in JAVA could someone explain to me how to make this program for loop.. asterisk pyramid sideward .. plss answer me i want to learn it
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.