Acasa Tehnologie Parcurgerea in spirala a unei matrici

Parcurgerea in spirala a unei matrici

by Dragos Schiopu

>

//parcurgere matrice in spirala
#include <stdio.h>
#include <conio.h>
#include <math.h>


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],stsus,drsus,drjos,stjos,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");
stsus=1;drsus=n;drjos=n;stjos=1;
do
{ // --->
for(i=stsus;i<=drsus;i++)
printf(" %d ",a[stsus][i]);
stsus=stsus+1;
//in jos
for(i=stsus;i<=drjos;i++)
printf(" %d ",a[i][drjos]);
drjos=drjos-1;

// <---
for(i=drjos;i>=stjos;i--)
printf(" %d ",a[drjos+1][i]);

// in sus
for(i=drjos;i>=stsus;i--)
printf(" %d ",a[i][stsus-1]);
drsus=drsus-1;
stjos=stjos+1;
}
while(drjos>n/2);
getch();
}

s-ar putea sa-ti placa