1 / 27

4 aprile 2002

4 aprile 2002. Avvisi:. 1 o Esonero: mercoledi 17 aprile ore 11:30 – 14:00 consulta la pag. WEB alla voce “esoneri” si raccomanda la puntualita’!. Introduzione. Array Struttura di elementi di dati, tutti dello stesso tipo , correlati.

raven-glenn
Télécharger la présentation

4 aprile 2002

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4 aprile 2002 Avvisi: • 1o Esonero: mercoledi 17 aprile ore 11:30 – 14:00 consulta la pag. WEB alla voce “esoneri” • si raccomanda la puntualita’!

  2. Introduzione • Array • Struttura di elementi di dati, tutti dello stesso tipo, correlati. • Strutture di elementi di dati di tipi diversi correlati (struct ), in seguito. • Struttura dati statica : ha la stessa dimensione per tutta la durata del programma • Strutture dati dinamiche saranno considerate nella parte finale del corso

  3. -45 c[0] Nome dell’array (Tutti gli elementi hanno lo stesso nome, c) 6 c[1] 0 c[2] 72 c[3] 1543 c[4] -89 c[5] 0 c[6] 62 c[7] -3 c[8] 1 c[9] 6453 c[10] 78 c[11] Posizione dell’elemento nell’array Array • Gruppo di locazioni di memoria consecutive • Stesso nome e stesso tipo

  4. Dichiarazioni di Array • Per dichiarare un array si deve specificare • Nome • Tipo dell’ array • Numero di elementi • Formato: arrayType arrayName[numberOfElements]; int c[ 10 ]; float myArray[ 3284 ]; • Dichiarare molti array dello stesso tipo • Similmente a quando si dichiarano le variabili comuni int b[ 100 ], x[ 27 ];

  5. Array • Per riferirsi ad un elemento, specificare: • Nome dell’array • Posizione (indice) • Formato: nomearray [posizione] • Primo elemento alla posizione 0 • Array di nome c di n elementi : c[0], c[1]...c[n-1]

  6. Array • Gli elementi sono normali variabili c[0] = 3; printf( "%d", c[0] ); • Si possono effettuare operazioni negli indici. Se x = 3, c[5-2] == c[3] == c[x]

  7. Esempio di uso di un array • Inizializzatori int n[5] = {1, 2, 3, 4, 5 }; • Se non ci sono abbastanza inizializzatori, i piu’ a destra saranno 0 • Se ci sono troppi inizializzatori, errore di sintassi int n[5] = {0} • Tutti gli elementi sono inizializzati a 0 • Se la size e’ omessa, viene determinata dagli inizializzatori int n[] = { 1, 2, 3, 4, 5 }; • 5 inzializzatori, dunque l’array ha 5 elementi

  8. /* Inizializzazione di un array */ • #include <stdio.h> • main() • { • int n[10], i; • for (i = 0; i <= 9; i++) • n[i] = 0; • printf("%s%13s\n", "Element", "Value"); • for(i = 0; i <= 9; i++) /* print array */ • printf("%7d%13d\n", i, n[i]); • return 0; • }

  9. Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 Output

  10. /* Inizializzazione di un arraycon una dichiarazione */ • #include <stdio.h> • main() • { • int n[10] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37}; • int i; • printf("%s%13s\n", "Element", "Value"); • for(i = 0; i <= 9; i++) • printf("%7d%13d\n", i, n[i]); • return 0; • }

  11. Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37 Output

  12. /* Initializza gli elementi dell’ array s con i • numeri pari da 2 a 20 */ • #include <stdio.h> • #define SIZE 10 • main() • { • int s[SIZE], j; • for (j = 0; j <= SIZE - 1; j++) /* assegna i valori */ • s[j] = 2 + 2 * j; • printf("%s%13s\n", "Element", "Value"); • for (j = 0; j <= SIZE - 1; j++) /* stampa i valori */ • printf("%7d%13d\n", j, s[j]); • return 0; • }

  13. Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 Output

  14. /* Calcola la somma degli elementi di un array */ • #include <stdio.h> • #define SIZE 12 • main() • { • int a[SIZE] = {1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45}; • int i, total = 0; • for (i = 0; i <= SIZE - 1; i++) • total += a[i]; • printf("Total of array element values is %d\n", total); • return 0; • } Output Total of array element values is 383

  15. /*Tiro un dado 6000 volte*/ • /* versione con istruzione switch*/ • #include <stdio.h> • #include <stdlib.h> • main() • { • int face, roll, frequency1 = 0, frequency2 = 0, frequency3 = 0, • frequency4 = 0, frequency5 = 0, frequency6 = 0; • for (roll = 1; roll <= 6000; roll++) { • face = 1 + rand() % 6; • switch (face) { • case 1: • ++frequency1; • break; • case 2: • ++frequency2; • break; • case 3: • ++frequency3; • break;

  16. case 4: • ++frequency4; • break; • case 5: • ++frequency5; • break; • case 6: • ++frequency6; • break; • } • } • printf("%s%13s\n", "Face", "Frequency"); • printf(" 1%13d\n", frequency1); • printf(" 2%13d\n", frequency2); • printf(" 3%13d\n", frequency3); • printf(" 4%13d\n", frequency4); • printf(" 5%13d\n", frequency5); • printf(" 6%13d\n", frequency6); • return 0; • }

  17. /* Tiro un dado 6000 volte – versione con array */ • #include <stdio.h> • #include <stdlib.h> • #include <time.h> • #define SIZE 7 • main() • { • int face, roll, frequency[SIZE] = {0}; • srand(time(NULL)); • for (roll = 1; roll <= 6000; roll++) { • face = rand() % 6 + 1; • ++frequency[face]; /* sostituisce 20 linee istruzione switch */ • } • printf("%s%17s\n", "Face", "Frequency"); • for (face = 1; face <= SIZE - 1; face++) • printf("%4d%17d\n", face, frequency[face]); • return 0; • }

  18. Face Frequency 1 1006 2 1011 3 983 4 978 5 988 6 1034 Output Face Frequency 1 1008 2 957 3 957 4 1040 5 1004 6 1034 Face Frequency 1 962 2 1028 3 1005 4 986 5 1018 6 1001

  19. 1 /* Fig. 6.8: fig06_08.c 2 Programma per stampare gli istogrammi */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 int main() 7 { 8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 9 int i, j; 10 11 printf( "%s%13s%17s\n","Element","Value“,"Histogram"); 12 13 for ( i = 0; i <= SIZE - 1; i++ ) { 14 printf( "%7d%13d ", i, n[ i ]) ; 15 16 for ( j = 1; j <= n[ i ]; j++ ) /* print una barra */ 17 printf( "%c", '*' ); 18 19 printf( "\n" ); 20 } 21 22 return 0; 23 }

  20. Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 * Output

  21. Array di caratteri • La stringa "hello" e’ di fatto un array static di caratteri • Gli array di caratteri si possono inizializzare usando delle stringhe char string1[] = "first"; • La stringa termina col carattere null '\0' • string1 ha di fatto 6 elementi char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

  22. Array di caratteri • Si puo’ accedere ai caratteri singoli della stringa • string1[ 3 ] e’ il carattere 's' • Il nome di un array e’ l’ indirizzo dell’array, dunque non si deve mettere & in scanf scanf( "%s", string2 ) ; • Legge i caratteri finche’ si incontra uno spazio • Si puo’ scrivere oltre la fine dell’array, attenzione!

  23. 1 /* Fig. 6.10: fig06_10.c 2 Trattare gli array di caratteri come stringhe */ 3 #include <stdio.h> 4 5 int main() 6 { 7 char string1[ 20 ], string2[] = "string literal"; 8 int i; 9 10 printf(" Enter a string: "); 11 scanf( "%s", string1 ); 12 printf( "string1 is: %s\nstring2: is %s\n" 13 "string1 with spaces between characters is:\n", 14 string1, string2 ); 15 16 for ( i = 0; string1[ i ] != '\0'; i++ ) 17 printf( "%c ", string1[ i ] ); 18 19 printf( "\n" ); 20 return 0; 21 }

  24. Output Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o

  25. /* Gliarrays statici sono inizializzati a zero */ • #include <stdio.h> • void staticArrayInit(void); • void automaticArrayInit(void); • main() • { • printf("First call to each function:\n"); • staticArrayInit(); • automaticArrayInit(); • printf("\n\nSecond call to each function:\n"); • staticArrayInit(); • automaticArrayInit(); • return 0; • } • /* funzione per dimostrare l’uso di un arraylocale statico */ • void staticArrayInit(void) • { • static int a[3]; • int i;

  26. printf("\nValues on entering staticArrayInit:\n"); • for (i = 0; i <= 2; i++) • printf("array1[%d] = %d ", i, a[i]); • printf("\nValues on exiting staticArrayInit:\n"); • for (i = 0; i <= 2; i++) • printf("array1[%d] = %d ", i, a[i] += 5); • } • /* funzione per dimostrare l’uso di un arraylocale automatico */ • void automaticArrayInit(void) • { • int a[3] = {1, 2, 3}; • int i; • printf("\n\nValues on entering automaticArrayInit:\n"); • for (i = 0; i <= 2; i++) • printf("array1[%d] = %d ", i, a[i]); • printf("\nValues on exiting automaticArrayInit:\n"); • for (i = 0; i <= 2; i++) • printf("array1[%d] = %d ", i, a[i] += 5); • }

  27. First call to each function: Values on entering staticArrayInit: array1[0] = 0 array1[1] = 0 array1[2] = 0 Values on exiting staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on entering automaticArrayInit: array1[0] = 1 array1[1] = 2 array1[2] = 3 Values on exiting automaticArrayInit: array1[0] = 6 array1[1] = 7 array1[2] = 8 Second call to each function: Values on entering staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on exiting staticArrayInit: array1[0] = 10 array1[1] = 10 array1[2] = 10 Values on entering automaticArrayInit: array1[0] = 1 array1[1] = 2 array1[2] = 3 Values on exiting automaticArrayInit: array1[0] = 6 array1[1] = 7 array1[2] = 8 Output

More Related