Hey all,
I'm busy with an assignment for which a question, due to being too complicated, has been cancelled (i.e. no longer required to be done).
The lecturers told us that if we still included this question in our assignment answers, they would give us the marks for it if we attempted it (even if the final program doesn't work).

The question is:
Suppose we want to display the following pattern on the screen:
&******&******&******&******&******&******&******&
*&******&******&******&******&******&******&******
**&******&******&******&******&******&******&*****
***&******&******&******&******&******&******&****
**&******&******&******&******&******&******&*****
*&******&******&******&******&******&******&******
&******&******&******&******&******&******&******&

"The size of the pattern is determined by counting the number of groups of '*' characters in the first line, or the number of rows in the pattern. Note that the pattern in the first line starts with an '&' character and ends with an '&' character, with each group of size - 1 '*' characters seperated by an '&' character as well. The size of the above pattern is 7. Note that the input value for the size of the pattern must be an odd number. A pattern of size 5 will look as follows:"

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

Question 4a: one value parameter

"We give the main function below. Write a function drawPattern that will display such a pattern on the screen. The function must have one parameter of type int, representing the size of the pattern. Use the main function and input 7 as the size. Make sure the pattern displays correctly.

Hint: Use a nested for loop. The outer loop runs from 1 to size/2+1. This will handle the pattern from the start row to the middle row (including the middle row). The inner for loop runs from size 1 to size * size + 1, completing the row. Then another nested for loop handles the rest of the pattern. i.e. the outer loop runs from size/2 to 1, decrementing the loop counter. This is done to make the pattern turn to the left. The inner loop works the same as for the first nested for loop. Test the program with different sizes."

This is the fragment of the program we have been provided with:

``````#include <iostream>
using namespace std;

//The required function [B]drawPattern[/B] should be inserted here.
(that which I have inserted is in bold)
[B]void drawPattern (int ptrnSize)
{
for (int i = 1; i <= ptrnSize/2 +1; i++)
{
for (int j = 1; j <= ptrnSize * ptrnSize +1; j++)

}[/B]
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);

return 0;
}``````

could anyone please assist me as it would only benefit me to gain these marks.

any help would be much appreciated,
Brandon.

Fbody commented: The syntax wasn't correct, but at least you tried to use code tags. Thanks. +1

Thank you for at least trying to use code tags. But they didn't work because the syntax wasn't correct. The proper syntax is [code=syntax]...your code... [/code].

There is a pattern to the output in each line, what is that pattern? (hint: the pattern is patternSize characters long) Also, do you know how to use the modulus operator (%)?

Thank you for at least trying to use code tags. But they didn't work because the syntax wasn't correct. The proper syntax is [code=syntax]...your code... [/code].

There is a pattern to the output in each line, what is that pattern? (hint: the pattern is patternSize characters long) Also, do you know how to use the modulus operator (%)?

If, say for example, I input a size of 7 then the pattern that is output will have seven occurences of the asterisk pattern in each row, & in addition to that, there will be seven rows of output

Thank you for at least trying to use code tags. But they didn't work because the syntax wasn't correct. The proper syntax is [code=syntax]...your code... [/code].

There is a pattern to the output in each line, what is that pattern? (hint: the pattern is patternSize characters long) Also, do you know how to use the modulus operator (%)?

The modulus operator is used to calculate the remainder of 1 value divided by another.. with 2 / 2 the remainder will be 0, or 3 % 2 will produce an output of 1.

Correct, if you input 7 the pattern is "&******" which is 7 characters long.
If you input 5 the pattern is "&****" which is 5 characters long.

In a source-code file separate from the assignment, can you create a for loop that creates N copies of a pattern that is N characters long on a single line of output? You will need to use the pattern length, the modulus operator, and an if-else statement.

Hint: your inner for-loop's header is correct, you just need to figure out the body.

The modulus operator is used to calculate the remainder of 1 value divided by another.. with 2 / 2 the remainder will be 0, or 3 % 2 will produce an output of 1.

We overlapped, this is also correct.....

I'm able to produce the output on a single line, however, I'm unable to get the '&' at the end of the line to display.

``````#include <iostream>
using namespace std;

int main()
{
int numberReps;

cout << "Enter the number of Repetitions: ";
cin >> numberReps;

for (int i = 1; i <= numberReps + 1; i++)
{
if (i <= numberReps)
cout << "&******";
else if (numberReps > i)
cout << "&";
}
cout << endl;

return 0;
}``````

