Curso de Programación en C/Prog76
Ir a la navegación
Ir a la búsqueda
Prog76
1 #include <stdio.h>
2
3 #define FILAS 3
4 #define COLS 4
5
6 void sum_filas( int ar[][COLS], int filas );
7 void sum_cols( int [][COLS], int );
8 int sum2d( int (*ar)[COLS], int filas );
9
10 int main( void )
11 {
12 int junk[FILAS][COLS] = {
13 {2,4,6,8},
14 {3,5,7,9},
15 {12,10,8,6}
16 };
17
18 sum_filas( junk, FILAS );
19 sum_cols( junk, FILAS );
20 printf( "Sum de todos los elementos = %d\n", sum2d( junk, FILAS ) );
21
22 return 0;
23 }
24
25 void sum_filas( int ar[][COLS], int filas )
26 {
27 int c,
28 r,
29 tot;
30
31 for( r = 0; r < filas; r++ )
32 {
33 tot = 0;
34 for( c = 0; c < COLS; c++ )
35 tot += ar[r][c];
36 printf( "fila %d: sum = %d\n", r, tot );
37 }
38 }
39
40 void sum_cols( int ar[][COLS], int filas )
41 {
42 int c,
43 r,
44 tot;
45
46 for( c = 0; c < COLS; c++ )
47 {
48 tot = 0;
49 for( r = 0; r < filas; r++ )
50 tot += ar[r][c];
51 printf( "col %d: sum = %d\n", c, tot );
52 }
53 }
54
55 int sum2d( int ar[][COLS], int filas )
56 {
57 int c,
58 r,
59 tot = 0;
60
61 for( r = 0; r < filas; r++ )
62 for( c = 0; c < COLS; c++ )
63 tot += ar[r][c];
64
65 return tot;
66 }
Resultado
[rrc@Pridd ~]$ gcc -Wall -o Prog76 Prog76.c [rrc@Pridd ~]$ ./Prog76 fila 0: sum = 20 fila 1: sum = 24 fila 2: sum = 36 col 0: sum = 17 col 1: sum = 19 col 2: sum = 21 col 3: sum = 23 Sum de todos los elementos = 80 [rrc@Pridd ~]$