Click here to Skip to main content
15,503,367 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
How i can get the following pattern using c or C++

*     *
**   **
* * * *
*  *  *
* * * *
**   **
*     *

here the user input is the no of rows and there should be diagonals from both ends. Please help me.
Updated 25-Jun-13 3:50am
[no name] 25-Jun-13 8:45am    
And what have you tried so far to accomplish your homework? What code have you written? What problems have you encountered? Did you ask your teacher? Have you reviewed your class notes?
Rinshad Hameed 25-Jun-13 8:48am    
i have tried but. I am not able to get such a pattern . please help me
[no name] 25-Jun-13 8:55am    
Help you with what exactly? I do not see any evidence at all that you have tried to do anything yourself to do your homework before asking someone to do it for you. It's your homework, don't you think that you should at least try to do it? I am not going to get any benefit from doing your homework for you.
CPallini 25-Jun-13 8:50am    
Wow, I see a pattern, there.
H.Brydon 26-Jun-13 12:37pm    
Virtual +5 for that one. Yes. :-)

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

Having said that, it's not complex - but I'll give you no code!

1) Allocate a character array big enough to hold the whole line (including a terminating null)
2) Set up a loop for the number of rows, (a for loop is fine, with a variable of curRow)
3) In the loop, clear the line to all spaces.
4) Then, set the first and last character to a '*'
5) Then, set the character at the current row number to a '*', and the character at the number of rows - (the current row + 1) to a '*'
6) Set the character at the number of rows to a null '\0'
7) Write the line
8 Repeat the loop for the next row.

Easy! Total: about 7 lines of code!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900