Salve,
stavo progettando questo programma che carica una matrice da un file seguendo un andamento degli indici "a spirale".
Il programma, però, anche se mi sembra logicamente corretto, non funziona...
Create un file e riempitelo così:
Questo, invece, è il codice:codice:4 1 8 7 2 3 4 9 1 5 7 3 2 5 4 7 8
Grazie degli eventuali aiuti.Codice PHP:#include <stdio.h>
#include <stdlib.h>
#define M 20
int caricaSpirale(int mat[][M]);
void stampaMatrice(int mat[][M], int dim);
int main()
{
int dim;
int mat[M][M];
dim=caricaSpirale(mat);
if (dim==-1)
{
printf("Errore\n");
system("PAUSE");
exit(1);
}
else
{
stampaMatrice(mat,dim);
printf("\n");
system("PAUSE");
}
}
int caricaSpirale(int mat[][M])
{
FILE* f;
int dim,est,dimOLD,j,i,temp;
char nomeFile[M];
printf("\nInserisci nome file:\t");
scanf("%s", nomeFile);
f=fopen(nomeFile,"r");
if (f==NULL)
{
fclose(f);
return -1;
}
else
{
fscanf(f,"%d",&dimOLD);
dim=dimOLD-1;
for (i=0; i<dim; i++)
for (j=0; j<dim; j++)
mat[i][j]=0;
printf("OK");
for (est=0; est<=dim/2; est++)
{
for (j=est; j<=dim-est; j++)
{
fscanf(f, "%d", temp);
mat[est][j]=temp;
// printf("%d %d\n", est, j);
}
for (i=est+1; i<=dim-est; i++)
{
fscanf(f, "%d", temp);
mat[i][dim-est]=temp;
// printf("%d %d\n", i, dim-est);
}
for (j=dim-est-1; j>=est; j--)
{
fscanf(f, "%d", temp);
mat[dim-est][j]=temp;
// printf("%d %d\n", dim-est, j);
}
for (i=dim-est-1; i>=est+1; i--)
{
fscanf(f, "%d", temp);
mat[i][est]=temp;
// printf("%d %d\n", i, est);
}
}
fclose(f);
return dim;
}
}
void stampaMatrice(int mat[][M], int dim)
{
int i,j;
printf("\n");
for (i=0; i<dim; i++)
{
for (j=0; j<dim; j++)
printf("%d\t", mat[i][j]);
printf("\n");
}
}
![]()

Rispondi quotando