|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
[C] - syntax error : missing ';' before 'type'
Ciao a tutti, espongo subito il mio problema.
questo è il programma che ho: Codice:
#include <stdio.h> #define N 7 int diff (int[], int, int[]); void main() { int i; int a[N]={6,2,3,12,5,8,20}; int b[N]; for(i=0; i<N; i++){ b[N]=diff(a, N, b); printf("L'array delle differenze è: "); } int diff (int a[], int N, int b[]) { int i; for(i=1; i<N-1; i++) b[i]=a[i+1]-a[i]; return b[i]; } Compiling... main.c C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(15) : error C2143: syntax error : missing ';' before 'type' C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(20) : warning C4098: 'main' : 'void' function returning a value Error executing cl.exe. main.obj - 1 error(s), 1 warning(s) ________________________________________________________ Perchè, qualsiasi cosa scrivo mi da sempre lo stesso errore che ho segnato in rosso??? Dov'è sbagliata la function diff?? Grazie per le eventuali risposte ![]() ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Manca la parentesi graffa chiusa prima di "int diff"...
|
![]() |
![]() |
![]() |
#3 | |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Quote:
![]() ![]() Grazie cionci |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Sep 2002
Città: Celano (AQ) Segno_Zodiacale: Leone Ascendente: Cammello Segni_Particolari: Quello
Messaggi: 9571
|
![]() |
![]() |
![]() |
![]() |
#5 |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Nooooooooooooo l'ho messa la {...ma ora mi da 4 errori!!!!!
![]() vabbè ci rinuncio. ciauz |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jul 2003
Città: Rimini
Messaggi: 3392
|
mi sa che nn hai inizializzato bene la N
![]()
__________________
. |
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
Quote:
tu ne avevi messa solo una, logico che se ne aggiungi un' altra aperta gli errori aumentino ![]()
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 16:58. |
|
![]() |
![]() |
![]() |
#8 | |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Quote:
![]() ![]() |
|
![]() |
![]() |
![]() |
#9 | |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Quote:
![]() ![]() |
|
![]() |
![]() |
![]() |
#10 | ||
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
questo warning
Quote:
Quote:
![]()
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 17:05. |
||
![]() |
![]() |
![]() |
#11 |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Riscritta bene, cioè cosi:
Codice:
#include <stdio.h> #define N 7 int diff (int[], int, int[]); void main() { int i; int a[N]={6,2,3,12,5,8,20}; int b[N]; for(i=0; i<N; i++){ b[N]=diff(a, N, b);} printf("L'array delle differenze è: "); } int diff (int a[], int N, int b[]) { int i; for(i=1; i<N-1; i++) b[i]=a[i+1]-a[i]; return b[i]; } --------------------Configuration: Differenze in avanti - Win32 Debug-------------------- Compiling... main.c C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(15) : error C2143: syntax error : missing ')' before 'constant' C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(15) : error C2143: syntax error : missing '{' before 'constant' C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(15) : error C2059: syntax error : '<Unknown>' C:\Documents and Settings\Peppe\Desktop\Array1D\Differenze in avanti\main.c(15) : error C2059: syntax error : ')' Error executing cl.exe. main.obj - 4 error(s), 0 warning(s) ___________________________________________________ tutti errori riferiti alla riga in rosso. ![]() ![]() ![]() |
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
stavo appunto per chiedere che errori ti dà, perchè ora che leggo meglio, l' algoritmo mi suona un po' strano
... nel main hai un loop per riempire gli N elementi di b con una funzione, e va bene però la diff fa (qualcosa) sugli elementi di a per poi ritornare (come secondo membro di un' assegnazione) di nuovo degli elementi di b... ![]() ![]() in pratica vuoi fare <delle cose> su una stessa variabile (l' array b) da due posti diversi annidando due cicli: sicuro che ti serva proprio una cosa del genere ?
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 17:27. |
![]() |
![]() |
![]() |
#13 | |
Member
Iscritto dal: Sep 2005
Messaggi: 51
|
Quote:
![]() ![]() |
|
![]() |
![]() |
![]() |
#14 | |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
Quote:
![]()
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 18:28. |
|
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Nov 2003
Messaggi: 980
|
"N" è una costante che vale in tutte le funzioni del tuo programma, quindi che bisogno hai di passarlo alla funzione "diff"?
"N" non è un int... è come se facessi int 6 ![]() Ultima modifica di kk3z : 19-11-2005 alle 17:42. |
![]() |
![]() |
![]() |
#16 | |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
Quote:
come mi chiedevo anche per quale motivo nel loop si va ogni volta ad assegnare non l' elemento i-esimo di b, ma quello N esimo (che non esiste, perchè l' indice dell' ultimo elemento in un array di dimensione N è N-1), quindi già qui c' è un errore ma anche se fosse scritto N-1, tu lasceresti indefiniti gli altri N-1 elementi dell' array per avere un <qualcosa> scritto nell' ultima cella
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 17:57. |
|
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Codice:
int b[N]; for(i=0; i<N; i++){ b[N]=diff(a, N, b);}
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
![]() |
![]() |
![]() |
#18 | |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
Quote:
![]() se non ricordo male, inizializzare l' array a dimensione fissa impedisce di sfondarlo dopo... è il caso delle stringhe o dei vettori di cui non si sa a priori la dimensione a essere problematico ...allora, partiamo dal presupposto che tu voglia ottenere di salvare in b un vettore di differenze di questo tipo ( 2-6, 3-2, 12-3, 5-12, 8-5, 20-8 ) si vede che tale vettore sarà composto da 6, cioè 7-1, cioè N-1, elementi, il che forse spiegherebbe l' N-1 nel loop della diff()... però allora secondo me si dovrà dimensionare b a N-1 e non avrà senso nè fare un loop annidato, nè iterare N volte quello nel main: l' algoritmo che si ottiene ha un implementazione in codice già ai minimi termini, quindi non avrebbe molto senso dichiarare funzioni aggiuntive come diff(a,i) per fare a[i+1]-a[i] ... (*) (*) a meno che il vero scopo dell' esercizio non fosse semplicente farti fare pratica con le chiamate a funzione per valore e per riferimento... ![]() o a meno che non fosse esplicitamente richiesto che l' array delle differenze generato non fosse anche ordinato o elaborato in altro modo... Codice:
#include <stdio.h> #define N 7 // int diff (int[], int, int[]); inutile void main() { int a[N]={6,2,3,12,5,8,20}; int b[N-1]; for(int i=0; i<N-1; i++) { b[i] = a[i+1] - a[i]; //invece di diff(a, N, b); // } printf("L'array delle differenze è: %d %d %d %d %d %d\n", b[0], b[1], b[2], b[3], b[4], b[5] ); }
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 19-11-2005 alle 19:58. |
|
![]() |
![]() |
![]() |
#19 |
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4741
|
in caso la richiesta fosse stata ordinare l' array delle differenze risultanti,
guarda se ti piace questa soluzione con un insertion sort (riciclabile!) da 4 lines of code reali ![]() Codice:
#include <stdio.h> #include <stdlib.h> #define N 7 void insertsort(int val, int* vector) // O(n^2) // N>1 // { int i=0; for (int j=0; j<N-1; j++) if (val>vector[i]) i++; for (int k=N-2; k>i; k--) vector[k+1]=vector[k]; vector[i]=val; }; void main() { int diff; int A[N]={6, 2, 3, 12, 5, 8, 20}; int B[N-1]; for (int i=0; i<N-1; i++) { diff=A[i+1]-A[i]; insersort(diff, B); } printf("L'array delle differenze è: %d %d %d %d %d %d\n", B[0], B[1], B[2], B[3], B[4], B[5]); }
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 20-11-2005 alle 23:00. |
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: May 2003
Messaggi: 1113
|
il problema non è nell'array....ma è in questa:
Codice:
int diff(int a[], int N, int b[]) Codice:
int diff(int a[], int b[]) ![]()
__________________
| Athlon XP Barton 3000+ | CoolerMaster HAC-V81 | ASUS A7N8X DELUXE v2.0 | 2*256 PC3200 + 1*512 PC3200 = 1GB DDR400| ATI Radeon 9250 | HD 80Gb Maxtor SATA | Ali Q-TEC 550W Dual Fan GOLD PFC |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:40.