number pattern struggles

So our professor asked us to make a program using nested loops that would output
      1
     246
    357911
  46810121416
579111315171921
  681012141618
   79111315
     81012
       9



And I already got how to the the diamond shape down, but I can't for the life of me figure out the algorithm for the program.

so far I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream.h>
#include<conio.h>
int main()
{

	for(int a=1; a<=5; a++)
	{
		for(int b=4; b>=a; b--)
		{
			cout<<" ";
		}

		for(int c=1; c<=a*2-1; c++)
		{
                       cout<<a*c;
		}

		cout<<"\n";
	}

	getch();
	return 0;
}


Which is the first half of the pyramid. but its output so far is:

    1
   246
  36913
 48121620
51015202530


Can anyone help me with this? What algorithm can I use so that I can achieve my desired output? Your help would be greatly appreciated. Thank you!
Last edited on
closed account (1CfG1hU5)
these for loops do not need the braces. will still be affected by
for loop with integer "a" encompassing them.

1
2
3
4
5
6
7
8
9
for(int b=4; b>=a; b--)
{
			cout<<" ";
}

for(int c=1; c<=a*2-1; c++)
{
                       cout<<a*c;
}


changed to this:

1
2
3
4
5
6
for(int b=4; b>=a; b--)
     cout<<" ";


for(int c=1; c<=a*2-1; c++)
     cout<<a*c;


two lines or more of code under a loop, requires the braces.

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{

int i, numbers[20];

srand((unsigned) time(NULL));

 // set all array values to 0 because...4 the hell of it

for (i = 0; i < 20; i++)
    numbers[i] = 0;    

  // make random numbers and print each to screen one at a time

printf("array elements are 0 to 19. total 20\n");

for (i = 0; i < 20; i++)
  {
     numbers[i] = rand() % 20+1;

     if (numbers[i] < 10)
          printf("numbers[%d] is %02d \n", i, numbers[i]);
       else
          printf("numbers[%d] is %d \n", i, numbers[i]);
  }

return 0;
}
Last edited on
Every number in a row is equal to previous + 2, not previous + first like you have now
each row prints +2 numbers than previous until 5, then -2.
so you can use another variable k in inner loop, declared outside it.
I am very thankful for your help! But excuse my stupidity and ignorance, how exactly is "previous +2" applied in the program codes? I'm afraid that I'm not well adjusted with the programming terms as of now since I am fairly new.
Calcualte hom many times you need to loop and starting value. Then each iteration add 2 to starting value:
1
2
3
4
5
6
7
for(int row = 0; row <= max_rows; ++row) { //Draws triangle
    int start = row;
    for(int i = 0; i < row*2 - 1; ++i) {
        std::cout << start;
        start += 2;
    }
}
@animezel,
line1: 1 > 1
line2: 2,4,6 > 3
line3: 3,5,7,9,11 > 5

watch the pattern
Thank you so much, it worked! If it wouldn't be too much trouble, can you help me out on the bottom reverse pyramid? For some reason it doesn't come out as a reverse pyramid as I want it to be. Would it have the same values or will the values be completely different?

Edit: Nvm finally figured it out by myself. Thank you for those who've helped!
Last edited on
similar to this..
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
#include <iostream>
using namespace std;

int main ()
{
  int n=5;

	for (int i=1; i<=2*n-1; i++) 
	{		
		int p = 1;
		
		for(int su=n-1; su>=i ;su--)		
			cout << "+";		
		
		for(int sl= i-n-1; sl>=0 && i>n ; sl--)		
			cout << "-";		
		
		for (int j=i, k=i;   ;  )
		{
			if(i<=n)
			{	
			  cout << j;
			  j+=2;
			  if (p > 2*i-2) break;
			  p++;
			} 
			else 
			{	
			  cout << k;	
			  k+=2;	
			  if (p > 2*i-2 - 4*(i-n) ) break;
			  p++;	
			}		
		}   	
    	cout << "\n";
	} 	
   	

  return 0;	
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

int main()
{
	int row, col, terms, blanks;

	for (row = 1; row < 10; row++)
	{
		terms = 9 - 2 * abs(row - 5);
		blanks = abs(5 - row);

		for (col = 0; col < blanks; col++)
			std::cout << ' ';

		for (col = 0; col < terms; col++)
			std::cout << row + col * 2;

		std::cout << std::endl;
	}

	return 0;
}


The intractable blanks ...
Last edited on
Topic archived. No new replies allowed.