it:informatica:linguaggio_c:vettori_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/vettori_1?rev=1551188125&do=diff
Differences
This shows you the differences between two versions of the page.
— | it:informatica:linguaggio_c:vettori_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Vettori (Esempio 1) ====== | ||
+ | **Concetti: | ||
+ | Vettori (array), inizializzazione ed utilizzo | ||
+ | **Testo: | ||
+ | Realizzare un programma in linguaggio C che: | ||
+ | * dopo aver dichiarato ed inizializzato un array di 10 elementi | ||
+ | * trovi e stampi il primo numero pari e la sua posizione all' | ||
+ | |||
+ | **Soluzione: | ||
+ | <file C vettori_1.c> | ||
+ | /* Stampare il primo numero pari e la sua posizione, scandendo un vettore di 10 elementi */ | ||
+ | |||
+ | #include < | ||
+ | #define SIZE 10 | ||
+ | |||
+ | int main(){ | ||
+ | |||
+ | int vet[SIZE] = {1, 5, 3, 7, 10, 1, 2, 3, 4, 5}; | ||
+ | int i; | ||
+ | int found = 0; | ||
+ | |||
+ | /* Prima soluzione */ | ||
+ | for(i=0; i<SIZE && !found; i++){ | ||
+ | if ( vet[i]%2 == 0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* Seconda soluzione */ | ||
+ | i = 0; found = 0; | ||
+ | while ( i<SIZE && !found ){ | ||
+ | if ( vet[i]%2==0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | i = i + 1; | ||
+ | } | ||
+ | | ||
+ | if (found){ | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Commenti: | ||
+ | Il programma: | ||
+ | * dopo aver inizializzato l' | ||
+ | <code c> | ||
+ | int vet[SIZE] = {1, 5, 3, 7, 10, 1, 2, 3, 4, 5}; | ||
+ | </ | ||
+ | * scandisce l' | ||
+ | <code c> | ||
+ | for(i=0; i<SIZE && !found; i++){ | ||
+ | if ( vet[i]%2 == 0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | * lo stesso esercizio è stato risolto in modo equivalente utilizzando il costrutto //while//. | ||
+ | <code c> | ||
+ | /* Seconda soluzione */ | ||
+ | i = 0; found = 0; | ||
+ | while ( i<SIZE && !found ){ | ||
+ | if ( vet[i]%2==0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | i = i + 1; | ||
+ | } | ||
+ | </ | ||
+ | * i risultati vengono stampati alla fine del programma. Si noti che all' | ||
+ | <code c> | ||
+ | if (found){ | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/vettori_1?rev=1551188125&do=diff