|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: May 2003
Città: Umbria
Messaggi: 220
|
Esercizio sui vettori
Sto seguendo il libro 'C corso completo di programmazione' di Deitel&&Deitel ed ho provato a fare un programma che gestisce una tartaruga meccanica che si muove in una matrice nelle quattro direzioni e puo' scrivere o non il suo percorso...
sono ancora alle prime armi percio' il programma è un po' lunghino.... Codice:
#include<stdio.h>
void spostamento(int, int, int [][], int, int, int, int []);
void visualizza(int [][]);
main()
{
int var=1, cont=0, penna=0, orien=2, cas, x=0, y=0;
int comandi[100];
int numcasel[99];
int tartaruga[2]={0};
int floor[50][50]={0};
floor[0][0]=8;
printf("Inserisci i comandi (9 per uscire): ");
while (var!=9)
{ scanf("%d", &var);
if (var!=9)
{ if (var==5)
{ printf("Di quante caselle? Inserisci: ");
scanf("%d", &cas);
comandi[cont]=var;
numcasel[cont]=cas;
}
else
{ comandi[cont]=var;
cont++;
}
}
}
for (var=0;var<=cont;var++)
{
switch (comandi[var])
{
case 1:
penna=1;
break;
case 2:
penna=0;
break;
case 3:
switch (orien)
{
case 1:
orien=3;
break;
case 2:
orien=4;
break;
case 3:
orien=2;
break;
case 4:
orien=1;
break;
}
break;
case 4:
switch (orien)
{
case 1:
orien=4;
case 2:
orien=3;
break;
case 3:
orien=1;
break;
case 4:
orien=2;
break;
}
break;
case 5:
spostamento(orien, numcasel[var], floor[][], penna, x, y, tartaruga[]);
break;
case 6:
visualizza(floor[][]);
break;
}//fine switch
x=tartaruga[0];
y=tartaruga[1];
}//fine for
printf("Ho la testa a pezzi\n\n\n");
}//fine main()
void spostamento(int orien, int caselle, int floor[][], int penna, int x, int y, int tartaruga[])
{
int a;
if (penna==0)
{
switch (orien)
{
case 1:
floor[y][x]=0;
floor[y-caselle][x]=8;
tartaruga[1]=y-caselle;
break;
case 2:
floor[y][x]=0;
floor[y+caselle][x]=8;
tartaruga[1]=y+caselle;
break;
case 3:
floor[y][x]=0;
floor[y][x+caselle]=8;
tartaruga[0]=x+caselle;
break;
case 4:
floor[y][x]=0;
floor[y][x-caselle]=8;
tartaruga[0]=x-caselle;
break;
}//fine swicth
}//fine if
else
{
switch (orien)
{
case 1:
for (a=y;a>=y-caselle;a++)
floor[a][x]=1;
floor[y-caselle][x]=8;
tartaruga[1]=y-caselle;
break;
case 2:
for (a=y;a<=y+caselle;a++)
floor[a][x]=1;
floor[y+caselle][x]=8;
tartaruga[1]=y+caselle;
break;
case 3:
for (a=x;a<=x+caselle;a++)
floor[y][a]=1;
floor[y][x+caselle]=8;
tartaruga[0]=x+caselle;
break;
for (a=x;a>=x-caselle;a++)
floor[y][a]=1;
floor[y][x-caselle]=8;
tartaruga[0]=x-caselle;
}//fine switch
}//fine else
}
void visualizza(int floor[][])
{
int a,b;
for (a=0;a<=49;a++)
{ for (b=0;b<=49;b++)
printf("%d", floor[a][b]);
printf("\n");
}
}
durante la compilazione mi da questi errori: [leonardo@localhost cap6]$ gcc programma9.c programma9.c: In function `main': programma9.c:74: parse error before ']' token programma9.c:77: parse error before ']' token programma9.c: In function `spostamento': programma9.c:94: invalid use of array with unspecified bounds programma9.c:95: invalid use of array with unspecified bounds programma9.c:99: invalid use of array with unspecified bounds programma9.c:100: invalid use of array with unspecified bounds programma9.c:104: invalid use of array with unspecified bounds programma9.c:105: invalid use of array with unspecified bounds programma9.c:109: invalid use of array with unspecified bounds programma9.c:110: invalid use of array with unspecified bounds programma9.c:121: invalid use of array with unspecified bounds programma9.c:122: invalid use of array with unspecified bounds programma9.c:127: invalid use of array with unspecified bounds programma9.c:128: invalid use of array with unspecified bounds programma9.c:133: invalid use of array with unspecified bounds programma9.c:134: invalid use of array with unspecified bounds programma9.c:138: invalid use of array with unspecified bounds programma9.c:139: invalid use of array with unspecified bounds programma9.c: In function `visualizza': programma9.c:151: invalid use of array with unspecified bounds sicuramente ho commesso qualche errore banale, perdonatemi grazie
__________________
gli hackers non esistono |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Questo non va bene:
visualizza(floor[][]); Devi scriverlo così: visualizza(floor); Inoltre quando dichiari una funzione a cui devi passare un vettore bidimensionale devi fare così: Codice:
void ccc(int i[][5])
{
}
int i[5][5];
ccc(i);
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:14.



















