// n: row; m: col
int delimit_width(int n, int m)
{
int s = n * m;
int w = 3;
while(s){
w ++;
s /= 10;
}
return w;
}
// n: row; m: col
bool allocate_array(int*** arr, int n, int m)
{
if(n < 1 || m < 1){
return false;
}
int** a = new int*[n];
for(int i = 0; i < n; ++i){
a[i] = new int[m];
}
*arr = a;
return true;
}
// n: row; m: col
void init_array(int** arr, int n, int m)
{
int k = 1;
for(int i = 0; i < n; ++ i){
for(int j = 0; j < m; ++ j){
arr[i][j] = k++;
}
}
}
// n: row; m: col
void print_array(int** arr, int n, int m)
{
cout << "array size: " << n << endl;
int w = delimit_width(n, m);
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cout.width(w);
cout << std::right;
cout << arr[i][j] ;
}
cout << endl;
}
cout << endl;
}
// row: n; col: m
void print_result(int** arr, int n, int m)
{
cout << "array size: " << n << endl;
int w = delimit_width(n, m);
int col = 0;
int row = 0;
for(int row = 0; row < m; ++ row){
for(int col = 0; col <= row; ++col){
cout.width(w);
cout << std::right << arr[row][col] ;
}
cout << endl;
}
for(int row = m; row < 2 * m - 1; ++row){
int len = m - (row - (m - 1)) - 1;
for(int col = 0; col <= len; ++col){
cout.width(w);
cout << std::right << arr[row][col] ;
}
cout << endl;
}
cout << endl;
}
void transform_array(int** out_arr, int** in_arr, int n, int m)
{
for(int i = 0; i < n; i ++){
int in_col = 0;
int in_row = i;
int out_col = i;
int out_row = i;
in_row = i;
while(in_col < n - i){
out_arr[out_row][out_col] = in_arr[in_row][in_col];
in_col ++;
out_row ++;
}
in_col = n - i - 1;
in_row ++;
while(in_row < n){
out_arr[out_row][out_col] = in_arr[in_row][in_col];
in_row ++;
out_row ++;
}
}
}
// row: n; col: m
void delete_array(int** arr, int n, int m)
{
for(int i = 0; i < n; ++i){
delete[] arr[i];
}
if( n != 1){
delete[] arr;
}
}
int main(int argc, char* argv[])
{
int** arr;
int** res;
for(int i = 2; i < 10; ++i){
if(!allocate_array(&arr, i, i)){
cout << "array size is less than 1" << endl;
continue;
}
allocate_array(&res, 2 * i - 1, i);
init_array(arr, i, i);
print_array(arr, i, i);
transform_array(res, arr, i, i);
print_result(res, 2 * i - 1, i);
delete_array(arr, i, i);
delete_array(res, 2 * i - 1, i);
}
return 0;
}
Output
array size: 2
1 2
3 4
array size: 3
1
2 3
4
array size: 3
1 2 3
4 5 6
7 8 9
array size: 5
1
2 4
3 5 7
6 8
9
array size: 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
array size: 7
1
2 5
3 6 9
4 7 10 13
8 11 14
12 15
16
array size: 5
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
array size: 9
1
2 6
3 7 11
4 8 12 16
5 9 13 17 21
10 14 18 22
15 19 23
20 24
25
array size: 6
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
array size: 11
1
2 7
3 8 13
4 9 14 19
5 10 15 20 25
6 11 16 21 26 31
12 17 22 27 32
18 23 28 33
24 29 34
30 35
36
array size: 7
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
array size: 13
1
2 8
3 9 15
4 10 16 22
5 11 17 23 29
6 12 18 24 30 36
7 13 19 25 31 37 43
14 20 26 32 38 44
21 27 33 39 45
28 34 40 46
35 41 47
42 48
49
array size: 8
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
array size: 15
1
2 9
3 10 17
4 11 18 25
5 12 19 26 33
6 13 20 27 34 41
7 14 21 28 35 42 49
8 15 22 29 36 43 50 57
16 23 30 37 44 51 58
24 31 38 45 52 59
32 39 46 53 60
40 47 54 61
48 55 62
56 63
64
array size: 9
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 78 79 80 81
array size: 17
1
2 10
3 11 19
4 12 20 28
5 13 21 29 37
6 14 22 30 38 46
7 15 23 31 39 47 55
8 16 24 32 40 48 56 64
9 17 25 33 41 49 57 65 73
18 26 34 42 50 58 66 74
27 35 43 51 59 67 75
36 44 52 60 68 76
45 53 61 69 77
54 62 70 78
63 71 79
72 80
81
Press any key to continue . . .
Solution
// PrintShape.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
//Sample Output:
//1,
//2, 4,
//3, 5, 7,
//6, 8,
//9
*/
//Sample Input:
// [[1, 2, 3],
// [4, 5, 6],
// [7, 8, 9]]
Problem
//Given an array of NxN, print its diagonals from top-right to bottom-left.