Skip to content

Commit 1c00dba

Browse files
committed
that prints the sum of the two diagonals of a square matrix of integers.
1 parent ef42bf1 commit 1c00dba

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
16.5 KB
Binary file not shown.

0x07-pointers_arrays_strings/8-main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int c3[3][3] = {
12+
{0, 1, 5},
13+
{10, 11, 12},
14+
{1000, 101, 102},
15+
};
16+
int c5[5][5] = {
17+
{0, 1, 5, 12124, 1234},
18+
{10, 11, 12, 123521, 12512},
19+
{1000, 101, 102, 12545, 214543435},
20+
{100, 1012451, 11102, 12545, 214543435},
21+
{10, 12401, 10452, 11542545, 1214543435},
22+
};
23+
print_diagsums((int *)c3, 3);
24+
print_diagsums((int *)c5, 5);
25+
return (0);
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* File: 8-print_diagsums.c
3+
* Auth: Gedeon Obae Gekonge
4+
*/
5+
6+
#include "holberton.h"
7+
#include <stdio.h>
8+
9+
/**
10+
* print_diagsums - Prints the sum of the two diagonals
11+
* of a square matrix of integers.
12+
* @a: The matrix of integers.
13+
* @size: The size of the matrix.
14+
*/
15+
void print_diagsums(int *a, int size)
16+
{
17+
int index, sum1 = 0, sum2 = 0;
18+
19+
for (index = 0; index < size; index++)
20+
{
21+
sum1 += a[index];
22+
a += size;
23+
}
24+
25+
a -= size;
26+
27+
for (index = 0; index < size; index++)
28+
{
29+
sum2 += a[index];
30+
a -= size;
31+
}
32+
33+
printf("%d, %d\n", sum1, sum2);
34+
}

0 commit comments

Comments
 (0)