Enunciado
Escribir un procedimiento que, dada una matriz cuadrada de caracteres inicializada en blancos y su dimensión, almacene asteriscos en las dos diagonales, principal y secundaria. Escribir un pequeño programa de prueba.
La diagonal principal de una matriz está definida por los índices i = j, mientras que la diagonal secundaria, por los índices i = n – j + 1, en donde n es la dimensión de filas y columnas.
Este ejemplo se va a utilizar para mostrar distintas formas posibles de recorrido de la diagonal principal y secundaria. En todos los casos sólo se accede a las posiciones necesarias.
Código
Python
# Un solo ciclo, por filas, orden de acceso : 1,1 ; 1,n
# ; 2,2 ; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
# A single cycle, row, access command: 1,1 ; 1,n ; 2,2
# ; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
def ASTERISCOS1( m, n ):
i = 0 ;
while i < n:
m[i][i] = '*'
m[i][n - i - 1] = '*'
i += 1
return m
# Un solo ciclo, por columnas, orden de acceso : 1,1 ;
# n,1 ; 2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
# A single cycle, column, access command: 1,1 ; n,1 ;
# 2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
def ASTERISCOS2( m, n ):
i = 0
while i < n:
m[i][i] = '*'
m[n-i-1][i] = '*'
i += 1
return m
# Dos ciclos, en uno recorre la diagonal principal y en
# el otro, la secundaria
# Two cycles, one runs the main diagonal and the other,
# secondary
def ASTERISCOS3( m, n ):
i = 0 ; j = 0 ;
while i < n and j < n:
m[i][j] = '*'
i += 1 ; j += 1
i = 0
j = n-1
while i < n and j >= 0:
m[i][j] = '*'
i += 1 ; j -= 1
def main():
while True:
n = int( input( "\nIngresar/Enter dimension: " ) )
m = []
for i in range( n ):
m.append( [" "] * n )
# En este punto se "llama" a cualquiera de los
# subprogramas
# At this point it is "called" to any of the
# subprograms
#ASTERISCOS1( m , n ) ;
#ASTERISCOS2( m , n ) ;
ASTERISCOS3( m , n ) ;
print( "\n" )
for i in range ( n ):
for j in range ( n ):
print( str(m[i][j]).rjust(3), end="" )
print( "\n" )
resp = input("Presionar/Press S to exit ").upper()
if resp == "S": break
main()
C++
#include <iostream>
#include <iomanip>
using namespace std ;
#include <conio.h>
// Un solo ciclo, por filas, orden de acceso : 1,1 ;
// 1,n ; 2,2 ; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
// A single cycle, row, access command: 1,1 ; 1,n ; 2,2
// ; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
void ASTERISCOS1( char m [][10], int n )
{
int i ;
i = 0 ;
while ( i < n )
{
m[i][i] = '*' ;
m[i][ n - i - 1] = '*' ;
i++ ;
}
}
// Un solo ciclo, por columnas, orden de acceso : 1,1 ;
// n,1 ; 2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
// A single cycle, column, access command: 1,1 ; n,1 ;
// 2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
void ASTERISCOS2( char m [][10], int n )
{
int i ;
i = 0 ;
while ( i < n )
{
m[i][i] = '*' ;
m[n - i - 1][i] = '*' ;
i++ ;
}
}
// Dos ciclos, en uno recorre la diagonal principal y
// en el otro, la secundaria
// Two cycles, one runs the main diagonal and the
// other, secondary
void ASTERISCOS3( char m [][10], int n )
{
int i, j ;
i = 0 ; j = 0 ;
while ( i < n && j < n )
{
m[i][j] = '*' ;
i++ ; j++ ;
}
i = 0 ; j = n-1 ;
while ( i < n && j >= 0 )
{
m[i][j] = '*' ;
i++ ; j-- ;
}
}
int main( )
{
int n, i, j ;
char m[10][10] ;
do
{
cout << "Ingresar/Enter dimension: " ;
cin >> n ;
for( i=0 ; i<n ; i++ )
for( j=0 ; j<n ; j++ )
m[i][j] = ' ' ;
// En este punto se "llama" a cualquiera de los
// subprogramas
// At this point it is "called" to any of the
// subprograms
//ASTERISCOS1( m , n ) ;
//ASTERISCOS2( m , n ) ;
ASTERISCOS3( m , n ) ;
cout << endl ;
for( i=0 ; i<n ; i++ )
{
for( j=0 ; j<n ; j++ )
cout << setw(3) << m[i][j] ;
cout << endl ;
}
cout << endl ;
cout << "Presionar/Press ESC to exit" ;
cout << endl ;
}
while ( getch() != char (27) ) ;
return 0 ;
}
Pascal
Program Array01 ;
uses CRT ;
const nmax = 10 ;
type
dim = 1 .. nmax ;
matriz = array [ dim , dim ] of char ;
var
m : matriz ; n , i , j : dim ;
{ Un solo ciclo, por filas, orden de acceso : 1,1 ; 1,n
; 2,2 ; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
A single cycle, row, access command: 1,1 ; 1,n ; 2,2
; 2,n-1 ; 3,3 ; 3,n-2 ; ... ; n,n ; n,1
}
Procedure ASTERISCOS1( var m : matriz ; n : dim ) ;
var i : dim ;
begin
i := 1 ;
while ( i <= n ) do
begin
m [i , i] := '*' ;
m [i , n - i + 1] := '*' ;
i := i + 1
end ;
end ;
{ Un solo ciclo, por columnas, orden de acceso : 1,1 ;
n,1 ; 2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
A single cycle, column, access command: 1,1 ; n,1 ;
2,2 ; n-1,2 ; 3,3 ; n-2,3 ; ... ; n,n ; 1,n
}
Procedure ASTERISCOS2( var m : matriz ; n : dim ) ;
var i : dim ;
begin
i := 1 ;
while ( i <= n ) do
begin
m [i , i] := '*' ;
m [n - i + 1 , i] := '*' ;
i := i + 1
end ;
end ;
{ Dos ciclos, en uno recorre la diagonal principal y en
el otro, la secundaria
Two cycles, one runs the main diagonal and the other,
secondary
}
Procedure ASTERISCOS3( var m : matriz ; n : dim ) ;
var i , j : dim ;
begin
i := 1 ; j := 1;
while ( i <= n ) and ( j <= n ) do
begin
m [i , j] := '*' ;
i := i + 1 ; j := j + 1 ;
end ;
i := 1 ; j := n ;
while ( i<=n ) and ( j>=1 ) do
begin
m[ i , j ] := '*';
i := i + 1 ; j := j - 1 ;
end
end;
begin
repeat
ClrScr ;
write( 'Dimension (maximo 10)/' ) ;
write( 'Dimention (maximun 10) : ' ) ;
readln( n ) ;
for i := 1 to n do
for j := 1 to n do
m[ i , j ] := ' ' ;
{ En este punto se "llama" a cualquiera de los
subprogramas
At this point it's "called" to any of the subprograms }
ASTERISCOS1( m , n ) ;
{ ASTERISCOS2( m , n ) ; }
{ ASTERISCOS3( m , n ) ; }
for i := 1 to n do
begin
for j := 1 to n do
write( m [ i , j ] ) ;
writeln
end ;
writeln;
write( 'Presione ESC para terminar ' ) ;
writeln( 'o Enter para continuar' ) ;
write( 'Press ESC to finish ' ) ;
writeln( 'or Enter to continue' ) ;
until ReadKey = #27
end.
Diagramas
Subprogramas









Programa Principal


