944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1040
  • C++ RSS
Nov 9th, 2009
0

how to display this shape using for loop?

Expand Post »
how to display this output ?

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




thank you...
Similar Threads
Reputation Points: 6
Solved Threads: 1
Posting Whiz in Training
samsons17 is offline Offline
225 posts
since Oct 2009
Nov 9th, 2009
0
Re: how to display this shape using for loop?
Please show effort in trying to make the shape, and I will help you.
Reputation Points: 102
Solved Threads: 17
Posting Whiz in Training
restrictment is offline Offline
228 posts
since Oct 2009
Nov 9th, 2009
0
Re: how to display this shape using for loop?
Here is a hint :
C++ Syntax (Toggle Plain Text)
  1. ********** 10 stars
  2. ********* 9 stars
  3. ******** 8 stars
  4. ******* 7 stars
  5. ****** 6 stars
  6. ***** 5 stars
  7. **** 4 stars
  8. *** 3 stars
  9. ** 2 stars
  10. * 1 stars
  11. ** 2 stars
  12. *** 3 stars
  13. ****
  14. *****
  15. ******
  16. *******
  17. ********
  18. *********
  19. ********** 10 stars

So you see that they decrease by 1 star until they reach 1 then they
start increment.

so assuming you can separate this problem into 2 blocks, you can
do something like this :

C++ Syntax (Toggle Plain Text)
  1. for(int i = 10; i != 1; i--)
  2. printStars(i); //a function that prints i stars with endl
  3. for(int i = 2; i <= 10; i++)
  4. printStars(i);

Now what should your printStars look like :
for example if one calls printStars(4) it should output the following :
C++ Syntax (Toggle Plain Text)
  1. ****

Go ahead and try to devise printStars then you problem becomes much
easier.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 9th, 2009
-1

re

This should do...

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. void printstar(int num_of_times);
  4. void reverseprint(int highest);
  5. int main()
  6. {
  7. for(int i=0;i<10;i++){
  8. printstar(i);
  9. }
  10. reverseprint(10);
  11. return 0;
  12. }
  13.  
  14. void printstar(int num_of_times){
  15. for(int i=0;i<num_of_times;i++){
  16. cout<<"* ";
  17. }
  18. cout<<endl;
  19. }
  20.  
  21. void reverseprint(int highest){
  22. for(int i=0;i<highest;i++){
  23. int x=highest-i;
  24. printstar(x);
  25. }
  26. cout<<endl;
  27. }
Please, if this satisfies your problem,mark this thread as solved.Thanks
Quote ...
"Show me your code and i will tell you who you are.."---Tkud
Reputation Points: 13
Solved Threads: 46
Posting Whiz in Training
tkud is offline Offline
235 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String Calculator C++
Next Thread in C++ Forum Timeline: PLEASE HELP! very difficult Homework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC