Acasa Tehnologie Parcurgere de matrice in spirala

Parcurgere de matrice in spirala

by Dragos Schiopu

>

//parcurgerea unei matrici in spirala
#include
#include
#include

void citire(int m, int n, int a[20][20],char c)
{
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
printf("%c[%i][%i]=",c,i,j);
scanf("%d",&a[i][j]);
}
}

void afisare(int m, int n, int a[20][20])
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
printf(" %d ",a[i][j]);
printf("n");
}
}

void main()
{
int n;
int a[20][20],i,j;
printf("n=");
scanf("%d",&n);

printf("nCitire matricen");
citire(n,n,a,'A');
printf("Afisare matrice n");
afisare(n,n,a);
printf("nParcurgere Spiralan");

for (i=1;i<=(n/2)+(n%2);i++)
{
for (j=i; j<=m-i+1; j++) printf("%d ",a[i][j]);
for (j=1+i; j<=n-i+1; j++) printf("%d ",a[j][m-i+1]);
for (j=m-i;j>=i;j--) printf("%d ",a[n-i+1][j]);
for (j=n-i; j>=i+1; j--) printf("%d ",a[j][i]);
}
getch();
}

s-ar putea sa-ti placa

0 comentarii

George 04-04-2012 - 18:50

dar in pascal