Need help with looping structure

I am a beginner with Programming Fundamentals. I have an assignment that need help.

Write a C++ program which implements any looping structure to draw the illustrations given
below.

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
  
Shape 1
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Shape 2
*************
*************
*************
*************
*************

Shape 3
============
* *
* *
* *
* *
* *
============
Hello khoavo87,

So what have you done so far. post your code for assistance.

Andy
I do not know where I could begin. Tried to read my book but I have no ideas
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    for(int line = 10; line > 0 /* blah */){
        for (int /* blah blah blah */)
        {
            std::cout << row << ' ';
        }
        std::cout << /* blah blah */;
    }
    return 0;
}



1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1
Hello khoavo87,

I do not know where I could begin. Tried to read my book but I have no ideas

Even if it is wrong post some code that you have done so I know where you are at and I can work from there. kemort has given you a good start to work with.

This may be of some help:

http://www.cplusplus.com/doc/tutorial/control/#for

http://www.cplusplus.com/forum/beginner/60722/

read the section on for loops. You could always do a search on "for loops" here or on Google.

Till I know what you have done it is difficult to know where to start.

Hope that helps,

Andy
Last edited on
I may be late but I figured it out the first shape! (I am also a beginner and taught my self functions and parameters yesterday using solo-learn and Bucky's C++)

I made it so you can type in the numbers:
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
#include <iostream>

using namespace std;



int lLength(int x){
    int a = 1;
    do {
        cout<<a<<" ";
        a++;
        if (a == x){
            cout << "\n";
            x--;
            lLength(x);
        }
    } while (a < x);
    return x;
}



int main()
{
    cout << "Put a number in to declare the length: " << endl;
    int leng;
    cin >> leng;
    lLength(leng);
}





but It prints "1" one extra more, here is the output if x is 10:


1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1

Last edited on
Hi, Dorby.
Your recursive function works fine, but the fact is that if “a” starts with an initial value of 1, at least a “1” will be printed on screen, since the statement
cout<<a<<" ";
comes before the control-flow instruction
while (a < x);

It doesn’t seem you need any further help to find a solution to this issue, but please post again if you are stuck.
And khoavo87 too: if you are not happy about the help received, just ask again.
Last edited on
Thanks all of your help and I am working on codes you provide. I am going to ask if I have any questions
I am still stuck on the code you provide above. Sorry, cause I took this class online and this is the first assignment that I am having big problem. Could everybody give me more details about the code ? Which one should I put in the x- letter and for "Put a number in to declare the length: ", which number should I put in there ?
Last edited on
Hello khoavo87,

When you RUN it, it will ask for number (as an user input)

for example if you type in 5, it will print:


1 2 3 4
1 2 3
1 2
1


Try figuring out the code your self first, I you can't I will break it down to small pieces to help you understand :D.


thanks all of you for helping me but for the shape 2 and 3, I tried to create the code for them, but it did not work as well :(
closed account (48T7M4Gy)
So what code have you got :)
Ye, lets see what you have got.
I got the code

#include<iostream>
using namespace std;

int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 13; j++)
cout << '*';
cout << endl;
}



system("pause");
return 0;
}

for shape 2 as

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

but I am still working on shape 3, does everyone give me ideas, and I would like to ask that if I want to combine three shapes for one C++ program, is it impossible ?

closed account (48T7M4Gy)
Well done.

For shape 3 you might like to consider breaking it down to 3 sub-shapes, the first and last being the same. And yes you can combine all of the 3 shapes into one program. Try it, just copy and paste each as a separate section the run ...
I tried to combine two code for shape 1 and shape 2 but they appeared the error message
closed account (48T7M4Gy)
Show me the code you used pls :)
Here combined shape 1 and shape 2.

I got an error for System ("pause"), so I removed it :/.

And here is the code:

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
#include <iostream>

using namespace std;


//First shape
int lLength(int x){ 
    int a = 1;
    do {
        if (a < x){cout<<a<<" ";}
        a++;
        if (a == x){
            cout << "\n";
            x--;
            lLength(x);
        }
    } while (a < x);
    return x;
}



int main()
{
    cout << "Put a number in to declare the length: " << endl;
    int leng;
    cin >> leng;
    lLength(leng); 
    
    
    // 2nd Shape
    int i, j;
    for (i = 1; i <= 5; i++)
    {
    for (j = 1; j <= 13; j++)
    cout << '*';
    cout << endl;
    }

    return 0;
}
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
75
76
77
#include <iostream>

using namespace std;


//First shape
int lLength(int x){ 
    int a = 1;
    do {
        if (a < x){cout<<a<<" ";}
        a++;
        if (a == x){
            cout << "\n";
            x--;
            lLength(x);
        }
    } while (a < x);
    return x;
}



int main()
{
    cout << "Put a number in to declare the length: " << endl;
    int leng;
    cin >> leng;
    lLength(leng); 
    
    
    // 2nd Shape
    int i, j;
    for (i = 1; i <= 5; i++)
    {
    for (j = 1; j <= 13; j++)
    cout << '*';
    cout << endl;
    }

  // 3rd Shape
  int size;

	cout << "please enter the size of the hollow square:" << endl;
	cin >> size;
	if (size < 0) {
		cout << "Please enter a postive number: " << endl;
		cin >> size;
	}

	// This outer for-loop prints 1 row of output per iteration
	else
	{
		for (int row = 0; row < size; row++) {
			// This inner for-loop prints 1 column per iteration
			for (int col = 0; col < size; col++) {
				if (row == 0 || row == (size - 1))
				{
					if (col == 0 || col == size - 1)
						cout << "=";
					else {

						cout << "=";
					}

				}
				else if (col == 0 || col == (size - 1))
					cout << "*";
				else
					cout << " ";
			}
			// After printing all columns, print the newline character
			cout << endl;
		}
	} 
    return 0;
}


is it correct?
Last edited on
closed account (48T7M4Gy)
Well done.
Topic archived. No new replies allowed.