#include <stdio.h>
#include <iostream>
using namespace Std;
int main ()
{
int N;
int S, I, J;
int Squa;
cin>>n;
Squa = N*n;
/* Allocates space for pointers to int type pointers,
This pointer points to n int pointer */
int **a = (int * *) malloc (n sizeof (int));
if (a = = NULL)
return 0;
for (i = 0; i < N; i++)
{
if ((a[i] = (int *) malloc (n*sizeof (int))) = = NULL)
{
/* Assigns a value to each value (int pointer) of the preceding pointer so that it points to an int
Array, if the allocation fails, the release is placed on it
Space to apply for success * *
while (-i. >= 0)
{
Free (a[i]);
Free (a);
return 0;
}
}
}
for (i = 0; i < N; i++)
{
for (j = 0; J < N; J + +)
{
s = i + j;
if (S < N)
A[I][J] = s* (s + 1)/2 + (((i + j)% 2 = = 0)? i:j);
Else
{
s = (N-1-i) + (N-1-J);
A[I][J] = squa-s* (s + 1)/2-
(N-((((i + j)% 2 = = 0) i:j));
}
}
}
for (i = 0; i < N; i++)
{
for (j = 0; J < N; J + +)
{
printf ("%6d", A[i][j]);
}
printf ("\ n");
}
System ("pause");
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implement a n*n matrix in C + +, The matrix is incremented along 45 degrees to form an zigzag array