943,108 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5012
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 8th, 2010
0

Make a inverted pyramid pattern using numbers from input..

Expand Post »
MY problem is .....
Given an input 'n' came from the user and display a pattern.
but my code is very long .... my code is only for 1-10 numbers i need to display any numbers that input of the users can u give me some short code or advice to make it easy or briefly...Thxxxxx
Ex. if n=6, display
C++ Syntax (Toggle Plain Text)
  1. Ex. output
  2.  
  3. 6 5 4 3 2 1
  4. 6 5 4 3 2
  5. 6 5 4 3
  6. 6 5 4
  7. 6 5
  8. 6
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include<iostream.h>
  3. #include<conio.h>
  4. main()
  5. {
  6. int n1,i;
  7. char c1;
  8. textcolor(YELLOW);
  9. a:
  10. clrscr();
  11. cout<<"Enter a number :";
  12. cin>>n1;
  13. for (i=n1;i>0;i--)
  14. {
  15. cout<<" "<<i;
  16. }
  17. cout<<endl<<" ";
  18. for (i=n1;i>1;i--)
  19. {
  20. cout<<" "<<i;
  21. }
  22. cout<<endl<<" ";
  23. for (i=n1;i>2;i--)
  24. {
  25. cout<<" "<<i;
  26. }
  27. cout<<endl<<" ";
  28. for (i=n1;i>3;i--)
  29. {
  30. cout<<" "<<i;
  31. }
  32. cout<<endl<<" ";
  33. for (i=n1;i>4;i--)
  34. {
  35. cout<<" "<<i;
  36. }
  37. cout<<endl<<" ";
  38. for (i=n1;i>5;i--)
  39. {
  40. cout<<" "<<i;
  41. }
  42. cout<<endl<<" ";
  43. for (i=n1;i>6;i--)
  44. {
  45. cout<<" "<<i;
  46. }
  47. cout<<endl<<" ";
  48. for (i=n1;i>7;i--)
  49. {
  50. cout<<" "<<i;
  51. }
  52. cout<<endl<<" ";
  53. for (i=n1;i>8;i--)
  54. {
  55. cout<<" "<<i;
  56. }
  57. cout<<endl<<" ";
  58. for (i=n1;i>9;i--)
  59. {
  60. cout<<" "<<i;
  61. }
  62. {
  63. cout<<""<<endl<<""<<endl;
  64. cout<<"Do you want to try another number or EXIT...?"<<endl;
  65. b:
  66. cout<<"Press 'y' if you want to try another number and 'n' to exit:";
  67. cin>>c1;
  68. if ( c1=='y' || c1== 'Y')
  69. goto a;
  70. else if (c1=='n' || c1=='N')
  71. cout<<endl<<"Press Any Key to EXIT";
  72. else
  73. {
  74. cout<<"Invalid input, Choose again"<<endl;
  75. goto b;
  76. }
  77. }
  78. getch();
  79. }
Last edited by khevz09; Feb 8th, 2010 at 7:15 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khevz09 is offline Offline
7 posts
since Jan 2010
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
Yeah your code is very long, thanks for the problem Why use so many for loops cant you do it with only
THis is what I did


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10. int num = 10;
  11. while (num !=0)
  12. {
  13. for(int i = 0; i < num; ++i)
  14. {
  15. cout << " " << i;
  16. }
  17. cout << endl;
  18. if(num == 10)
  19. cout << " ";
  20. if(num == 9)
  21. cout << " ";
  22. if(num == 8)
  23. cout << " ";
  24. if(num == 7)
  25. cout << " ";
  26. if(num == 6)
  27. cout << " ";
  28. if(num == 5)
  29. cout << " ";
  30. if(num == 4)
  31. cout << " ";
  32. if(num == 3)
  33. cout << " ";
  34. if(num == 2)
  35. cout << " ";
  36.  
  37.  
  38. --num;
  39. }
  40. }
Last edited by invisi; Feb 8th, 2010 at 7:50 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
help me plssssss...... i need it ASAP...
THxxxxx......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khevz09 is offline Offline
7 posts
since Jan 2010
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
i don't know how.... Because I'am a begginer in c++... Can u help me to my prob?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khevz09 is offline Offline
7 posts
since Jan 2010
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
Well, for starters have a good think about your problem.

Take another look at your desired output and really think about it for a minute.
After the user has entered a number (num) between 1-10, you want the first line to count down from num to 1, so your initial minimum value to count down to is 1, but for each line afterwards the minimum is increased by 1.

To generate each line of output, you're going to need some kind of loop to count down from n to minimum and output each value.

And you want to keep outputting lines until the minimum value is greater than the entered number (num). (the very last line output is equal to the entered number, so as soon as the minimum becomes greater than num, you want to stop!)

So this implies that you'll need a second loop to keep track of the minimum value.

Bearing all of that in mind, you should now be able to code a solution for this, it's really not a difficult problem to solve. It may help you to take a piece of paper and actually draw out a flowchart or write some pseudo-code before coding your solution.

