I tried to create an hourglass with asterisk characters using 'for' loop... both decrement and increment works fine, but I got stucked with text alignment where those asterisks supposed to be printed in the center and justified to bring an hourglass look instead of being sticked to left or right of the screen...

any clue to get alignment works with loop result?
thx in advance and sorry for my bad English...

here's the code I made so far:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int bintang,baris;
  for(bintang=4; bintang>0; bintang--)
  {
    for(baris=0; baris<bintang; baris++) {
             cout.width(2);
             cout << "*";
             }
             cout << "\n";

             if (baris==1){
                           for(bintang=0; bintang<3; bintang++)
                              {
                                for(baris=-2; baris<bintang; baris++)
                                 {
                                   cout.width(2);
                                   cout << "*";
                                   }
                                   cout << "\n";
                         }
                         break;
             }
}
  cout << "\n";
  system("pause");
}

Edited 3 Years Ago by niario

I'd like to start with your looping structure. At line 18 above, if (baris == 1), since this is after the loop of baris over the range [0, bintang) is done, baris can only be 1 if bintang was also 1. And bintang == 1, looking at the outer loop, means this is the last time through the outer loop. Since the only thing you do at this point is loop over bintang and baris again (also making the final break; statement irrelevant), those loops can be performed after the initial loop completes, so you have a structure more like the following:

for(bintang=4; bintang>0; bintang--) {
    for(baris=0; baris<bintang; baris++) {
        // print something
    }
    cout << "\n";
}
for(bintang=0; bintang<3; bintang++) {
    for(baris=-2; baris<bintang; baris++) {
        // print something
    }
    cout << "\n";
}

(Also, consistent use of indentation will make it much easier people, yourself included, to read and understand the flow of your code!)

As far as centering your output, rather than having it left-aligned, consider how many leading spaces would be needed on each line, to position the asterisks where you want them. Then for each line (inside the for(bintang...) loops, but before the existing for(baris...) loops, add additional code to print the spaces (with no newlines, since you want the asterisks to print immediately after the spaces).

I knew somebody would correct me about the flow... :D
I just start learn programming for this last one month at campus and everything feels so dark... :(
think I'm not ready yet for this assignment... :(

if I go with your code, will you have any additional hint for me to get both the decrement of asterisks and increment of spaces to work at once in each triangle?

many thanks to you raptr_dflo

Edited 3 Years Ago by niario

Sorry to take so long getting back to you.

I think you're ready for this assignment, just take it one step at a time, and break larger steps into multiple smaller steps until each step is something you can do in a line or two of code.

For this assignment, you need to print out a certain number of rows of text, and for each row, you need to print out a certain number of asterisks ('*'), possibly with a space between successive ones, and preceded by enough spaces to get the alignment you need. You're already off to a good start, but since I don't have your actual assignment in front of me, all I can do is help you with the more general stuff. There are several ways to do almost anything, so unless your assignment requires you to use a specific approach (presumably because that's what the instructor is trying to teach at the moment), feel free to use whatever makes the most sense to you.

Suppose you need to produce the following output (a wine-glass, not an hour-glass):

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

How many lines of text is that? (10) How many asterisks are on each line? (It varies...) Are there spaces between asterisks? (Yes, 1, allowing odd and even numbers of asterisks to be centered with respect to each other)

I would implement this starting as I suggested above. A first version doesn't do anything:

for (line=0; line<10; line++) {
  // print the correct number of plain spaces to indent this row
  // print the correct number of space-separated asterisks
}

Note that to make a symmetric pattern, the amount of spaces to indent will always be the same for rows with the same number of asterisks. Can you determine a formula for that? It may help to create a grid of empty squares, 10 rows high and (as it would turn out for this example) 9 columns wide, and draw asterisks where you want them to end up.

I would then break the outer loop into a few pieces, say one for the "bowl" of the glass, one for the stem, and (since it isn't as nicely behaved) a couple one-liners for the base:

for (line=5; line>=1; line--) {
  // print spaces for indent
  indent = ???;
  for (i=0; i<indent; i++)
    cout << ' ';
  // print "line" space-separated asterisks
  for (i=0; i<line; i++)
    cout << '*';
    if (i < line-1)
      cout << ' ';
  // print a newline
  cout << endl;
}
for (line=0; line < 3; line++) {
  // print spaces for indent
  // print a single asterisk, and a newline
  cout << '*' << endl;
}
// print spaces for indent, then two space-separated asterisks, and a newline
// print spaces for indent, then five space-separated asterisks, and a newline

Can you finish this block of code? In particular, determining the correct value of indent. Note that your hour-glass assignment is probably actually easier: you can probably use the first outer loop above as-is (or close to it), and then you just need to figure out a loop to do the bottom half of the hour-glass.

This article has been dead for over six months. Start a new discussion instead.