Recap sul C
description
Transcript of Recap sul C
![Page 1: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/1.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Recap sul CRecap sul C
Marco D. Santambrogio – [email protected]. aggiornata al 11 Aprile 2013
![Page 2: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/2.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
FeedbackFeedback
• NUOVO, per le exe:http://tinyurl.com/IEIMExe2013
• Per i lab:http://tinyurl.com/Feedback-LabIEIM2013
• Per le lezioni:http://tinyurl.com/Feedback-IEIM2013
2
![Page 3: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/3.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
WATWAT
• 8 Maggio, sospesione lezione! No homework!!
3
WAT
![Page 4: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/4.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
WATWAT
• Da feedback l'esercitatore era troppo veloce.... ahahah
4
WAT
![Page 5: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/5.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
ObiettiviObiettivi
• Un ripasso generale sul C
• In particolare Array multi-dimensionali Dati strutturati e passaggio a funzioni
5
![Page 6: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/6.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
ProblemaProblema
• Si scriva in C un programma che, recuperato un cubo di caratteri, dice quante ‘a’ vi sono contenute. La dimensione del cubo è: 2x3x4
6
![Page 7: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/7.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Sotto-problemiSotto-problemi
• Si scriva in C un programma che, recuperato un cubo di caratteri, dice quante ‘a’ vi sono contenute. La dimensione del cubo è: 2x3x4
• Sotto-problemi P0: cubo di caratteri P1: Popolare il cubo di caratteri P2: Contare le ‘a’
7
![Page 8: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/8.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Cubo di caratteriCubo di caratteri
• La dimensione del cubo è: 2x3x4
8
char data[dx][dy][dz];
#define dx 3
#define dy 2
#define dz 4
![Page 9: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/9.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Array in memoriaArray in memoria
9
char data[dx]; array di dx char (dx:3)
data[0]data[0]
data[1]data[1]
data[2]data[2]
![Page 10: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/10.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Matrice (array di array) in Matrice (array di array) in memoriamemoria
10
char data[dx][dy];char data[dx]; array di dx char (dx:3)
array di dx array (dx:3)array di dy char (dy:2)
[0][0]
[1][1][0][0]
[0][0]
[1][1][1][1]
[0][0]
[1][1][2][2]
data[0][0]data[0][0]
data[0][1]data[0][1]
data[1][0]data[1][0]
data[1][1]data[1][1]
data[2][0]data[2][0]
data[2][1]data[2][1]
![Page 11: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/11.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Un array Un array 3D3D
11
char data[dx][dy][dz]
char data[dx][dy];char data[dx]; array di dx char
array di dx array di dy char
array di dx array (dx:3)
array di dy array (dy:2)
array di dz char (dz:4)
![Page 12: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/12.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
12
char data[dx][dy][dz]
data[0][0][0]data[0][0][0]
In memoria, macchina a 32bit, In memoria, macchina a 32bit, e.g. x86e.g. x86
dx:3, dy:2, dz:4
data[0][0][1]data[0][0][1]
data[0][0][2]data[0][0][2]
data[0][0][3]data[0][0][3]
data[0][1][0]data[0][1][0]
data[0][1][1]data[0][1][1]
data[0][1][2]data[0][1][2]
data[0][1][3]data[0][1][3]
data[1][0][0]data[1][0][0]
data[1][0][1]data[1][0][1]
data[1][0][2]data[1][0][2]
data[1][0][3]data[1][0][3]
data[1][1][0]data[1][1][0]
data[1][1][1]data[1][1][1]
data[1][1][2]data[1][1][2]
data[1][1][3]data[1][1][3]
data[2][0][0]data[2][0][0]
[0][0]
[0][0]
[0][0]
[1][1]
![Page 13: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/13.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Torniamo al problemaTorniamo al problema
• Si scriva in C un programma che, recuperato un cubo di caratteri, dice quante ‘a’ vi sono contenute. La dimensione del cubo è: 2x3x4
• Sotto-problemi P0: cubo di caratteri P1: Popolare il cubo di caratteri P2: Contare le ‘a’
13
![Page 14: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/14.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P1: popolare il cubo di P1: popolare il cubo di charchar
• Serve una funzione che Recuperato il cubo
Permette l’inserimento dei caratteri
14
void popola(char *p, int x, int y, int z);
![Page 15: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/15.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P2: contare le ‘a’P2: contare le ‘a’
• Serve una funzione che Recuperato il cubo
Scorre gli elementi per cercare le ‘a’
15
int foo2(char p[][dy][dz], char x);
Attenzio
ne a cha
r x
![Page 16: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/16.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
ProblemaProblema
• Si scriva in C un programma che, recuperati i cognomi di 5 studenti, li ordina alfabeticamente
16
![Page 17: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/17.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Sotto-problemiSotto-problemi
• Si scriva in C un programma che, recuperati i cognomi di 5 studenti, li ordina alfabeticamente
• Sotto-problemi P0: rappresentare i cognomi P1: recuperare i cognomi P2: ordinare i cognomi alfabeticamente
17
![Page 18: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/18.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P0: rappresentare i P0: rappresentare i cognomicognomi
• Vi sono diversi modi…• Noi, per esercitarci,
creiamo una struttura e definiamo un nuovo tipo di dato
18
![Page 19: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/19.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
datidati
• Dopo aver definito studente
• Dobbiamo creare 5 studenti
19
studente studenti[5];
![Page 20: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/20.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P1: recuperare i cognomiP1: recuperare i cognomi
20
• Studenti è un array… si passa per indirizzo
• studente è “strutturato” come accedo ai campi attraverso un puntatore? Con una “freccia”
void ins_alunno(studente *p, int dimensione);
p->cognome;
![Page 21: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/21.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P1: recuperare i cognomiP1: recuperare i cognomi
21
![Page 22: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/22.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P2: ordinare i cognomiP2: ordinare i cognomi
• Come faccio ad ordinare i cognomi? Vi è differenza nell’ordinare studente rispetto ad ordinare int?
• Ma quindi il problema è… Come ordino N interi???
22
![Page 23: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/23.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Ordiniamo N interiOrdiniamo N interi
• Problema Dati: 4, 3, 1, 2 Voglio ottenere: 1, 2, 3, 4
• Iniziamo ad ipotizzare una soluzione Confronto gli elementi a due a due e se non sono nell’ordine corretto, inverto i valori
23
![Page 24: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/24.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Ordino 4, 3, 1, 2Ordino 4, 3, 1, 2
• Confronto gli elementi a due a due e se non sono nell’ordine corretto, inverto i valori
Dati: 4, 3, 1, 24 > 3?Si, inverto: 3, 4, 1, 2
4 > 1?Si, inverto: 3, 1, 4, 2
4 > 2?Si, inverto: 3, 1, 2, 4
24
![Page 25: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/25.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Da 4, 3, 1, 2 a 3, 1, 2, Da 4, 3, 1, 2 a 3, 1, 2, 4…4…
• Problema 3, 1, 2, 4 è ordinato? Basta confrontare a due a due gli elementi?
25
NO!!!Devo confrontare a due a due
TUTTI (quasi vero) gli elementi
![Page 26: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/26.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Facciamolo su tuttiFacciamolo su tutti
Dato vet[4]={4, 3, 1, 2}
for(j=0;j<dimensione-1;j++)è vero che vet[j]>vet[j+1]?
ci porta da 4, 3, 1, 2a 3, 1, 2, 4
Ma questo vogliamo farlo su tutti gli elementi for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
26
![Page 27: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/27.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
E quindi… i=0E quindi… i=0
Dato vet[4]={4, 3, 1, 2}for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
è vero che vet[j]>vet[j+1]?
i=0, vario jci porta da 4, 3, 1, 2 a 3, 1, 2, 4
j=0: 4 > 3?Si, inverto: 3, 4, 1, 2
j=1: 4 > 1?Si, inverto: 3, 1, 4, 2
j=2: 4 > 2?Si, inverto: 3, 1, 2, 4
27
![Page 28: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/28.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
E quindi… i=1E quindi… i=1
Dato vet[4]={4, 3, 1, 2}for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
è vero che vet[j]>vet[j+1]?i=0, vario j
ci porta da 4, 3, 1, 2 a 3, 1, 2, 4i=1, vario j
ci porta da 3, 1, 2, 4 a 1, 2, 3, 4
j=0: 3 > 1?Si, inverto: 1, 3, 2, 4
j=1: 3 > 2?Si, inverto: 1, 2, 3, 4
j=2: 3 > 4?No, non faccio nulla: 1, 2, 3, 4
28
![Page 29: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/29.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
E quindi… i=2E quindi… i=2
Dato vet[4]={4, 3, 1, 2}for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
è vero che vet[j]>vet[j+1]?i=0, vario j
ci porta da 4, 3, 1, 2 a 3, 1, 2, 4i=1, vario j
ci porta da 3, 1, 2, 4 a 1, 2, 3, 4i=2, vario j
ci porta da 1, 2, 3, 4 a 1, 2, 3, 4
j=0: 1 > 2?No, non faccio nulla: 1, 2, 3, 4
j=1: 2 > 3?No, non faccio nulla: 1, 2, 3, 4
j=2: 3 > 4?No, non faccio nulla: 1, 2, 3, 4
29
![Page 30: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/30.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
E quindi… i=3E quindi… i=3
Dato vet[4]={4, 3, 1, 2}for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
è vero che vet[j]>vet[j+1]?i=0, vario j
ci porta da 4, 3, 1, 2 a 3, 1, 2, 4i=1, vario j
ci porta da 3, 1, 2, 4 a 1, 2, 3, 4i=2, vario j
ci porta da 1, 2, 3, 4 a 1, 2, 3, 4i=3, vario j
ci porta da 1, 2, 3, 4 a 1, 2, 3, 4
j=0: 1 > 2?No, non faccio nulla: 1, 2, 3, 4
j=1: 2 > 3?No, non faccio nulla: 1, 2, 3, 4
j=2: 3 > 4?No, non faccio nulla: 1, 2, 3, 4
30
![Page 31: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/31.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Ordinamento, qualche Ordinamento, qualche osservazioneosservazione
Si può migliorare?
for(i=0;i<dimensione;i++) for(j=0;j<dimensione-1;j++)
è vero che vet[j]>vet[j+1]?
•Nel for innestato Mi serve davvero arrivare a dimensione-1?
•Nel for esterno, mi serve veramente arrivare a dimensione?
31
![Page 32: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/32.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
P2: ordinare i cognomiP2: ordinare i cognomi
Come faccio ad ordinare i cognomi?
32
![Page 33: Recap sul C](https://reader035.fdocument.pub/reader035/viewer/2022062217/5681409b550346895dac3ea4/html5/thumbnails/33.jpg)
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Fonti per lo studio + Fonti per lo studio + CreditsCredits• Fonti per lo studio
Tutte le slide precedenti del corso di IEIM 2012/2013
33