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 > Chiarimento su riempimento e stampa vettore
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Chiarimento su riempimento e stampa vettore

Ciao a tutti! :)
Vorrei riempire un vettore con numeri casuali e stamparlo! Solo che difficoltà a capire dove sbaglio...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100

void riempi_array (int a[], int n);
void stampa_array (int a[], int n);

int main (void) {
int a[N] = {0}, n;
printf ("Inserisci n\n");
scanf ("%d", &n);
riempi_array (a, n);
stampa_array (a, n);
return 0;
}

void riempi_array (int a[], int n) {
int* p;
for (p = a; p< a+n; p++) {
srand (time(NULL));
*p = rand() % 99+1; //assegno al contenuto di numeri compresi tra 0 e 99
}
}

void stampa_array (int a[], int n) {
int* p;
printf ("vettore: ");
for (p = a; p< a+n; p++)
printf ("%d ",*p);
printf ("\n");
}

Grazie :shock:

17-11-2014 12:14
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
mark
.grande:maestro.

User info:
Registered: Oct 2003
Posts: 783 (0.10 al dì)
Location:
Corso: F49
Anno: finito!
Time Online: 8 Days, 18:34:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

code:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 void riempi_array (int a[], int n); void stampa_array (int a[], int n); int main (void) { int a[N] = {0}, n; printf ("Inserisci n\n"); scanf ("%d", &n); riempi_array (a, n); stampa_array (a, n); return 0; } void riempi_array (int a[], int n) { int* p; srand (time(NULL)); for (p = a; p< a+n; p++) { *p = rand() % 99+1; //assegno al contenuto di numeri compresi tra 0 e 99 } } void stampa_array (int a[], int n) { int* p; printf ("vettore: "); for (p = a; p< a+n; p++) printf ("%d ",*p); printf ("\n"); }


questa riga di codice --------> srand (time(NULL)); va messa fuori dal ciclo for()

__________________
Non ti perdere di coraggio se ti tocca lavorare molto e raccogliere poco.....

Last edited by mark on 17-11-2014 at 13:25

17-11-2014 13:23
Click Here to See the Profile for mark Click here to Send mark a Private Message Find more posts by mark Add mark to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

grazie :)

17-11-2014 14:39
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

ne approfitto per chiedere un'altra cosa sui vettori!
ho scritto questo codice per allocare un vettore con n elementi, dove n è inserito da tastiera.appena compilo ottengo 2 warning:

c:13:7: warning: conflicting types for built-in function ‘__builtin_alloca’ [enabled by default]
void *alloca (int n);
^
c:30:7: warning: conflicting types for built-in function ‘__builtin_alloca’ [enabled by default]
void *alloca (int n)
^

code:
int main (void) { int n, *a; //int *p; printf ("Inserisci n\n"); scanf ("%d", &n); a = (int*) alloca (n); free (a); return 0; } void *alloca (int n) { void *a; a = malloc (n * sizeof (int)); if (a == NULL) exit (1); return a; }

17-11-2014 15:43
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Cambia nome ad "alloca" e mettila prima del main :) (oltre ad importare le opportune librerie)

Last edited by Cronovirus on 17-11-2014 at 16:19

17-11-2014 16:15
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Ho importato le librerie e scritto il prototipo della funzione, anche se nn le ho scritte nel post. Il problema è solo per il nome, ma come mai? Forse xke avevo già scritto in un altro programma una funzione con lo stesso nome?

17-11-2014 16:56
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

lancia "man alloca", esiste già! :D

17-11-2014 17:09
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

ahahah va bene! grazie come sempre :)

17-11-2014 17:17
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 04:08.    Post New Thread    Post A Reply
  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.054 seconds (82.23% PHP - 17.77% MySQL) con 27 query.