When trying to tackle any programming problem, don't just try to dive in and start coding without properly thinking things through and getting a solid program design first. Programmers of all levels have to practice this, even the most experienced. But as you are still starting out it is especially important for you!

Once you've got a bit more experience with designing and coding programs, you'll find yourself able to code solutions to simple problems like this in your sleep, but until then I'd advise using a bit more caution and forethought.

Cheers for now,
Jas.

[edit] This applies to invisi as well! I see where you were going with that incomplete attempt, but it is also an inelegant solution to the problem!
Last edited by JasonHippy; Feb 8th, 2010 at 8:22 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
ok what you need to do is come up with a basic algorithm. your basic algorithm in this case would be the first line contains the n number of characters. taking from your eg lets say its a 6. note that ur max characters is also the max num of lines you will have. so basically with a 6 u have 6 lines of display wea first line has no space sec has one and so forth. note this would give u a irregular pyramid. now lets say you have a num x = 0 so in in the first line u print n and do n-1 until u reach x.wen u reach x print a \n. for the sec line start printing at n again n go until x+1.wen u reach x+1 print a \n. go ahead using this algorithm and you will come with your solution.now to make your program small and since you will use the same algorithm again for n number of lines you will need to get help from a loop.try and go with small eg nums you will be able to get it. if any other queries let me know. note that i have assumed that the pyramid will be irregular coz u cannot print a half space in ur screen
Last edited by kesh1000; Feb 8th, 2010 at 8:34 am.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
kesh1000 is offline Offline
69 posts
since May 2009
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
Oh yeah, good point kesh...Looking at my previous post, I forgot to include a bit about the leading spaces in the output. (My bad, I spotted it when writing my original post, but completely forgot to include it in my description! Very careless of me!)

@OP:
If you look at your desired output, the first line has no leading space, the 2nd has one leading space, the 3rd has two etc.etc. .
So you can see that for each additional line you output, you will need an additional space.
So you start with 0 leading spaces.
Before you use the loop to count down and output the values from n to minimum, you'll need to use another loop to output the required number of spaces.
After outputting each line you'll need to increment the number of spaces.

In light of my glaring omission, I'll give you some pseudo code for the solution! What the hell I'm feeling generous...

In pseudo code, your algorithm should be something like this:
Quote ...
1. Input a number (num) between 1 and 10
(NOTE: Don't forget to do any error checking, prompt user until they enter something valid)
2. Set minimum value to 1
3. Set number of Leading spaces to 0
4. While minimum is <= to num:
Do the following:
(i). if leading spaces > 0: use a loop to output the required number of spaces.
(ii). Using another loop, count down from num to minimum, outputting each value.
(iii). output a newline character
(iv). increase the minimum value
(v). increase the number of leading spaces.
And that is it!

Cheers for now,
Jas.
Last edited by JasonHippy; Feb 8th, 2010 at 9:44 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
is there any code for increamenting the space?
like for example in
cout<<" "; // I need to increament this space to make my work right...

thats the only thing that i need to work for it....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khevz09 is offline Offline
7 posts
since Jan 2010
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
Use a loop printing one space to the screen each time through the loop. When you are done with the loop it will look like you printed a string of spaces all at once.

As an alternative to doing everyting on the fly you could use two arrays (tables) of strings, one containing the string of spaces you need and the other containing the string of numbers you need, printing each string as you need them for each line of the pyramid. In addition to being comfortable using loops, table look up protocols are another nice tool to have in your tool box, so working out the solution using both approaches isn't necessarily the worst idea.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 8th, 2010
0
Re: Make a inverted pyramid pattern using numbers from input..
Add a variable called something like leading_spaces and set it to 0 as per my algo, then inside your main loop, if the number of spaces is greater than 0, set up a loop to output the appropriate number of spaces.
After the loop to output the spaces you'll have your loop to output the rest of the line (i.e. the count-down).
Towards the end of the main loop, increment the number of spaces ready for the next iteration of the main loop.

So if you've followed my algo correctly, you should have something that looks a bit like this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int num=0;
  6. // your code to get user input here....
  7. ...
  8.  
  9. // set initial values for minimum and the number of leading spaces
  10. int minimum = 1;
  11. int leading_spaces=0;
  12.  
  13. // loop until minimum is greater than the entered number.
  14. while(minimum<=num)
  15. {
  16. // display any leading spaces
  17. if(leading_spaces>0)
  18. {
  19. for(int s=0; s<leading_spaces; ++s)
  20. std::cout << " ";
  21. }
  22.  
  23. // your loop to count down and output values from num to minimum to go here....
  24. ...
  25.  
  26. // terminate the current line
  27. std::cout << std::endl;
  28.  
  29. // increment minimum and leading_spaces
  30. minimum++;
  31. leading_spaces++;
  32. }
  33. std::cout << std::endl;
  34. return 0;
  35. }

Note: I've deliberately left out the code for getting user input and the code for outputting the numbers. You've already got that by the sounds of it..

Cheers for now,
Jas.
Last edited by JasonHippy; Feb 8th, 2010 at 11:02 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

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: need help figuring out behaviour
Next Thread in C++ Forum Timeline: C++ Craps problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC