I'm trying to complete a void function problem, but I'm having difficulty.
Here's the problem: I have two variables, pattern_type and pattern_size. pattern_type can be anything from 1 - 3 and pattern_size can be anything from 1 - 10. That gives me 30 possible outcomes but there should only be 3 loops, that are nested.
I've begun with something like this to output the $ in a specific pattern outlined by my professor's guidelines:
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
|
//Begin function definition
void PrintPattern(int pattern_type, int pattern_size)
{
//Begin function body
//pattern_type 1
for (int i=0; i < pattern_size; i++)
{
for int (j=0; j < pattern_type; j++)
if (i==j)
cout << "$";
cout << endl;
}
//pattern_type 2
for (int i=0; i < pattern_size; i++)
{
for int (j=0; j < pattern_type; j++)
if (i==j)
cout << "$";
cout << endl;
}
//pattern_type 3
for (int i=0; i < pattern_size; i++)
{
for int (j=0; j < pattern_type; j++)
if (i==j)
cout << "$";
cout << endl;
}
//End function body
}
//End function definition
|
Now I know this is far from complete, it's logically incorrect. The output should be:
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
|
pattern_type=1:
I:0 | J:0
I:1 | J:1
I:2 | J:2
I:3 | J:3
OR
$
$
$
$
and so on until they reach a maximum pattern_size=10
for pattern_type=2:
$
$$
$$$
$$$$
$$$$$
$$$$$$
and finally, for pattern_type=3:
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
|
I've ALWAYS had problems with structuring the logical output in my programs, and this one is especially NOT an exception to my flaw. I understand the loop I need for the outside part of my program, which handles the basic output and input required to call the function "PrintPattern(int pattern_type, int pattern_size)" and so on, but the structure of the function is confusing me. I think the best approach would be to do something like:
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
|
void PrintPattern(int pattern_type, int pattern_size)
{
//if pattern_type is not valid, ie: 1-3
cout << "Pattern type isn't valid!\n";
return false;
//if pattern_size is not valid, ie: 1-10
cout << "Pattern size isn't valid!\n";
return false;
switch (pattern_type)
{
case 1:
for (int i=0; i < pattern_size; i++)
{
for int (j=0; j < pattern_type; j++)
if (i==j)
cout << "\t" << "$";
cout << endl;
}
case 2:
for (int i=0; i < pattern_size; i++)
{
for int (j=0; j < pattern_type; j++)
if (i==j)
cout << "$";
cout << endl;
}
case 3:
//I have no clue
default:
}
|
Can anyone give me some guidance? I'm lost.
Thank you!