Click here to Skip to main content
14,665,301 members
Rate this:
Please Sign up or sign in to vote.
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:
int i, j, n;
i = 1;
while (i <= n) {
    j = 0;
    while (j < i) {
        cout << "*";
        j++;
    }
    cout << endl;
    i++;
}


Pattern 2:
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:
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:
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. :)

Rate this:
Please Sign up or sign in to vote.

Solution 4

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):
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. ^^
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

This should get you started, but you will need to figure out the correct spacing values for yourself
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;
   
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. :)
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.

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!
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

KISS.

std::cout << "*     ***       *     ***" << std::endl << "**     **      **     **" << std::endl << "***     *     ***     *" << std::endl;
   
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. :)
Member 8616148 9-Dec-13 14:04pm
   
Well, there are some other inconsistencies in your problem statement you should think about as well. A not-so-subtle point I'm trying to get across is that the very first step in solving any programming challenge is to find a clear and specific statements of the requirements and constraints. For example, you say there are "4 patterns", and yet I obviously saw 3 (one line for each, where I think you meant 3 lines for pattern #1, and 3 different lines for pattern #2, etc.) You also state "We haven't learned the for loop in this class and are therefore not allowed to use it." -- but I have no idea what else you "haven't learned", thus I don't know what other constraints there are on solving this problem. Finally, you mention "in one line of code", which is impossible, unless you really stretch the definition of "one line", how are you going to get and store the input variable? How are you going to meet the requirement for "main"?
Member 8616148 9-Dec-13 14:17pm
   
Using 2 lines, and disregarding "main", and making some other assumptions about your true requirement, try this (assuming ternary isn't also forbidden):

std::cin >> n;
std::cout << ((1==n)? "*\n**\n***\n" : ((2==n)? "***\n **\n *\n" : ((3==n)? " *\n **\n***\n" : ((4==n)? "***\n**\n*\n" : ""))));
enhzflep 9-Dec-13 13:27pm
   
I think you may want a few more spaces between your stars than that! - (The example has 5, 6 or 7 spaces between each block of stars)
Member 8616148 9-Dec-13 13:34pm
   
I think you need to learn the difference between proportional spaced fonts and others. Try copying what I posted into notepad, then set your font to Courier New. Eliminate the code, and paste each group of stars (asterisks, actually) on a new line. I believe you'll find they match the original requirement quite well.
enhzflep 9-Dec-13 13:43pm
   
I did better than that. I copied/pasted your code and ran it. It didn't produce the example shown(which was also copy/pasted). You snooze, you lose!

I snoozed!
Member 8616148 9-Dec-13 13:48pm
   
In a console app?
enhzflep 9-Dec-13 13:49pm
   
Yup.
Stefan_Lang 10-Dec-13 9:10am
   
If you copy directly from the solution you only get one space between each block of ***, because it's HTML-formatted! If you click on "Improve Solution" you can see the original code has multiple spaces though. ;-)
enhzflep 10-Dec-13 9:57am
   
Oh dear. Yes indeed - totally missed that point. Thank-you for restraint that exceeded mine.

:Pours himself a strong coffee then walks over to gaze in mirror and says 'you snooze, you lose!':
nv3 22-Aug-18 17:35pm
   
I am reading this entire thread and wondering what's the big deal of solving the problem in one line. It just depends on how long that line may be. With very few exception, C code is not dependent on any line structure. So every C program can be basically be written in one single line. Not that this would make any sense to a human reader :-)

I am glad the teacher solved the misunderstanding. Otherwise I would not have seen what good it would do to a class of beginners, actually any C or C++ class.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100