Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
I know this is my homework and it is assigned for a reason, but I've been sitting on the problem for a week and I still haven't figured it out.

So the goal here is to create four patterns (amount of rows is based on user input) using one line of C++ code. The four patterns are:
*     ***       *     ***
**     **      **     **
***     *     ***     *


I've figured out how to code each pattern, but doing so means that I have two/three while loops. We haven't learned the for loop in this class and are therefore not allowed to use it. But I have no idea how I can even compress those loops into one single line! If I cheat and use for loops it almost works for the first and fourth patterns. But otherwise...

Here's the code I have for the patterns (n is user input of amount of rows):

Pattern 1:
C++
int i, j, n;
i = 1;
while (i <= n) {
    j = 0;
    while (j < i) {
        cout << "*";
        j++;
    }
    cout << endl;
    i++;
}


Pattern 2:
C++
int i, j, k, n;
i = 0;
while (i < n) {
    k = 0;
    while (k < i) {
        cout << " ";
        k++;
    }
    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }
    cout << endl;
    i++;
}


Pattern 3:
C++
int i, j, k, n;
i = 1;
while (i <= n) {
    k = n;
    while (k > i) {
        cout << " ";
        k--;
    }
    j = 0;
    while (j < i) {
       cout << "*";
       j++;
    }
    cout << endl;
    i++;
}


Pattern 4:
C++
int i, j, n;
i = 0;
while (i < n) {
    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }
    cout << endl;
    i++;
}


What we learned in class is very basic because this course is an introduction to computing science. It is supposed to touch on a lot of the different topics within in without going into too much depth. All we really learned for C++ is basic declaration, if statements, and the while loop.

I really need help. :( Some tips or hints will suit me just fine; I'm asking for help, not for someone to do my work for me.

Thank you in advance!
Posted
Updated 9-Dec-13 7:51am
v3
Comments
Richard MacCutchan 7-Dec-13 13:07pm    
I don't think you could do it in one line. I suggest you ask your teacher from some guidance on what he/she really expects.
BelzetStarling 7-Dec-13 17:45pm    
That's what I feared. Thanks for the answer anyway. :)

Okay, I misunderstood my teacher. :( Terribly sorry, everyone! He wanted all of the patterns printed in one line, not necessarily one line of code. For those who are interested, here's the code I ended with (and that he accepted as a solution):
C++
int i, j, k, n;

cout << "Enter number of rows: ";
cin >> n;

i = 0;
while (i < n) {
    
    j = 0;
    while (j <= i) {
        cout << "*";
        j++;
    }

    k = n - 1;
    while (k > i) {
        cout << " ";
               k--;
    }

    cout << "     ";

    k = 0;
    while (k < i) {
        cout << " ";
        k++;
    }

    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }

    cout << "     ";

    j = n - 1;
    while (j > i) {
        cout << " ";
        j--;
    }

    k = i;
    while (k >= 0) {
        cout << "*";
        k--;
    }

    cout << "     ";

    j = n;
    while (j > i) {
        cout << "*";
        j--;
    }

    cout << endl;
    i++;
}


Once again, sorry for the headaches I may have caused. ^^
 
Share this answer
 
Comments
enhzflep 10-Dec-13 0:51am    
:D
Stefan_Lang 10-Dec-13 9:11am    
Never mind - there are lots of people on this site who love tricky questions! ;-p
This should get you started, but you will need to figure out the correct spacing values for yourself
C++
int i = 0;
while (i != 3)
    cout << string(i, '*') << string(4-i, ' ') << setw(4) << string(4 - i, '*') << setw(6) << string(i, '*')  << string(4, ' ') << string(4 - i, '*') << string(i++, ' ') << endl;
 
Share this answer
 
v3
Comments
Stefan_Lang 9-Dec-13 8:04am    
Good thinking about using setw, but if the OP is accurate about having learned only "basic declaration, if statements, and the while loop.", I doubt that is the answer the teacher was looking for.

That said, I have no other idea how to solve it either, short of removing all line breaks, and maybe replacing ';' with ',' ;-p
Richard MacCutchan 9-Dec-13 8:19am    
My original suggestion just had stars and spaces. The above was more an exercise in learning for me. :)
What a brain-bender. I can see a pattern here, I just can't quite paramaterize it any better just yet.

Looking at your example, I noted down the number of asterisks and spaces on each line. This gave me the following table:

*     ***       *     ***
**     **      **     **
***     *     ***     *

1, 5, 3, 7, 1, 5, 3
2, 5, 2, 6, 2, 5, 2
3, 5, 1, 5, 3, 5, 1


You can clearly see a relationship in the odd-numbered columns to the max number of rows, N.
You can also see that the even numbered columns have their own patterns - n+1+2, 2*(n+1)-row, n+1+2 N+2, 2*(N+1)-row, N+2

I smashed together a version using for loops, then re-read your question. So I converted them to while loops. Clearly, I'm using more than 1 line of code. But, in doing so - I noticed that there was a re-occurant pattern along each row. 1,5,3 in the first row is repeated, as is 2,5,2 in the second. What a total brain buster!!

Here's some ode to consider. Perhaps you can identify a pattern I've just not quite come to grips with yet. A patter that would eliminate much of the repetition I've exhibited and in doing so, produce code that is (almost/actually) reasonable as a single line of code.

I tested with N ranging up to about 25 and was rewarded with the same pattern, albeit scaled ever larger. Note that for the ease/brevity of the terminating condition of each while loop, N holds a number 1 larger than the number of rows you'd actually like to print.

C++
int main()
{
    int row, sp, st;
    int n = 4;

    row=1; while (row < n)
    {
        st=0; while(st++ < row) printf("*");
        sp=0; while(sp++ < n+1) printf(" ");
        st=0; while(st++ < n-row) printf("*");
        sp=0; while(sp++ < 2*n-row) printf(" ");

        //---------------
        st=0; while(st++ < row) printf("*");
        sp=0; while(sp++ < n+1) printf(" ");
        st=0; while(st++ < n-row) printf("*");

        printf("\n");
        row++;
    }
    return 0;
}


Here's a sample of (my)n = 10
*           *********                   *           *********
**           ********                  **           ********
***           *******                 ***           *******
****           ******                ****           ******
*****           *****               *****           *****
******           ****              ******           ****
*******           ***             *******           ***
********           **            ********           **
*********           *           *********           *


My head hurts!
 
Share this answer
 
v3
KISS.

C++
std::cout << "*     ***       *     ***" << std::endl << "**     **      **     **" << std::endl << "***     *     ***     *" << std::endl;
 
Share this answer
 
v2
Comments
BelzetStarling 9-Dec-13 13:15pm    
That was something I was thinking of, but unfortunately the amount of rows depends on n, which is a user input. I'm not sure how to implement that into your solution.
Member 8616148 9-Dec-13 13:31pm    
Ahh, then you've learned something. No programmer can "implement" a solution without knowing *all* the requirements. You had not mentioned "user input" in your question.
BelzetStarling 9-Dec-13 13:39pm    
I actually did mention user input: right above all the codes for the patterns - "Here's the code I have for the patterns (n is user input of amount of rows):"
Member 8616148 9-Dec-13 13:47pm    
That was a statement about "the code you have", not the requirement. Your statement of the requirement was quite specific: "So the goal here is to create four patterns using one line of C++ code. The four patterns are:"
BelzetStarling 9-Dec-13 13:49pm    
I'm sorry that that wasn't clear. I'll edit the post to mention that. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900