Printing out two patterns of asterisks!

Write a program that asks the user to enter an integer between 1 and 15. If the number entered is outside that range, your program should print out an error message and re-prompt for another number.
Once the user enters a number in the correct range, your program will print out two patterns of asterisks that look like the following. Note that the number provided by the user is equal to the height of the pattern.

Let us say that the input value was 5. Then please print the following two patterns:
*****
****
***
**
*
*****
+****
++***
+++**
++++* (Without the +'s)




Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>


using namespace std;

int main()
{
   int num;
   
   do
   {
      cout << "Enter a number between 1 and 15: ";
      cin >> num;
      
      if (num > 15 || num < 1)
      {
         cout << "Error! The number must be between 1 and 15." << endl;
      }
   }   
   while (num > 15 || num < 1);
   
   for(int pattern = 0; pattern < 2; pattern++)
   {
      for (int i = num; i > 0; i--)
      {
         for (int j = 0; j < i; j++)
         {
            cout << "*";
         }
         
         cout << endl;
       }
    }
    
    cin.get();
    
    return 0;
}               
        


How can I get it to print out:
*****
****
***
**
*
*****
+****
++***
+++**
++++* (Without the +'s)

instead of:
*****
****
***
**
*
*****
****
***
**
*
Last edited on
So you want an extra space between the two copies of the pattern?
Just add a cout << endl; between lines 32 and 33.
(I think)
The pattern should go for example
5 *'s *****
4 *'s ****
3 *'s ***
2 *'s **
1 *'s *

then print out again but this time the astericks like this
5 *'s
_4 *'s
__3 *'s
___2 *'s
____1 *'s
Last edited on
Oh, I see.
By the way, you can use [output] [/output] tags so that your spaces won't get eaten up.
So, if I understand correctly, you want it to look like
*****
****
***
**
*
*****
 ****
  ***
   **
    *
?
It's similar to what you're doing right now for the first pattern, except before you print the stars, you should first print out a number of spaces equal to 5 - (how many stars you're supposed to print on this loop iteration).

Does that make sense?
Yes that is the correct output!

I'm sorry I didn't understand what you were saying.

I should print out the number of spaces before the asterisks?
Yeah, so for the second pattern, you should print out
0 spaces + 5 asterisks
1 space + 4 asterisks
2 spaces + 3 asterisks
3 spaces + 2 asterisks
4 spaces + 1 asterisk

Does that make sense?
Yes, but how would I write the code for that? Would I use a for loop again?
Your code right now (which prints out the first pattern) is something like
for i = num to 0
    print i asterisks
.
For the second pattern, you basically just need to do
for i = num to 0
    print num-i spaces
    print i asterisks
.

Make sense?
I'm sorry I got lost. So I have this code in there twice? I don't get how to do it.
This is probably not the expected implementation, but perhaps you can take from it the basic idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <limits>
#include <iostream>
#include <iomanip>

int main()
{
    unsigned num;

    bool num_is_valid = false;
    while (!num_is_valid)
    {
        std::cout << "Enter a number between 1 and 15: ";
        if (std::cin >> num && num > 0 && num < 16)
            num_is_valid = true;
        else
        {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }

    for (unsigned i = 0; i < num; ++i)
        std::cout << std::setw(num - i) << std::setfill('*') << "" << '\n';

    for (unsigned i = 0; i < num; ++i)
    {
        std::cout << std::setw(i) << std::setfill(' ') << "";
        std::cout << std::setw(num-i) << std::setfill('*') << "" << '\n';
    }
}


http://ideone.com/jP3Bhs
Topic archived. No new replies allowed.