No, I don't know I just feel stupid now...

I don't know how to get a for loop to do that

Did you try putting a for loop in that spot and seeing if what if anything you could accomplish with it? It's a for loop to print the stars on the left side of the picture.
It needs to print (for a seven line shape):

(zero stars) line 1 -- based on you going 1-7 rather than 0-6
*
**
***
**
*
(zero stars again) line 7

After you've printed those on the appropriate lines, print out the rest of the line. There's a change you have to make so that you don't get extra characters that go beyond the end of the line. Just see if you can do this first part before that. You should be experimenting with it until you get it right or sitting down with a piece of paper and plotting it out. No one is going to do this for you.

This is what I've got it to do so far:

How to make it determine how mnay stars to produce on which rows is where I'm now stuck

Please enter the size of the pattern.
(You must enter an odd number) :
7
*******&******&******&******&******&******&******&******&
*******&******&******&******&******&******&******&******&
*******&******&******&******&******&******&******&******&
*******&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&

Press any key to continue . . .

I achieved this by adding the following for loop on line 8:

for (int j= 1; j <= ptrnSize; j++)
        {
                cout << '*';      
        }

Okay, so you know that's not correct but how can you change it? In this case ptrnSize is not the right number of stars. Go back to post 28 and read it again. How many do you need on each row? It varies, but what does it vary based on?

Instead of pattern size use i-1 for the exit criteria

Okay, Ive got it to display sort of the correct thing, must I use an if...else statement in that for loop now or not?

Ive replaced ptrnSize with i and the output I'm now getting is:


Please enter the size of the pattern.
(You must enter an odd number) :
7
*&******&******&******&******&******&******&******&
**&******&******&******&******&******&******&******&
***&******&******&******&******&******&******&******&
****&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&

Press any key to continue . . .

Additional for loops may work, but as you can see all it is doing is adding characters to the beginning of the string. The code that you already have to produce this:

&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&

is sufficient. You just need to modify 2 characters. One (1) each in the conditional statements contained in the inner for loops.

Think back to when you found out that (j%ptrnSize) == 1 produces the pattern you were looking for. Do you remember what happened when you put a different value in for the '1'?

If you enter a '2', you get this:

*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******
*&******&******&******&******&******&******&******

How can you make the '1' a different value in each iteration of the loop?

Instead of pattern size use i-1 for the exit criteria

Thyank-yu for that

Additional for loops may work, but as you can see all it is doing is adding characters to the beginning of the string. The code that you already have to produce this:
is sufficient. You just need to modify 2 characters. One (1) each in the conditional statements contained in the inner for loops.

Think back to when you found out that (j%ptrnSize) == 1 produces the pattern you were looking for. Do you remember what happened when you put a different value in for the '1'?

I've played around with what you have said, and I have come up with the following:

#include <iostream>
using namespace std;

void drawPattern (int ptrnSize)
{
    for (int i = 1; i <= ptrnSize/2 + 1; i++)
    {
    
        for (int j = (ptrnSize - (i - 2)); j <= ptrnSize * ptrnSize + 1; j++)
    
        {
            if ((j % ptrnSize) == 1)
               cout << '&';  
            else
               cout << '*';
        }
        cout << endl;
    }

    for (int k = ptrnSize / 2; k >= 1 ; k--)
    { 
    
        for (int l = 1; l <= ptrnSize * ptrnSize + 1; l++)
        {
            if ((l % ptrnSize) == 1)
               cout << '&';
            else 
               cout << '*';
        }
        cout << endl;
    }         

}

