Skip to content

Instantly share code, notes, and snippets.

@galaydaroman
Created May 14, 2020 00:02
Show Gist options
  • Save galaydaroman/161e2ebea3e7dc4d1ade6b946991cc24 to your computer and use it in GitHub Desktop.
Save galaydaroman/161e2ebea3e7dc4d1ade6b946991cc24 to your computer and use it in GitHub Desktop.
Valera lab
#include <iostream>
using namespace std;
int main()
{
// Data
int **matrix, size, counter = 0, max;
int x, y;
bool up = true;
cout << "Enter matrix dimension: Size = ";
cin >> size;
// Create matrix
matrix = new int*[size];
for (int i = 0; i < size; i++) {
matrix[i] = new int[size];
}
// start point
x = y = max = size - 1;
while (counter != size * size) {
matrix[y][x] = ++counter;
if (up) {
x++; y--;
if (x > max && y >= 0) { x--; up = !up; }
if (y < 0) { x-=2; y = 0; up = !up; }
} else {
x--; y++;
if (y > max && x >= 0) { y--; up = !up; }
if (x < 0) { y-=2; x = 0; up = !up; }
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%3d ", matrix[i][j]);
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment