thedickblack

Cuestionario de Programación #6 Módulo #3 - Funciones y Matr

Oct 18th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None | 0 0
  1. EJERCICIO 1 (MATRIZ IDENTIDAD)
  2.  
  3. #include <stdio.h>
  4. int re_diagonal ( int filas,int matriz[][filas]);
  5. int re_ceros ( int filas,int matriz[][filas]);
  6. int main (void){
  7.     int flag_ceros,flag;
  8.     int filas;
  9.     int i,j;
  10.     do{
  11.         scanf("%d",&filas);
  12.         int matriz[filas][filas];
  13.         if(filas <= 0){
  14.             break;
  15.         }
  16.         for(i=0;i<filas;i++){
  17.             for(j=0;j<filas;j++){
  18.                 scanf("%d",&matriz[i][j]);
  19.             }
  20.         }
  21.         flag=re_diagonal(filas,matriz);
  22.         flag_ceros=re_ceros(filas,matriz);
  23.         if ((flag_ceros == 1) && (flag == 1)) {
  24.             printf("SI\n");
  25.         }
  26.         else{
  27.             printf("NO\n");
  28.         }
  29.     }while(filas > 0);
  30. }
  31. int re_diagonal (int filas,int matriz[][filas]){
  32.     int i;
  33.     int cont=0;
  34.    
  35.     for(i=0;i<filas;i++){
  36.         if(matriz[i][i] == 1){
  37.             cont++;
  38.         }
  39.     }
  40.     if (cont == filas){
  41.         return 1;
  42.     }
  43.     else{
  44.         return 0;
  45.     }
  46. }
  47. int re_ceros (int filas,int matriz[][filas]){
  48.     int i,j;
  49.     int cont=0;
  50.     for(i=0;i<filas;i++){
  51.         for(j=0;j<filas;j++){
  52.             if(matriz[i][j]==0){
  53.                 cont++;
  54.             }
  55.         }
  56.     }
  57.     if( cont == ((filas*filas)-filas) ){
  58.         return 1;
  59.     }
  60.     else{
  61.         return 0;
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. EJERCICIO 2 (BUSCAMINAS)
  69.  
  70. #include <stdio.h>
  71. int cuentaMinas (int filas,int columnas,char matriz[][columnas]);
  72.  
  73. int main (void){
  74.     int filas,columnas;
  75.     int total=0;
  76.     do{
  77.         scanf("%d %d",&columnas,&filas);
  78.         getchar();
  79.         if((filas > 0) && (columnas > 0)){
  80.             char matriz[filas][columnas];
  81.             int i,j;
  82.    
  83.             for(i=0;i<filas;i++){
  84.                 for(j=0;j<columnas;j++){
  85.                     scanf("%c",&matriz[i][j]);
  86.                 }
  87.                 getchar();
  88.             }
  89.    
  90.             total=cuentaMinas(filas,columnas,matriz);
  91.             printf("%d\n",total);
  92.         }
  93.     }while((filas != 0) && (columnas != 0));
  94. }
  95.  
  96. int cuentaMinas (int filas,int columnas,char matriz[][columnas]){
  97.     int i,j,minas,cont;
  98.    
  99.     cont=0;
  100.     for(i=1;i<filas-1;i++){
  101.         for(j=1;j<columnas-1;j++){
  102.             minas=0;
  103.             if(matriz[i][j] == '-'){
  104.                 if(matriz[i-1][j-1]=='*'){
  105.                     minas++;
  106.                 }
  107.                 if(matriz[i-1][j]=='*'){
  108.                     minas++;
  109.                 }
  110.                 if(matriz[i-1][j+1]=='*'){
  111.                     minas++;
  112.                 }
  113.                 if(matriz[i][j-1]=='*'){
  114.                     minas++;
  115.                 }
  116.                 if(matriz[i][j+1]=='*'){
  117.                     minas++;
  118.                 }
  119.                 if(matriz[i+1][j-1]=='*'){
  120.                     minas++;
  121.                 }
  122.                 if(matriz[i+1][j]=='*'){
  123.                     minas++;
  124.                 }
  125.                 if(matriz[i+1][j+1]=='*'){
  126.                     minas++;
  127.                 }
  128.             }
  129.             if(minas >= 6){
  130.                 cont++;
  131.             }
  132.         }
  133.     }
  134.     return cont;
  135. }
  136.  
  137.  
  138.  
  139. EJERCICIO 3 (SOMBRAS EN EL CAMPING)
  140.  
  141. #include <stdio.h>
  142.  
  143. //------------------------PROTOTIPO DE FUNCIONES--------------------------------------------------
  144. void iniciacion (int filas,int columnas,int matriz[][columnas]);
  145. void asignacion_arboles ( int filas, int columnas ,  int matriz[][columnas],int largo);
  146. void sombras (int filas, int columnas, int matriz[][columnas]);
  147. void cuenta_sombras (int filas, int columnas,int matriz[][columnas]);
  148.  
  149. //--------------PROGRAMA PRINCIPAL-------------------
  150. int main (void){
  151.     int filas,columnas,arboles;
  152.     do{
  153.         scanf("%d%d%d",&columnas,&filas,&arboles);
  154.         if((filas==0)&&(columnas==0)&&(arboles == 0)) break;
  155.        
  156.         int matriz[filas+1][columnas+1];
  157.         if( (arboles == 0) && (filas!=0) && (columnas!=0)){
  158.             printf("0\n");
  159.         }
  160.         else{
  161.             iniciacion(filas,columnas,matriz);
  162.             asignacion_arboles(filas,columnas,matriz,arboles);
  163.             sombras(filas,columnas,matriz);
  164.             cuenta_sombras(filas,columnas,matriz);
  165.                    
  166.         }
  167.        
  168.     }while ( (filas != 0) && (columnas != 0) );
  169.     return 0;
  170. }
  171.  
  172. //-------------------------FUNCIONES A UTILIZAR------------------------------------------------
  173. void iniciacion (int filas,int columnas,int matriz[][columnas]){//LIMPIA MATRIZ
  174.     int i,j;
  175.    
  176.     for(i=1;i<=filas;i++){
  177.         for(j=1;j<=columnas;j++){
  178.             matriz[i][j]=0;
  179.         }
  180.     }
  181. }
  182.  
  183. void asignacion_arboles (int filas, int columnas ,  int matriz[][columnas],int largo){// ASIGNA UBICACION DE LOS ARBOLES.
  184.     int i,x,y;
  185.     for(i=0;i<largo;i++){
  186.         scanf("%d %d",&x,&y);
  187.         matriz[y][x]=1;
  188.     }
  189. }
  190.  
  191. void sombras (int filas, int columnas, int matriz[][columnas]){ //ASIGNA UBICACION DONDE HABRA SOMBRA POR CADA ARBOL.
  192.     int i,j;
  193.     for(i=1;i<=filas;i++){
  194.         for(j=1;j<=columnas;j++){
  195.             if(matriz[i][j] == 1){
  196.                 if((matriz[i-1][j-1] != 1) && (i-1 >= 1) && (j-1 >= 1)){
  197.                     matriz[i-1][j-1]=3;
  198.                 }
  199.                 if((matriz[i-1][j]!=1)&&(i-1 >= 1)){
  200.                     matriz[i-1][j]=3;
  201.                 }
  202.                 if((matriz[i-1][j+1]!=1)&&(i-1>=1)&&(j+1 <= columnas)){
  203.                     matriz[i-1][j+1]=3;
  204.                 }
  205.                 if((matriz[i][j-1]!=1)&&(j-1 >=1)){
  206.                     matriz[i][j-1]=3;
  207.                 }
  208.                 if((matriz[i][j+1]!=1)&&(j+1<=columnas)){
  209.                     matriz[i][j+1]=3;
  210.                 }
  211.                 if((matriz[i+1][j-1]!=1)&&(i+1 <=filas)&&(j-1>=1)){
  212.                     matriz[i+1][j-1]=3;
  213.                 }
  214.                 if((matriz[i+1][j]!=1)&&(i+1 <= filas)){
  215.                     matriz[i+1][j]=3;
  216.                 }
  217.                 if((matriz[i+1][j+1]!=1) && (i+1 <= filas)&&(j+1<=columnas)){
  218.                      matriz[i+1][j+1]=3;
  219.                 }
  220.             }
  221.         }
  222.     }
  223. }
  224.  
  225. void cuenta_sombras (int filas, int columnas,int matriz[][columnas]){ //REALIZA LA CUENTA DE LA CANTIDAD DE CARPAS QUE PUEDEN QUEDAR CUBIERTAS POR SOMBRA
  226.     int i,j;
  227.     int cont=0;
  228.  
  229.     for(i=1;i<=filas;i++){
  230.         for(j=1;j<=columnas;j++){
  231.             if(matriz[i][j]==3){
  232.                 cont++;
  233.             }
  234.         }
  235.     }
  236.     printf("%d\n",cont);
  237. }
Add Comment
Please, Sign In to add comment