I'm aware the the for statement could just be:

``for (int i = 1; i <= numberReps; i++)``

but I'm not to certain as how to implement the if...else statements or the modulus operator

Correct, if you input 7 the pattern is "&******" which is 7 characters long.
If you input 5 the pattern is "&****" which is 5 characters long.

In a source-code file separate from the assignment, can you create a for loop that creates N copies of a pattern that is N characters long on a single line of output? You will need to use the pattern length, the modulus operator, and an if-else statement.

Hint: your inner for-loop's header is correct, you just need to figure out the body.

Is the outer for loop's header also correct?

You need to generate your output 1-character at a time, you can not print an entire pattern on each pass of your loop. Otherwise, the length of the pattern would not adjust based on your input. You need to remember that what you are trying to create is N copies of a pattern that is N characters long. You are NOT trying to create N copies of a pattern that is 7 characters long.

Have another look at the header that you created for your inner for loop in your OP. That is the correct header. If you enter '7' at the prompt, the loop iterates `i = (7*7)+1 = 50` times (1-time for each character in the output line). If you take the modulus of i, you will get a cyclic result of [0, N).

What result value should you use to tell the program to output a '&' instead of a '*'?

Please slow down a little on your posts. If we keep overlapping, I guarantee something is going to get missed.

Is the outer for loop's header also correct?

Yes, but the outer doesn't actually contribute much to the final solution so let's not worry about that for now... Let's just get your inner working first, that is where the bulk of the work gets done. Once that's working we'll place it in your final solution, then we'll do the outer, then we may have to tweak the inner a little.

I'm sorry,
but I don't know, I've been looking at it for some time now & can't seem to come to a conclusion.
Is there anything you can give me that will help me to get what you want me to get?

I'm guessing that you must use an if statement to determine whether the remainder of 2 values (which are unknown to me) is a certain value, & if that if statement is tested & comes out true then it must output a '&', else it must output a '*'

I think you must use the value 'N' to alternate between the '&' & the '*'

You're on the right track so I'll give you a little more...
You should use the modulus of your loop counter and your pattern size. When the result is a certain value, you output '&', else you output '*'.

``````if ((loopCounter % patternSize) == someValue)
cout << "&";
else
cout << "*";``````

If you combine this with your inner for loop from your OP, what do you get?

You're on the right track so I'll give you a little more...
You should use the modulus of your loop counter and your pattern size. When the result is a certain value, you output '&', else you output '*'.

``````if ((loopCounter % patternSize) == someValue)
cout << "&";
else
cout << "*";``````

Thank-you for that,
I worked it out that if the remainder of the pattern size and the counter variable is '1' then the output is correct

Here is my code so far:

``````#include <iostream>
using namespace std;

int main()
{
int numberReps;

cout << "Enter the number of Repetitions: ";
cin >> numberReps;

for (int i = 1; i <= numberReps; i++)
{
if ((i % numberReps) == 1)
cout << '&';
else
cout << '*';
}
cout << endl;

return 0;
}``````

This is from the seperate source-file you told me to create

The body of the loop looks correct, but you need to replace your for-loop header with the header from your inner loop in this post. `for (int j = 1; j <= ptrnSize * ptrnSize +1; j++)` The code you posted still does not output enough characters, you need to make sure you output `(patternSize * patternSize) + 1` characters (which, for patternSize = 7, is 50 characters).

Your code needs to produce the first line of the output correctly before we can move on. For patternSize = 7 that line is:

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

OK, I've done that:

``````#include <iostream>
using namespace std;

int main()
{
int ptrnSize;

cout << "Enter the number of Repetitions: ";
cin >> ptrnSize;

for (int j = 1; j <= ptrnSize * ptrnSize +1; j++)
{
if ((j % ptrnSize) == 1)
cout << '&';
else
cout << '*';
}
cout << endl;

return 0;
}``````

I assume I now have to formulate a way for the loop to be executed "ptrnSize" times?
Which would be done using the outer for loop?

I assume I now have to formulate a way for the loop to be executed "ptrnSize" times?

Partially true. Let's not get too far ahead of ourselves. Don't forget:

The outer loop runs from 1 to size/2+1. This will handle the pattern from the start row to the middle row (including the middle row). The inner for loop runs from size 1 to size * size + 1, completing the row. Then another nested for loop handles the rest of the pattern.

OK, that looks good. Now, add the loop to your solution (nothing else). If you do it correctly, for a patternSize of 7 you should get this output:

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

You may have to tweak the variable names you used to match the variable names used in your solution.

I have done so, however I'm not getting that exact output.
My output is over 3 lines & doesn't go to the next line, after the last '&' in the first line the next line begins on the same line...

My code is as follows:

``````#include <iostream>
using namespace std;

void drawPattern (int ptrnSize)
{
for (int i = 1; i <= ptrnSize/2 + 1; i++)
{

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

}

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;

return 0;
}``````

The output I get is as follows:

Please enter the size of the pattern.
(You must enter an odd number) :
7
&******&******&******&******&******&******&******&&******&******&******&******&*
*****&******&******&&******&******&******&******&******&******&******&&******&**
****&******&******&******&******&******&
Press any key to continue . . .

Answer for yourself these 2 questions:
1. What statement causes output to move to the next line?

2. Where can you put the statement in question 1 so that it only executes after you output the whole line?

Hint: the statement is part of the outer loop, not the inner loop.

OK I've sorted that out:

``````for (int j = 1; j <= ptrnSize * ptrnSize + 1; j++)
{
if ((j % ptrnSize) == 1)
cout << '&';
else
cout << '*';
}
cout << endl;``````

Unfortunately I have to go now, is there any way I can organise to speak to you tomorow again? (I'm in South Africa & the local time is 17:17 here)

Keep plugging away at it. It should be to a point where you can add to and tweak it to get what you need pretty easily. Others should be able to help you if I'm not around. Except for the second pair of loops, everything you need is there.

Make sure you pay attention to the assignment write-up. There are actually several good tips in the write-up. All you need to do is implement them.

I've done some more coding which now produces the correct amount of rows of output, but I'm still stuck in terms of getting the pattern to be displayed.

The output i get for an input of 7 is:

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

The output I gte for 5 is:
&****&****&****&****&****&
&****&****&****&****&****&
&****&****&****&****&****&
&****&****&****&****&****&
&****&****&****&****&****&

The code I have so far is as follows:

``````#include <iostream>
using namespace std;

void drawPattern (int ptrnSize)
{
for (int i = 1; i <= ptrnSize/2 + 1; i++)
{

for (int j = 1; 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;

return 0;
}``````

I shouldn't reply since you keep bumping this... So what do you believe you have left to do? Looks to me like it's putting stars to head up the front of each of the rows and that you have to push some off the end those rows that receive extra stars. There's probably a bunch of ways you can pull it off.

Pseudo code.

First line first element is always &.

else if not the first line, but the first element ( we need to print out variable amount of * based on the line number)

output linenumber-1 * and then an &.

else (now worry about placing the & at different locations inside the pattern.)
print out the & based on the relative position of j to i and also it should not be the first line.

else print out *

Do the same in both inner for loops

Yes, but I'm new to this and that is a little too complicated for me..I figured out the last outer for loop by myself & am now only stuck regarding creating the final pattern...

I'm willing to put my effort in, but it would be much appreciated to receive some guidance along the way!

I apologise for bumping it up the whole time, it's just that this is due next week Tuesday so I'd rather finish it sooner than later

output linenumber-1 * and then an &.

He's given you a good clue there. See if you can get the stars in front of the & at the left border of your creation.

Start converting what I have given you into code. (You are also welcome to ignore this logic and come up with something on your own)

Post the code and we can see.

I'm dumbstruck,
I'm not trying to just get the code from you & walk away, believe me, but could you walk me through it?

I've tried to figure out what code to write but I don't know...

Can you add the additional stars to the left margin? There's 0 on the first line, 1 on the second line, ..., up to (rows/2) stars in the rows/2 line and then down from there,...,1 on the second to last line and 0 on the last line.
i and k are your row counters in the code above -- use them to know how many stars to print at front of the line. It's going to push your rows out beyond the end but you can take care of that in the next step.

Hint: Add another for loop at lines 8 and in between lines 19 and 20. Use it to print the first stars of the row. Modify the starting point of your for loops on 9 and 21 to take this shift into account.

Sorry I Hadn't refreshed my browser

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.