Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi A - F > Algoritmi e strutture dati > e C (salute!)
Pages (2): [1] 2 »   Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
e C (salute!)

Sto cercando di capire questo cavolo di C per l'esame di algoritmi.

Ho provato a fare un esercizio di mescolamento mazzi che c'e' sul Deitel&Deitel, ma mi da un errore -1 in fase di esecuzione, e non so proprio cosa ho sbagliato nel programma. Sicuro che qua ci siano persone che sanno programmare in C, vi pongo il listato.
C'e' qualcuno che mi puo' aiutare a capire dove sta l'inghippo?

Io lo so che ho sbagliato qualcosa sui puntatori, ma non so cosa!!!! :(

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void mescola (int [][13], const char *[], const char *[]);

int main()
{
char *semi[4]= {"quadri", "cuori", "fiori", "picche"};
char *numeri[13] = {"asso", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove", "dieci", "fante", "donna", "re"};
int mazzo[4][13]={0};

srand (time(NULL));
mescola (mazzo, semi, numeri);

return 0;
}


void mescola (int mazzo [][13], const char *s[], const char *n[])
{
int i, j, seme, numero;
void swap (int *, int *);

printf ("Sto mescolando il mazzo\n");

for (i=1; i<=52; i++) {
mazzo[(i-1)/13][(i-1)%13] = i;
};

for (j=1; j<=4; j++) {
for (i=1; i<=52; i++) {
int valore;
valore = rand () % 52;
swap (&mazzo[(i-1)/13][(i-1)%13], &mazzo[(valore)/13][(valore)%13]);
};
};
printf ("%8s di %-7s%c", n[numero], s[seme], i % 3 == 0 ? '\n' : '\t');
};

void swap (int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
};

/* rutt */

Grazie!!!!

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

01-12-2002 15:48
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Fatur
.distinto:signore

User info:
Registered: May 2002
Posts: 998 (0.12 al dì)
Location: Lotzorai
Corso: Ls.TI.COM.
Anno: primo
Time Online: 14 Days, 14:07:47 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Il gdb ti potrebbe essere utile... cmq appena vado su linux provo...

__________________
Non è difficile! E' impegnativo!!

01-12-2002 15:52
Click Here to See the Profile for Fatur Click Here to See the Blog of Fatur Click here to Send Fatur a Private Message Visit Fatur's homepage! Find more posts by Fatur Add Fatur to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Sto usando lcc per windows. Compila ansi c.

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

01-12-2002 16:12
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Fatur
.distinto:signore

User info:
Registered: May 2002
Posts: 998 (0.12 al dì)
Location: Lotzorai
Corso: Ls.TI.COM.
Anno: primo
Time Online: 14 Days, 14:07:47 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Allora usa il suo debug! E' abbastanza facile ed efficente(un po' meno la videoscrittura).

__________________
Non è difficile! E' impegnativo!!

01-12-2002 16:41
Click Here to See the Profile for Fatur Click Here to See the Blog of Fatur Click here to Send Fatur a Private Message Visit Fatur's homepage! Find more posts by Fatur Add Fatur to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Fatur
.distinto:signore

User info:
Registered: May 2002
Posts: 998 (0.12 al dì)
Location: Lotzorai
Corso: Ls.TI.COM.
Anno: primo
Time Online: 14 Days, 14:07:47 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: e C (salute!)

Originally posted by AlphaGamma

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void mescola (int [][13], const char *[], const char *[]);

int main()
{
char *semi[4]= {"quadri", "cuori", "fiori", "picche"};
char *numeri[13] = {"asso", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove", "dieci", "fante", "donna", "re"};
int mazzo[4][13]={0};

srand (time(NULL));
mescola (mazzo, semi, numeri);

return 0;
}


void mescola (int mazzo [][13], const char *s[], const char *n[])
{
int i, j, seme, numero;
void swap (int *, int *);

printf ("Sto mescolando il mazzo\n");

for (i=1; i<=52; i++) {
mazzo[(i-1)/13][(i-1)%13] = i;
};

for (j=1; j<=4; j++) {
for (i=1; i<=52; i++) {
int valore;
valore = rand () % 52;
swap (&mazzo[(i-1)/13][(i-1)%13], &mazzo[(valore)/13][(valore)%13]);
};
};
printf ("%8s di %-7s%c", n[numero], s[seme], i % 3 == 0 ? '\n' : '\t');
};

void swap (int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
};

/* rutt */



questo è il primo errore che ho visto. hai ridichiarato la funzione invece di usarla.

__________________
Non è difficile! E' impegnativo!!

01-12-2002 17:08
Click Here to See the Profile for Fatur Click Here to See the Blog of Fatur Click here to Send Fatur a Private Message Visit Fatur's homepage! Find more posts by Fatur Add Fatur to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
lorenzo
...ma anche no.

User info:
Registered: Jun 2002
Posts: 3149 (0.38 al dì)
Location: Milano
Corso: F49
Anno: 00000011
Time Online: 49 Days, 1:47:46 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: e C (salute!)

Originally posted by AlphaGamma
printf ("%8s di %-7s%c", n[numero], s[seme], i % 3 ==


Sostituisci i e j a numero e seme.

__________________
Non ti laureare, continua a cazzeggiare!

"È tutta merda..." - clod81

01-12-2002 17:49
Click Here to See the Profile for lorenzo Click Here to See the Blog of lorenzo Click here to Send lorenzo a Private Message Visit lorenzo's homepage! Find more posts by lorenzo Add lorenzo to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
lorenzo
...ma anche no.

User info:
Registered: Jun 2002
Posts: 3149 (0.38 al dì)
Location: Milano
Corso: F49
Anno: 00000011
Time Online: 49 Days, 1:47:46 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Re: e C (salute!)

Originally posted by Fatur
questo è il primo errore che ho visto. hai ridichiarato la funzione invece di usarla.


No, non è un errore.

Io però l'avrei dichiarata fuori lo stesso.

__________________
Non ti laureare, continua a cazzeggiare!

"È tutta merda..." - clod81

01-12-2002 17:52
Click Here to See the Profile for lorenzo Click Here to See the Blog of lorenzo Click here to Send lorenzo a Private Message Visit lorenzo's homepage! Find more posts by lorenzo Add lorenzo to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Fatur
.distinto:signore

User info:
Registered: May 2002
Posts: 998 (0.12 al dì)
Location: Lotzorai
Corso: Ls.TI.COM.
Anno: primo
Time Online: 14 Days, 14:07:47 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Re: Re: e C (salute!)

Originally posted by lorenzo
No, non è un errore.

Io però l'avrei dichiarata fuori lo stesso.

non è un errore?? boh.. però così non ho mai visto fare...

__________________
Non è difficile! E' impegnativo!!

01-12-2002 17:57
Click Here to See the Profile for Fatur Click Here to See the Blog of Fatur Click here to Send Fatur a Private Message Visit Fatur's homepage! Find more posts by Fatur Add Fatur to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Autovelox
Il Supremo

User info:
Registered: Oct 2002
Posts: 131 (0.02 al dì)
Location: Milano
Corso: Informatica
Anno: 9878 + o -
Time Online: 1 Day, 16:17:19 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: e C (salute!)

Originally posted by AlphaGamma

[...]

void mescola (int mazzo [][13], const char *s[], const char *n[])
{
int i, j, seme, numero;
void swap (int *, int *);

[...]


Qui usi la funzione swap senza averla dichiarata prima. Secondo me è un errore.


Originally posted by AlphaGamma

[...]

void swap (int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
};

/* rutt */

Grazie!!!!


I due parametri della funzione sono puntatori ad int. Te per invertirli usi una variabile d'appoggio, ma tale variabile deve essere puntetore ad int e non variabile int. Quindi dovrebbe essere cosi:

void swap (int *a, int *b)
{
int *c;
c = a;
a = b;
b = c;
};

O almeno cosi avrei fatto io!

__________________
"Il football non e' una questione di vita o di morte... E' molto piu' importante!"

Bill Shankly

01-12-2002 21:25
Click Here to See the Profile for Autovelox Click here to Send Autovelox a Private Message Find more posts by Autovelox Add Autovelox to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Re: e C (salute!)

Originally posted by Fatur
questo è il primo errore che ho visto. hai ridichiarato la funzione invece di usarla.


Quello era il prototipo. Sul libro c'e' scritto che lo puoi dichiarare locale (a me strippano queste storie!).

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

01-12-2002 23:47
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Re: e C (salute!)

Originally posted by lorenzo
Sostituisci i e j a numero e seme.


Perche'?

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

01-12-2002 23:47
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Re: e C (salute!)

Originally posted by Autovelox
Qui usi la funzione swap senza averla dichiarata prima. Secondo me è un errore.


I due parametri della funzione sono puntatori ad int. Te per invertirli usi una variabile d'appoggio, ma tale variabile deve essere puntetore ad int e non variabile int. Quindi dovrebbe essere cosi:

void swap (int *a, int *b)
{
int *c;
c = a;
a = b;
b = c;
};

O almeno cosi avrei fatto io!


No, non e' un errore: quello e' il prototipo. Si usa per utilizzare una funzione PRIMA di averla dichiarata. L'ho messo dentro per vedere se compilava come diceva il libro.

Anche sulla variabile di appoggio ti sbagli, per quanto poco conosca il C. Come l'hai scritta tu, scambi i puntatori anziche' i valori.

Prova a scriverla con questa definizione:

void swap (int *a const, int *b const);

... ed il compilatore ti da errore.

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

01-12-2002 23:53
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Sephirot
.grande:maestro.

User info:
Registered: Nov 2001
Posts: 3856 (0.45 al dì)
Location: South of No North
Corso: Informatica
Anno: III
Time Online: 60 Days, 15:06:08: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Alpha quando hai intenzione di dare l'esame? devi fare lo scritto o solo il progetto?

__________________
SEPHIROT
mi piacciono i bluvelvet menosi - http://www.menschenfreundlicher.ch/ - "secondo me basta copiarlo in notepad"

02-12-2002 00:28
Click Here to See the Profile for Sephirot Click Here to See the Blog of Sephirot Click here to Send Sephirot a Private Message Find more posts by Sephirot Add Sephirot to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
AlphaGamma
.piano ma pieni.

User info:
Registered: Jun 2002
Posts: 6732 (0.80 al dì)
Location:
Corso: Eh?
Anno: Io cosa?
Time Online: 21 Days, 21:22:01: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Devo fare il progetto e l'orale (sono con Torelli).

__________________
Attenzio', concentrazio', ritmo e VITALITÀ

02-12-2002 00:41
Click Here to See the Profile for AlphaGamma Click Here to See the Blog of AlphaGamma Click here to Send AlphaGamma a Private Message Visit AlphaGamma's homepage! Find more posts by AlphaGamma Add AlphaGamma to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
lorenzo
...ma anche no.

User info:
Registered: Jun 2002
Posts: 3149 (0.38 al dì)
Location: Milano
Corso: F49
Anno: 00000011
Time Online: 49 Days, 1:47:46 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: e C (salute!)

code:
printf ("%8s di %-7s%c", n[numero], s[seme], i % 3 == 0 ? '\n' : '\t');


Scusa, prima non mi sono spiegato bene.

Qui tu usi le variabili numero e seme come indice di vettore senza averle inizializzate prima. Quindi non hanno alcun valore sensato, e il programma ti va a cercare delle posizioni del vettore che non esistono!!

Leggendo il codice ho supposto che tali variabili debbano essere sostituite da di i (che puo' assumere valori da 1 a 13, quindi il numero della carta) e j (che va da 1 a 4, come i semi).

Inoltre tale istruzione deve esser inserita all'interno del primo ciclo for (quello con indice j), altrimenti non avrebbe senso.

Ti consiglio anche di modificare i cilci for in questo modo:
code:
for (j=0; j<4; j++) e for (i=0; i<52; i++)

e usare semplicemente "i" al posto di "i-1" all'interno dei cicli.

Per ulteriori delucidazioni chidi pure di persona, che forse facciamo prima.

__________________
Non ti laureare, continua a cazzeggiare!

"È tutta merda..." - clod81

02-12-2002 07:55
Click Here to See the Profile for lorenzo Click Here to See the Blog of lorenzo Click here to Send lorenzo a Private Message Visit lorenzo's homepage! Find more posts by lorenzo Add lorenzo to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 06:23.    Post New Thread    Post A Reply
Pages (2): [1] 2 »   Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.085 seconds (67.98% PHP - 32.02% MySQL) con 24 query.