Click here to Skip to main content
15,609,640 members

Comments by Member 8616148 (Top 6 by date)

Member 8616148 9-Dec-13 14:17pm View    
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" : ""))));
Member 8616148 9-Dec-13 14:04pm View    
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 13:48pm View    
In a console app?
Member 8616148 9-Dec-13 13:47pm View    
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:"
Member 8616148 9-Dec-13 13:34pm View    
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.