Basic Loop Help (Beginner) - Somewhat Urgent

Hello so for my c++ homework I need to write nested for loops that print an hourglass. I have searched through the forums and haven't really found a solid answer. I'm not looking for someone to give me the answer but really just provide assistance as I am struggling with the class and I cannot make office hours in time for when this is due.

So the program reads in how many #'s in the top row and how many rows. So if I read in 4 #'s in the top row and 3 rows, it will print:

####
_##_
#### (assuming _ is just a space)

Cannot be less than 3 in the top row and less than 1 row. The hourglass cannot lead to less than 3 #'s in the middle. So if I did like 15 in the top row and 3 rows it would print

################
_##############_
__############__
_##############_
################

I need to use nested for loops. I have the basics setup so if you type in invalid numbers it asks you to retype in numbers that work, but I really don't understand the nested for loops. Any help is appreciated, thank you!
Hi..
I have made the program for you.
In case of any query, please feel free to ask.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace std;

int main()
{
    int NoOfHashes,NoOfRows,R1; int space_counter=1,chkflag;
    do
    {
        cout << "Enter no of hashes: " << endl;
        cin>>NoOfHashes;
        cout << "Enter no of rows: " << endl;
        cin>>NoOfRows;



        if(NoOfRows%2==0)
        {
            chkflag=1;
            R1=NoOfRows/2;
        }
        else
        {
            R1=NoOfRows/2+1;
            chkflag=2;
        }
        if(NoOfRows<3)
        {
            cout<<"ERROR: Minimum No of Rows Allowed is 3\n";
        }
        if(NoOfRows>(NoOfHashes-chkflag))
        {
            cout<<"ERROR: By the given data the middle row will contain less than 3 #s. Please enter data again\n";
        }

     }while(  NoOfRows>(NoOfHashes-chkflag) || NoOfRows<3  );

    for(int i=1;i<=R1;i++)
    {
        for(int j=0;j<space_counter;j++)
           cout<<" ";

        for(int j=0;j<NoOfHashes;j++)
            cout<<"#";
        space_counter++;
        NoOfHashes-=2;

        cout<<endl;
    }

    if(NoOfRows%2==0)
    {
        R1=NoOfRows/2;
        NoOfHashes-=2;
        space_counter++;
    }
    else
        R1=NoOfRows/2;


    for(int i=1;i<=R1;i++)
    {
        for(int j=0;j<space_counter-2;j++)
           cout<<" ";

        for(int j=0;j<NoOfHashes+4;j++)
            cout<<"#";

        NoOfHashes+=2;
        space_counter--;
        cout<<endl;
    }
    return 0;
}

Last edited on
Topic archived. No new replies allowed.