1 / 17

Primo esercizio

Primo esercizio. Scrivere un programma che legge da input le misure dei lati di tre diversi parallelepipedi. vol1 := largh1 * prof1 * alt1 vol2 := largh2 * prof2 * alt2 vol3 := largh3 * prof3 * alt3. Simulazione.

sven
Télécharger la présentation

Primo esercizio

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. Primo esercizio Scrivere un programma che legge da input le misure dei lati di tre diversi parallelepipedi. vol1 := largh1 * prof1 * alt1 vol2 := largh2 * prof2 * alt2 vol3 := largh3 * prof3 * alt3

  2. Simulazione Si simuli “manualmente” l'esecuzione del seguente programma al fine di dedurre l'output prodotto. VAR x, y: integer; PROCEDURE piu5 (a: integer; VAR b: integer); BEGIN writeln ('All''inizio dell''esecuzione di piu5: ',a:4,b:4); a := a + 5; b := b + 5; writeln ('Alla fine dell''esecuzione di piu5: ',a:4,b:4) END; BEGIN x := 10; y := 20; piu5(x,y); writeln ('Al rientro, dopo l''esecuzione di piu5: ',x:4, y:4); readln; END.

  3. Debug • Procedura simulazione • Procedure annidate • Programma EC • Procedura ricorsiva (fattoriale)

  4. program EC; • {$APPTYPE CONSOLE} • uses • SysUtils; • var b, ris: integer; • Function funz(Var a: integer): integer; • begin • a:=2*a; • funz:=a; • end; • begin • b:=1; • ris:=2* funz(b); • writeln('Primo risultato: ', ris); • writeln; • b:=1; • ris:= funz(b) + funz(b); • writeln ('Secondo risultato: ', ris); • readln; • end.

  5. Primo risultato: ris:=2*funz(B) A:=2*A; funz:=A; La funzione funz(b) = 2 Risultato = 2 * 2 = 4

  6. Secondo risultato: ris:=funz(B)+funz(B) A:=2*A; funz:=A; La funzione funz(B) = 2 Risultato = 2 + funz(B) La funzione funz(B) = 4 Risultato = 2 + 4 = 6

  7. Fattoriale function fatt (n: integer): integer; begin if n=0 then fatt:=1 else fatt:= n* fatt (n-1); end;

  8. Fattoriale di n=4 Risultato: 24

  9. Prova del 21 settembre 2004 Si scriva una funzione che, presa in ingresso una matrice A, restituisca il numero delle colonne i cui elementi sono tutti dispari.

  10. Scacchiera (7 Febbraio 2005 ) Considerare una matrice quadrata di ordine n come una scacchiera. Scrivere una procedura (o funzione) che calcoli la somma degli elementi in posizione “nera”, la somma degli elementi in posizione “bianca”, e dica a quale colore appartiene la maggiore.

  11. Esercizio Trovare il numero di occorrenze di una parola in una frase, senza usare POS.

  12. Sostituire While con Repeat VAR a,b: integer; BEGIN write ('Inserisci il primo numero: '); readln(a); write ('Inserisci il secondo numero: '); readln(b); WHILE (a<=b) AND (a<100) DO BEGIN writeln(a); a:=a+1 END; END

  13. Con repeat VAR a,b: integer; BEGIN write ('Inserisci il primo numero: '); readln(a); write ('Inserisci il secondo numero: '); readln(b); if (a<= b) and (a<100) then repeat writeln(a); a:=a+1; until (a>b) or (a>=100); writeln ('Fine!'); readln; END.

  14. Quali While si possono sostituire con For Per ognuno dei seguenti frammenti di programma dire se è possibile sostituire i cicli WHILE con cicli FOR motivando la risposta. In caso affermativo, riscrivere il codice dopo la sostituzione. • i := k; WHILE i <n DO BEGIN writeln(i); i := i + 1; END • i := k; WHILE i < n DO BEGIN writeln(i); i := i + 3; END • i := k; WHILE i < n DO BEGIN writeln(i); i := i + 1; readln(n); END

  15. Da Repeat a While VAR x,y: integer; BEGIN write ('Inserisci il primo numero: '); readln(x); write ('Inserisci il secondo numero: '); readln(y); REPEAT x := x - 1; y := y + 2 UNTIL (x <= 0) AND (y > x); END;

  16. Con While VAR x,y: integer; BEGIN write ('Inserisci il primo numero: '); readln(x); write ('Inserisci il secondo numero: '); readln(y); x := x - 1; y := y + 2; while (x > 0) or (y <= x) do begin x := x - 1; y := y + 2; end; END

  17. Fine

More Related