Can Someone please explain the logic of loops to me.

My professor and TA are not good teachers, whatsoever... I tried to ask them about the logic of looping, but they won't give me an answer. Instead, they keep beating around the bush, and I am even more confused than when I started. I understand the logic behind looping, it's more in the lines of making pictures with my loops is my problem.

For example:

We have to write a program that's a pyramid using numbers that are odd based on the input of the user.


so if the user inputs "13" it should look something like this:



      7
     876
    98765
   0987654
  109876543
 21098765432
3210987654321



I know this program requires one outer loop, and two inner loops. But I don't understand the logic of setting the loops up, that's where I'm stuck. I failed my midterm because of this.

Here's what I have for the for loop right now:




Input:	for(int i=1; i<=numRows; i++)
	{	
		for(int j=1; j<=i; j++)
		{	
			cout << " ";
		}

		for (int j=1; j<=i; j++)
		{
			cout << numRows/2+1;
			
		}

		i++;
			cout <<endl;
		
	}


Note: the input i used was 13.




Output:
 7
   777
     77777
       7777777
         777777777
           77777777777
             7777777777777


It's not what I need, but I'm getting somewhere. I know this program requires the "%" somewhere, and there are too many spaces between each line as well.


What would be a good way to think about loops. I have to start at a certain number for the first one which is numRows/2+1, and when I go down a row, I gain two numbers and lose one space.



Any and all help would be appreciated.



Thank you.
Last edited on
Start from something easy: It's obvious that, while the example correct output has a decreasing number of spaces per line, yours has an increasing one. Your first inner loop increases in iterations as i increases. What would you need to change to make it decrease in iterations as i increases?
here is my version :

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
#include <iostream>
int main(int argc, char* argv[])
{
	//ask the user to enter an unsigned integer
	int n = 13;

	int middleCol = (n + 1) / 2;
	int nCols = n;
	int nRows = (n + 1) / 2;

	for (int row = 0,pre = middleCol,post = middleCol; 
		row < nRows; 
		row++, pre--, post++)
	{
		int col;
		for (col = 0; col < pre; col++)
		{
			std::cout << " ";
		}
		for (col = pre; col < middleCol; col++)
		{
			int value = n - col + 1;
			while (value > 9)
				value -= 10;
			std::cout << value;
		}
		std::cout << middleCol;
		for (col = middleCol; col < post; col++)
		{
			int value = n - col;
			while (value > 9)
				value -= 10;
			std::cout << value;
		}
		for (col = post; col < nCols; col++)
		{
			std::cout << " ";
		}
		std::cout << std::endl;
	}


	return 0;
}
My professor and TA are not good teachers, whatsoever... I tried to ask them about the logic of looping, but they won't give me an answer. Instead, they keep beating around the bush, and I am even more confused than when I started.

Have you considered the possibility that they're trying to get you to think for yourself, so that you can start to understand how to think logically about a problem, rather than simply regurgitating a solution that they've spoon-fed you?

Good teaching isn't about feeding students answers they can recite by rote. It's about helping them understand the principles and processes that enable them to work things out for themselves.

I suspect that you went looking to be be told the answer, and when, instead, they tried to give you hints to direct you in working it out yourself, you've simply rejected that as "not good" teaching, because it wasn't the complete solution you were hoping for. And then you came whining about it here and blaming other people for your reluctance to actually think about the problem you've been set.
To solve a problem like this, start by making a table with some key
data. For example, when the input is 13, here are the number of spaces
in each row, and the starting digit that you print:

Row  Spaces   Starting #
1       6        7
2	5	 8
3	4	 9
4	3	 0
5	2	 1
6	1	 2
7	0	 3


Hmm. What's up with the starting number? Ah! I see that it's the
last digit of a larger number:

Row  Spaces   last digit of
1       6        7
2	5	 8
3	4	 9
4	3	 10
5	2	 11
6	1	 12
7	0	 13

Now, look at this and express the values using a formula for the row
number:

Row	Spaces	last digit of
R	  7-R	 R+6


Okay, what if the input number is 15? I suspect R=8 and the triangle
looks like:
       8
      987
     09876
    1098765
   210987654
  32109876543
 4321098765432
543210987654321

Row  Spaces   last digit of
1       7        8
2       6        9
3	5	 10
4	4	 11
5	3	 12
6	2	 13
7	1	 14
8	0	 15

R      8-R       R+7

Now we can guess the general pattern:

Input: I
Rows: (I+1)/2
Spaces: Rows-R
Last digit: Rows+R-1

And the code is (in pseudo-code)
1
2
3
4
5
input I
Rows = (I+1)/2;
for (R = 1 to Rows) {
    print Rows-R spaces
    for this digits

That last "print the digits" is a problem all it's own. Use the same technique to determine how to do it. You know that your printing the last digit if a number. Where does the number start? How many digits are printed? How can you set up the loop?
Topic archived. No new replies allowed.