Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
73
Task/Pascal-matrix-generation/C/pascal-matrix-generation.c
Normal file
73
Task/Pascal-matrix-generation/C/pascal-matrix-generation.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void pascal_low(int **mat, int n) {
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
for (j = 0; j < n; ++j)
|
||||
if (i < j)
|
||||
mat[i][j] = 0;
|
||||
else if (i == j || j == 0)
|
||||
mat[i][j] = 1;
|
||||
else
|
||||
mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
|
||||
}
|
||||
|
||||
void pascal_upp(int **mat, int n) {
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
for (j = 0; j < n; ++j)
|
||||
if (i > j)
|
||||
mat[i][j] = 0;
|
||||
else if (i == j || i == 0)
|
||||
mat[i][j] = 1;
|
||||
else
|
||||
mat[i][j] = mat[i - 1][j - 1] + mat[i][j - 1];
|
||||
}
|
||||
|
||||
void pascal_sym(int **mat, int n) {
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
for (j = 0; j < n; ++j)
|
||||
if (i == 0 || j == 0)
|
||||
mat[i][j] = 1;
|
||||
else
|
||||
mat[i][j] = mat[i - 1][j] + mat[i][j - 1];
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int **mat;
|
||||
int i, j, n;
|
||||
|
||||
/* Input size of the matrix */
|
||||
n = 5;
|
||||
|
||||
/* Matrix allocation */
|
||||
mat = calloc(n, sizeof(int *));
|
||||
for (i = 0; i < n; ++i)
|
||||
mat[i] = calloc(n, sizeof(int));
|
||||
|
||||
/* Matrix computation */
|
||||
printf("=== Pascal upper matrix ===\n");
|
||||
pascal_upp(mat, n);
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');
|
||||
|
||||
printf("=== Pascal lower matrix ===\n");
|
||||
pascal_low(mat, n);
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');
|
||||
|
||||
printf("=== Pascal symmetric matrix ===\n");
|
||||
pascal_sym(mat, n);
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue