-
-
Save XePeleato/01d5825baf29cc62b7bc0f26ed662a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SZ 4 | |
void suma_diags(int a[][SZ], int *ret) | |
{ | |
int i, j, k, veces; | |
/* Para cada diagonal (de las de arriba) */ | |
for (k = 1; k < (2 * (SZ + 1)) / 2; k++) | |
{ | |
int it = 0; | |
/* Cada diagonal hay un sumando mas */ | |
for (i = 0; i < k; i++, it++) { | |
j = SZ - k + it; | |
printf("\n%d m[%d][%d] = %d\n", k, i, j, a[i][j]); | |
ret[2 * SZ - k - 1] += a[i][j]; | |
} | |
} | |
veces = k; | |
/* Para cada diagonal (de las de abajo*/ | |
for (k = 1, i = 1; k < ((2 * (SZ + 1)) / 2) - 1; k++, veces++) | |
{ | |
for (i = k, j = 0; i <= SZ - 1; i++) { | |
printf("\n%d m[%d][%d]\n", k, i, j); | |
ret[2 * SZ - veces - 1] += a[i][j]; | |
j++; | |
} | |
} | |
} | |
int main() | |
{ | |
int i; | |
int a[SZ][SZ] = { {2, 5, 7, 1}, | |
{2, 0, 3, 1}, | |
{2, 1, 2, 0}, | |
{5, 3, 1, 2}, | |
}; | |
int ret[SZ * 2 - 1] = {}; | |
suma_diags(a, ret); | |
printf("\n("); | |
for (i = 0; i < 2 * SZ - 1; i++) | |
printf("%d, ", ret[i]); | |
printf(")\n"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment