Instituto Politécnico Nacional
Arreglos bidimensionales
Secretaria Académica Dirección de Educación Superior ESIME Culhuacan M. en C. Lorena Azuara Pérez Ingeniería en Computación Fundamentos de Programación Alumno: Grupo: Fecha:
Práctica 12
!"#$%&'( "# $%&$#'() *+,- .)-$,($/ *# *+,'0(/&,* +*)- $/./1 0*2
*+%$,&+- . /01&2( 3%4.',/0%)/5 3%4.(#/0%) 35 '&(0/0 0* /#4/$*&/4(*&,% 6789:;
34%,(5166&74 %$7,&6+ <%+ /))*=#%+ >(+,%+ /&,*)(%)4*&,* +* $%&%$*& $%4% /))*=#%+ !"#$#%&"'#(")*&' 6'&/ +%#/ 0(4*&+(?&: @ +* $/)/$,*)(1/& .%) ,*&*) '& +%#% +'AB&0($*; C+,%+ /))*=#%+ +* $%&%$*& ,/4A(D& .%) *# ,D)4(&% *#',)'-
.("/&0,( 1 2(3%) 4&"&3)* 5(' )33&4*(' %!*,#$#%&"'#(")*&' +%& /E'*##%+ E'* ,(*&*& 4-+ 0* '&/ 0(4*&+(?& @5 *& $%&+*$'*&$(/5 4-+ 0* '& B&0($*; <%+ /))*=#%+ 4-+ '+'/#*+ +%& #%+ 0* 0%+ 0(4*&+(%&*+5 $%&%$(0%+ ,/4A(D& .%) *# &%4A)* 0* ,)6*)' % %),3#/&'- 8(& *4A/)=%5 *+ .%+(A#* $)*/) /))*=#%+ 0* ,/&,/+ 0(4*&+(%&*+ $%4% )*E'(*)/& +'+ /.#($/$(%&*+5 *+,% *+5 ,)*+5 $'/,)% % 4-+ 0(4*&+(%&*+; 7& /))*=#% .'*0* ,*&*) 4-+ 0* '&/ 0(4*&+(?&5 0* ,/# 4/&*)/ E'* F%)4* 4/,)($*+ 0* 0%+5 ,)*+ % /G& 4-+ 0(4*&+(%&*+; ,*)-.'/+
! %&'()*'&+
"
#
$
!
" # $
H(=')/ IJ;I C+,)'$,')/ 0* '& /))*=#%+ 0* 0%+ 0(4*&+(%&*+
C& *# /))*=#% A(0(4*&+(%&/# 0* #/ H(=')/ IJ;I5 #/+ F(#/+ +* *,(E'*,/& 0* K / % @ #/+ $%#'4&/+ 0* K / "7 *# &G4*)% 0* *#*4*&,%+ E'* ,*&0)- *# /))*=#% +*)- *# )*+'#,/0% 0*# .)%0'$,% 64 L I: M 6& L #:; C# +(+,*4/ 0* #%$/#(1/) '& *#*4*&,% +*)- .%) #/+ $%%)0*&/0/+ )*.)*+*&,/0/+ .%) +' &G4*)% 0* F(#/ @ +' &G4*)% 0* $%#'4&/ 6/ 5 A:;
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
<33&4*(' "!%?3#/(' 1 $& /)3)/,&3&' +(&,/M(+ ./)/ #/ 0*$#/)/$(?& 0* '& /))*=#% 0* 0%+ 0(4*&+(%&*+ *+2 ,#0(@$&@$),( "(%63&@$&*@)33&4*( ABC%&3($&D#*)'E ABC%&3($&.(*!%")'EF "#='&%+ *N*4.#%+ 0* 0*$#/)/$(?& 0* /))*=#%+ A(0(4*&+(%&/#*+ +%&2 /G)3 *#',)AHIEAJIEF #", 0!&',('AKEAHE F #", &L!#0('AME AJIEF #", %),3#>AMEA;EF
<%+ /))*=#%+ A(0(4*&+(%&/#*+ +* .'*0*& (&($(/#(1/)5 /# (='/# E'* #%+ 0* '&/ 0(4*&+(?&5 $'/&0% +* 0*$#/)/&; (&($(/#(1/$(?& $%&+,/ 0* '&/ #(+,/ 0* $%&+,/&,*+ +*./)/0/+ .%) $%4/+ @ *&$*))/0/+ *&,)* ##/>*+5 $%4% *& #%+ +(='(*&,*+ *N*4.#%+2 #", ,)6*)A;EAJE N OP:7 P;7 PJ7 PM7 PP7 PKQF % A(*& *& #%+ F%)4/,%+ 4/+ /4(=/A#*+2 #", ,)6*)A;EAJEN O OP:7 P;7 PJQ7 O PM 7 PP7 PKQQ F #", ,)6*)A;EAJE N OOP:7 P;7 PJQ7 OPM7 PP7 PKQQF 8* .'*0* /$$*0*) / #%+ *#*4*&,%+ 0* '& /))*=#% A(0(4*&+(%&/# 0* #/ 4(+4/ 4/&*)/ E'* +* /$$*0* / #%+ *#*4*&,%+ 0* '& /))*=#% '&(0(4*&+(%&/#5 *+ 0*$()5 / ,)/>D+ 0* +'+ +'AB&0($*+; 0(F*)*&$(/ *+ E'* *& '& /))*=#% A(0(4*&+(%&/# O/@ E'* *+.*$(F($/) #%+ +'AB&0($*+ 0* #/+ F(#/+ @ #/+ $%#'4&/+; ,)6*)A;EAJE N M-PF )'#&",('AMEARE N :SF T&",)' N ,)6*)A;EAPEF + F'&$(%&*+ 0* *&,)/0/ % +/#(0/ +* /.#($/& 0* (='/# F%)4/ / #%+ *#*4*&,%+ 0* '& /))*=#% A(0(4*&+(%&/#; P%) *N*4.#%5 #", ,)6*)AJEAMEF 2*(), 3&'#',&"/#)'AMEAPEF '/)"2UVW$X7 Y,)6*)A;EAJEZF 03#",2UVW$X7,)6*)A:EA:EZF '/)"2UVW2X7 Y3&'#',&"/#)'A;EAMEZF
8* .'*0* /$$*0*) / #%+ *#*4*&,%+ 0* '& /))*=#% A(0(4*&+(%&/#*+ 4*0(/&,* A'$#*+ /&(0/0%+; 8' +(&,/M(+ *+2 #", [717 $),('APEAPEF 2(3U[ N IF [ \ PF []]Z O 2(3U1NIF 1 \ PF 1]]Z O '/)"2UVW$X7 Y$),('A[EA1EZF Q Q
=- &" .- 5(3&") <>!)3) 8?3&>
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
CN*4.#%2 C+$)(A/ '& .)%=)/4/ E'* ##*&* '&/ 4/,)(1 0* Q M R $%& 0/,%+ /#*/,%)(%+ @ #%+ (4.)(4/; /*Ejemplo 12.1*/ #include #include #include void main( ) { int matriz[10][10], x, y, r, c; printf(“Numero de renglones: ”); scanf(“%d”, &r); printf(“Numero de columnas: ”); scanf(“%d”,&c); for(x = 0; x < r; x++) { for(y= 0; y < c; y++) { matriz[x][y] = 1 + rand()%9; printf(“%d ”, matriz[x][y]); } printf(“\n”); } getch(); }
B!%&3( $& 3&"4*("&'^ P B!%&3( $& /(*!%")'^ P SMPJ: H:J;: PMJRH J:;KR :J;KR
CN*4.#%2 C+$)(A/ '& .)%=)/4/ E'* =*&*)* #/ 4/,)(1 (0*&,(0/0 0* ,/4/S% Q M Q; /*Ejemplo 12.2*/ #include #include void main( ) { int matriz[10][10], x, y, t; printf(“Teclea el tamaño de la matriz: scanf(“%d”, &t); for(x = 0; x < t; x++) { for(y = 0; y < t; y++) { if(x == y) matriz[x][y] = 1; else matriz[x][y] = 0; printf(“%d ”, matriz[x][y]); } printf(“\n”); } getch(); }
_&/*&) &* ,)%)`( $& *) %),3#>^ R ”);
=- &" .- 5(3&") <>!)3) 8?3&>
:IIIIII I:IIIII II:IIII III:III IIII:II IIIII:I IIIIII:
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
CN*4.#%2 P)%=)/4/ E'* )*/#(1/ #/ +'4/ 0* 0%+ 4/,)($*+ 0* Q M Q; /*Ejemplo 12.3*/ #include #include #include void main( ) { int m1[10][10], m2[10][10], suma[10][10], t, i, j; printf(“Teclea el tamaño de la matriz: ”); scanf(“%d”, &t); for(i = 0; i < t; i++) { for(j = 0; j < t; j++) { m1[i][j] = 1 + rand()%9; m2[i][j] = 1 + rand()%9; suma[i][j] = m1[i][j] + m2[i][j]; } } printf(“\nContenido de las matrices\n”); printf(“Matriz 1\n”); for(i = 0; i < t; i++) { for(j = 0; j < t; j++) { printf(“%d ”,m1[i][j]); } printf(“\n”); }
printf(“Matriz 2\n”); for(i = 0; i < t; i++) { for(j = 0; j < t; j++) { printf(“%d ”,m2[i][j]); } printf(“\n”); } printf(“Suma\n”); for(i = 0; i < t; i++) { for(j = 0; j < t; j++) { printf(“%d ”,suma[i][j]); } printf(“\n”); } getch(); }
_&/*&) &* ,)%)`( $& *) %),3#>^ M .(",&"#$( $& *)' %),3#/&' =),3#> : :;:J :MPK K;:K RMJ: =),3#> ; ;J;P PM;: HRKS SPM: a!%) J P K H :M S :K S
J H R R
H R :P ;
=- &" .- 5(3&") <>!)3) 8?3&>
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
8$9+,,(--( I; C+$)(A/ '& .)%=)/4/ $%& /))*=#%+ A(0(4*&+(%&/#*+ 0* Q M R E'* )*/#($* #/ 4'#,(.#($/$(?& 0* 4/,)($*+; #include #include void main() { int fa,ca,fb,cb,r,c,d; int ma[10][10],mb[10][10], mc[10][10]; printf("Dame la dimension de la matriz A...\n"); printf("Da el numero de renglones: "); scanf("%d",&fa); printf("Da el numero de columnas: "); scanf("%d",&ca); printf("Lectura de la Matriz A: \n"); for (r=0 ; r
=- &" .- 5(3&") <>!)3) 8?3&>
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
J; C+$)(A/ '& .)%=)/4/ E'* $)** #/ ,)/&+.'*+,/ 0* '&/ 4/,)(1;
/*Práctica 12.2*/ #include #include #include void main( ) { int mat[10][10],r,c,i,j; printf(“Teclea el numero de renglones: ”); scanf(“%d”,&r); printf(“Teclea el numero de columnas: ”); scanf(“%d”,&c); printf(“MATRIZ ORIGINAL\n”); for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { mat[i][j] = 1 + rand() % 9; printf(“%d ”, mat[i][j]); } printf(“\n”); } printf(“MATRIZ TRANSPUESTA\n”); for(i = 0; i < c; i++) { for(j = 0; j < r; j++) { printf(“%d ”, mat[j][i]); } printf(“\n”); } getch(); }
_&/*&) &* "C%&3( $& 3&"4*("&'^ J _&/*&) &* "C%&3( $& /(*!%")'^ M =<_bcd ebcfcB<5 : M PR ; M : P R K M S =<_bcd _b
=- &" .- 5(3&") <>!)3) 8?3&>
839/,#/) :; <33&4*(' 6#$#%&"'#(")*&'
T; C+$)(A/ '& .)%=)/4/ E'* $)** '& $'/0)% 4-=($%; #include #include void main() { int mat[10][10],i,j,n, num; printf("Ingrese el tamaño del cuadro: "); scanf("%d", &n); if(n%2 == 1) { j = (n + 1) / 2; i = 1; mat[i][j] = 1; num = 2; while(num <= n*n) { if((num -1)%n == 0) { i++; } else { i = i -1; j = j +1; } if(i == 0) i = n; if (j > n) j=1; mat[i][j]=num; num = num +1; } printf("Cuadro magico\n"); i=1; while(i <= n) { j = 1; while (j <= n) { printf("%d ",mat[i][j]); j++; } printf("\n"); i++; } } else printf("Error de tamanio!!!"); getch(); }
Y&=)*+* *# ,/4/S% 0*# $'/0)%2 T 3'/0)% 4-=($% ZI[ T\] W^J
:1$9%&(4+,&( I; J; T; W;
U3?4% +* /$$*0* / '& +%#% *#*4*&,% *& '& /))*=#% A(0(4*&+(%&/#V U3?4% +* /$$*0* / '& $/0*&/ *& '& /))*=#% A(0(4*&+(%&/#V UP%) E'D &% +%& )*$%4*&0/A#*+ #%+ /))*=#%+ 0* 4-+ 0* 0%+ 0(4*&+(%&*+V X*+$)(A/ $'-#*+ +%& #/+ >*&,/N/+ 0* '+/) /))*=#%+ 0* 0%+ 0(4*&+(%&*+;
:(46-19&(4$9
=- &" .- 5(3&") <>!)3) 8?3&>