pattern

closed account (23RkizwU)
A user inputs an integer number.


Last edited on
for an odd number you can load up your vector this way
num = num/2+1. //9 becomes 5
v.push_back(num); //5

then loop for i =1 to num/2
v.push_back(num-i); //4 | 3 (5-1, 5-2, 5-3, ...etc)
v.push_back(num+i); //6 | 7 (5+1, 5+2 etc)
giving v = {5,4,6,3,7,2,8,1,9}


then just a fancy loop to print out the values with spaces or tabs and end of lines.

Similar logic should work for evens with the zero added etc.

you don't even HAVE to store the values in a vector, you can just compute, print and throw away. The storage makes it a little easier to code and follow the logic end to end.
Last edited on
My apologies jonnin, I did it the hard way for fun. (I also saw the fact that it's (num-i, num+i) pattern, but chose this way cuz it was the easiest to just bruce-force type). [I made no attempt at brevity]

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

int v(int t, int n)
{
	if (t <= n/2)
	{
		return (n/2) - t;
	}
	else
	{
		return (n/2) - (n - t - 1);
	}
}

int main()
{
	int n = 9;
	
	char offset = '1';
	if (n % 2 == 0)
	{
		n += 1;
		offset = '0';
	}
	
	int size = n * (n / 2 + 1);
	std::vector<char> arr(size);
	for (int i = 0; i < size; i++)
		arr[i] = ' ';
	
	for (int i = 0; i < n; i++)
	{
		arr[v(i, n)*n + i] = static_cast<char>(i + offset);
	}
	
	
	for (int i = 0; i < size; i++)
	{
		std::cout << " " << arr[i];
		if (i % n == n - 1)
			std::cout << "\n";
	}
	
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
using namespace std;
#define WD 3

void writeNums( int a, int b )
{
   if ( a < b ) writeNums( a + 1, b - 1 );
   cout << setw( (a+1)*WD ) << a;
   if ( a < b ) cout << setw( (b-a)*WD ) << b;
   cout << '\n';
}

int main ()
{
   int N = 9;
   writeNums( N%2, N );
}
Last edited on
no need to apologize lol, its fun/school code and any solution that works is fine.
I mean lastchance's code is short, elegant, and sweet but its also likely incomprehensible to a new student.

Since OP is gone, I might as well post my solution too.

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
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

// return the minimum number of digits in the decimal representation of x
int ndigits( int x ) { return x ? std::floor( std::log10( x ) ) + 1 : 1; }

// return a string n spaces long
std::string blanks( int n ) { return std::string( n, ' ' ); }

int main( int argc, char** argv ) 
  try
  {
    int N = std::stoi( argv[1] );           // N   ← the maximum value to print
    int L, R = L = (N+1) / 2;               // L,R ← the left and right value for each line
    int a = ndigits( L ) - ndigits( L-1 );  // a   ← see explanation below

    // print the first line at the point of the chevron
    std::cout << std::left << blanks( (L-N%2)*3 ) << L << "\n";
    
    // print each additional line
    while (--L, R++ < N)
    {
      std::cout << blanks( (L-N%2)*3     ) << std::setw( 3 ) << L;
      std::cout << blanks( (R-L-1)*3 + a ) << std::setw( 3 ) << R << "\n";
      // a is the amount we adjust the distance between the left and right
      // halves of the chevron so that there is always an equal number of 
      // spaces between the center number and the left and right halves.
      // Run the program for N = 18, 20, and 21 to observe the effect.
    }
  } 
  catch (...) { }
$ ./a 9
            5
         4     6
      3           7
   2                 8
1                       9

If you like triangles, check out my Pascal’s Triangle Challenge in the Lounge.
Last edited on
Registered users can post here. Sign in or register to post.