int main( )
{
    int size = 0;
    
    do
    {
        cout << endl << "Please enter the size of the pattern. " << endl;
        cout << "(You must enter an odd number) : " << endl;
        cin >> size;
    }while (size % 2 != 1);
    drawPattern (size);
    cout << endl;

see the changes in line 9...

the output that now produces is:


Please enter the size of the pattern.
(You must enter an odd number) :
7
&******&******&******&******&******&******&
*&******&******&******&******&******&******&
**&******&******&******&******&******&******&
***&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&
&******&******&******&******&******&******&******&

Press any key to continue . . .

I have done it!

I edited line 23 as such:

for (int l = ptrnSize - (k - 2); l <= ptrnSize * ptrnSize + 1; l++)

The output I get for an input of 7 is now:

Please enter the size of the pattern.
(You must enter an odd number) :
7
&******&******&******&******&******&******&
*&******&******&******&******&******&******&
**&******&******&******&******&******&******&
***&******&******&******&******&******&******&
**&******&******&******&******&******&******&
*&******&******&******&******&******&******&
&******&******&******&******&******&******&

Press any key to continue . . .

For an input of 5 is:

Please enter the size of the pattern.
(You must enter an odd number) :
5
&****&****&****&****&
*&****&****&****&****&
**&****&****&****&****&
*&****&****&****&****&
&****&****&****&****&

Press any key to continue . . .

Now allthat needs to be done is to drop the addtional characters at the end of the respective lines??

Very close... I suggest you count the patterns though...

For an input of 7 your output only has 6 patterns when you need 7. Also, for an input of 5, your output only has 4 patterns instead of 5.

Oh my, how do I work around that now?

Put your for loops back the way they were when you were getting the non-shifting 7x7 output. Once those are reset, re-read one of my previous posts (clicky).

Then look at your if-then statements instead. You only need to change 1 character in each your if statements to make it work properly.

If you change the correct characters properly, you will get:

Please enter the size of the pattern.
(You must enter an odd number) :
7
&******&******&******&******&******&******&******&
*&******&******&******&******&******&******&******
**&******&******&******&******&******&******&*****
***&******&******&******&******&******&******&****
**&******&******&******&******&******&******&*****
*&******&******&******&******&******&******&******
&******&******&******&******&******&******&******&

All you have to do to shift the pattern is change the modulus value that you use to determine when to output an '&' instead of an '*'.

Ok Ive now sorted that out, here are the modified for loops:

for (int j = 1 - (i - 1) ; j <= ptrnSize * ptrnSize + 1; j++)
    
        for (int l = 1 - (k - 1); l <= ptrnSize * ptrnSize + 1; l++)

Now how do I get the end characters to drop?

You really need to stop double-posting and give people time to respond. You've overlapped me every time I've posted...

Look at post 43...

Put your for loops back the way they were when you were getting the non-shifting 7x7 output. Once those are reset, re-read one of my previous posts (clicky).

Then look at your if-then statements instead. You only need to change 1 character in each your if statements to make it work properly.

If you change the correct characters properly, you will get:


All you have to do to shift the pattern is change the modulus value that you use to determine when to output an '&' instead of an '*'.

I substituted the 1's with the counter variables of both of the outer for loops & that has produced the desired output.

And I'm sorry about that...

And I'm sorry about that...

No worries :) Just try to keep that in mind in the future.

Sounds like you got it. Well done.

Thank-you!:D Now the last 2 parts of this question require that the user enters which character will replace the * and then the user must also enter which character will be used in place of the &...

Would it be right in saying that I include the string header file

#include <string>

and then prompt the user for input of the 2 characters they require, and modify the drawPattern function as such:

void drawPattern (int ptrnSize, char groupC, char GroupB)
{
    for (int i = 1; i <= ptrnSize/2 + 1; i++)
    {
    
        for (int j = 1 ; j <= ptrnSize * ptrnSize + 1; j++)
    
        {
            if ((j % ptrnSize) == i)
               cout << groupB;  
            else
               cout << groupC;
        }
        cout << endl;
    }

    for (int k = ptrnSize / 2; k >= 1 ; k--)
    { 
    
        for (int l = 1; l <= ptrnSize * ptrnSize + 1; l++)
        {
            if ((l % ptrnSize) == k)
               cout << groupB;
            else 
               cout << groupC;
        }
        cout << endl;
    }         

}

Don't bother with the string header, it's not needed if you are going to work with chars. Just declare a couple char variables then prompt for and input values for them.

It looks like you have the right idea for the modification of drawPattern(), just pay attention to which input you use for which argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.