• Jueves 28 de Marzo de 2024, 22:09

Autor Tema:  Matriz En Espiral  (Leído 6532 veces)

blue_angel

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Matriz En Espiral
« en: Sábado 28 de Octubre de 2006, 01:19 »
0
como se hace una matriz en espiral, en la cual el usuario decida de cuanto por cuanto la matriz(filas y columnas) y en q posicion empieza y con q numero empieza a llenar en espiral?
ayudaaaaaaaaaa

wako13

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
Re: Matriz En Espiral
« Respuesta #1 en: Sábado 28 de Octubre de 2006, 02:01 »
0
/* Cargar una matriz, numérica entera, de orden N, a partir de un valor cualquiera, introducido por teclado, y recorriéndola en espiral.
Ej: N=4 ,Vi=6   Matriz resultante:    
                                                           6          7       8       9
                                                           17  18   19  10
                                                           16  21   20  11
                                                           15  14   13  12        */

Código: Text
  1. #include <stdio.h>
  2.  
  3.  
  4. void mostrar(int[][10],int);
  5. void espiral(int[][10],int,int);
  6.  
  7. void main()
  8. {
  9. int m[10][10],rango,a;
  10. system("cls");
  11. do{
  12. printf("¨Rango de la matriz(1-10)?");
  13. scanf("%d",&rango);
  14. }while(rango<=0||rango>10);
  15.  
  16. printf("Valor de A:");
  17. scanf("%d",&a);
  18. espiral(m,rango,a);
  19. system("cls");
  20. printf("\nDatos de la tabla...\n");
  21. mostrar(m,rango);
  22. printf("\n\nPulse una tecla para acabar...");
  23. system("pause>nul");
  24.  
  25. }
  26.  
  27. void espiral(int t[][10],int r,int n)
  28. {
  29. int tope,a,i,k;
  30. if(r%2) tope=r/2+1;
  31. else tope=r/2;
  32. for(a=0;a<tope;a++)
  33.  {
  34.   for(i=a,k=a;k<r-a;k++,n++) t[i][k]=n;
  35.   printf("\n");
  36.   for(i=a+1,k--;i<r-a;i++,n++) t[i][k]=n;
  37.   printf("\n");
  38.   for(k--,i--;k>=a;k--,n++) t[i][k]=n;
  39.   printf("\n");
  40.   for(k++,i--;i>a;i--,n++) t[i][k]=n;
  41.  }
  42. return;
  43. }
  44.  
  45. void mostrar(int t[][10],int r)
  46. {
  47. int i,k;
  48. for(i=0;i<r;i++)
  49. {
  50.   for(k=0;k<r;k++)
  51.      printf("%5d",t[i][k]);
  52.   printf("\n");
  53. }
  54. return;
  55. }
  56.