123 10 for program upologismosathroismatosinput output var i

  • Slides: 49
Download presentation

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή for. program upologismos_athroismatos(input, output); var

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή for. program upologismos_athroismatos(input, output); var i, N, athroisma: integer; begin N: =10; athroisma: =0; i: =1; for i: = 1 to N do athroisma: = athroisma+i; writeln('athroisma=1+2+3+4+5+ 6+7+8+9+10=', athroisma); readln; end.

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή while. program upologismos_athroismatos(input, output); var

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή while. program upologismos_athroismatos(input, output); var i, N, athroisma: integer; begin N: = 10; athroisma: =0; i: =1; while i<=N do begin athroisma: = athroisma +i; i: = i+1; end; writeln('athroisma=1+2+3+4+5+6+7+ 8+9+10=', athroisma); readln; end.

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή repeat. program upologismos_athroismatos(input, output); var

Υπολογίστε το άθροισμα 1+2+3+. . . +10 χρησιμοποιώντας εντολή repeat. program upologismos_athroismatos(input, output); var i, N, athroisma: integer; begin N: =10; athroisma: =0; i: =1; repeat athroisma: = athroisma+i; i: = i+1 until i>N; writeln('athroisma=1+2+3+4+5+ 6+7+8+9+10=', athroisma); readln; end.

Τo πρόγραμμα διαβάζει τον αριθμό Ν, χρησιμοποιεί εντολή repeat και υπολογίζει το άθροισμα 1+2+3+.

Τo πρόγραμμα διαβάζει τον αριθμό Ν, χρησιμοποιεί εντολή repeat και υπολογίζει το άθροισμα 1+2+3+. . . +Ν. • • • • • program upologismos_athroismatos(input, Αποτελέσματα εκτέλεσης του output); προγράμματος var i, N, athroisma: integer; dwse plhthos diadoxikwn akeraiwn pou begin tha prostheseis writeln('dwse plhthos diadoxikwn akeraiwn pou 5 tha prostheseis'); 15 (επειδή 1+2+3+4+5=15) readln(N); if (N<1) or (N>10) then writeln('dwse swsta to plhthos') else begin athroisma: =0; i: =1; repeat athroisma: = athroisma+i; i: = i+1; until i>N; writeln('athroisma =', athroisma); end; readln; End.

Ακολουθούν παραδείγματα προγραμμάτων που χρησιμοποιούν while, repeat. • • • • • program upologismos_athroismatos(input,

Ακολουθούν παραδείγματα προγραμμάτων που χρησιμοποιούν while, repeat. • • • • • program upologismos_athroismatos(input, output); var arithmos, athroisma: integer; begin writeln('to programma tha diavazei kai tha prosthetei arithmous'); writeln('dwse arithmo <10 kai >0'); writeln('gia na stamathsei dwse arithmo pou den anhkei sto diasthma [1, 10)'); readln(arithmos); while (arithmos>=1) and (arithmos<10) do begin athroisma : = athroisma+arithmos; writeln('dwse arithmo < 10 kai > 1'); readln(arithmos) end; writeln('athroisma =', athroisma); readln; end. • Το πρόγραμμα διαβάζει και προσθέτει αριθμούς μεγαλύτερους από 0 και μικρότερους από 10. Για να σταματήσει το πρόγραμμα πληκτρολογείς είτε αριθμό αρνητικό ή 0 ή αριθμό μεγαλύτερο του 9.

 • • • • program upologismos_athroismatos(input, output); var arithmos, athroisma: integer; begin writeln('to

• • • • program upologismos_athroismatos(input, output); var arithmos, athroisma: integer; begin writeln('to programma tha diavazei kai tha • Το πρόγραμμα διαβάζει και προσθέτει αριθμούς μεγαλύτερους από 0 και prosthetei arithmous'); μικρότερους από 10. Για να repeat σταματήσει το πρόγραμμα writeln('dwse arithmo < 10 kai > 1'); πληκτρολογείς είτε αριθμό αρνητικό ή athroisma : = athroisma+arithmos; 0 ή αριθμό μεγαλύτερο του 9. readln(arithmos); writeln('arithmos=', arithmos); until(arithmos<1)or(arithmos>=10); writeln('athroisma =', athroisma); readln; end.

 • • • program diavazw_zeugh(input, output); var i, arithmos 1, arithmos 2: integer;

• • • program diavazw_zeugh(input, output); var i, arithmos 1, arithmos 2: integer; begin for i: = 1 to 5 do begin writeln('Dwse 2 akeraious aritmous. '); readln(arithmos 1, arithmos 2); writeln('oi arithmoi pou edwses einai: ', arithmos 1, ' , ', arithmos 2); end; readln end.

 • • • • • program diavazw_zeugh(input, output); var arithmos 1, arithmos 2:

• • • • • program diavazw_zeugh(input, output); var arithmos 1, arithmos 2: integer; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden to programma tha stamathsei'); readln(arithmos 1, arithmos 2); while (arithmos 1 > 0) and (arithmos 2 > 0) do begin writeln('oi arithmoi pou edwses einai: ', arithmos 1, ' , ', arithmos 2); writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden to programma tha stamathsei'); readln(arithmos 1, arithmos 2); end; readln end.

 • • • • program diavazw_zeugh(input, output); var arithmos 1, arithmos 2: integer;

• • • • program diavazw_zeugh(input, output); var arithmos 1, arithmos 2: integer; begin repeat writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden to programma tha stamathsei'); readln(arithmos 1, arithmos 2); writeln('oi arithmoi pou edwses einai: ', arithmos 1, ' , ', arithmos 2) until (arithmos 1 = 0) or (arithmos 2 = 0); readln end.

Τα υποπρογράμματα χωρίζονται σε διαδικασίες (procedures) και συναρτήσεις (functions): • • function athroisma_duo_akeraiwn(a 1,

Τα υποπρογράμματα χωρίζονται σε διαδικασίες (procedures) και συναρτήσεις (functions): • • function athroisma_duo_akeraiwn(a 1, a 2: integer): integer; begin athroisma_akeraiwn: = a 1+a 2; end; • • • procedure hello; begin writeln('Eimaste sti diadikasia'); writeln('Hello world!'); end; • • procedure vres_min(a 1, a 2: integer; var elaxisto: integer); begin if a 1 > a 2 then elaxisto: =a 2 else elaxisto: =a 1; end; •

 • • • • program greetings(input, output); procedure hello; begin writeln('Eimaste sti diadikasia');

• • • • program greetings(input, output); procedure hello; begin writeln('Eimaste sti diadikasia'); writeln('Hello world!'); end; begin writeln('Eimaste sto kurio programma'); writeln('Kaloume th diadikasia gia na grapsoume to minima '); hello; writeln('Xanagurisame sto kurio programma'); readln; end. Τα αποτελέσματα εκτέλεσης του προγράμματος Eimaste sto kurio programma Kaloume th diadikasia gia na grapsoume to minima Eimaste sti diadikasia Hello world! Xanagurisame sto kurio programma

 • • • • • Οι μεταβλητές του κύριου προγράμματος • λέγονται καθολικές

• • • • • Οι μεταβλητές του κύριου προγράμματος • λέγονται καθολικές (global) επειδή μπορούμε να τις χρησιμοποιήσουμε και στο πρόγραμμα και • στα υποπρογράμματα. • Δηλαδή μία καθολική μεταβλητή μπορεί να την αλλάξει και το κύριο πρόγραμμα και τα • υποπρογράμματα. • Η i είναι καθολική μεταβλητή. • program greetings(input, output); • var i: integer; procedure hello; begin writeln('Eimaste sti diadikasia'); writeln('i=', i); writeln('Hello world!'); end; begin writeln('Eimaste sto kurio programma'); i: = 10; writeln('Kaloume th diadikasia gia na grapsoume i=10 kai Hello world!'); hello; writeln('Xanagurisame sto kurio programma'); readln; end. Τα αποτελέσματα εκτέλεσης του προγράμματος Eimaste sto kurio programma Kaloume th diadikasia gia na grapsoume i=10 kai Hello world! Eimaste sti diadikasia i=10 Hello world! Xanagurisame sto kurio programma

 • Τι κάνει το παρακάτω πρόγραμμα; • • • • • program kurio_programma(input,

• Τι κάνει το παρακάτω πρόγραμμα; • • • • • program kurio_programma(input, output); var x, y: integer; procedure diadikasia_ipologismwn; begin writeln('Eimaste sti diadikasia'); x: =y+10; y: =x+y; writeln('x=', x, ' y=', y); end; begin writeln('Eimaste sto kurio programma'); x: = 10; y: = 20; writeln('x=', x, ' y=', y); writeln('Kaloume th diadikasia '); diadikasia_ipologismwn; writeln('Xanagurisame sto kurio programma'); readln; end. • • Τα αποτελέσματα εκτέλεσης του προγράμματος Eimaste sto kurio programma x= 10 y= 20 Kaloume th diadikasia Eimaste sti diadikasia x= 30 y= 50 Xanagurisame sto kurio programma

 • • • • • program vathmologia(input, output); var i, athroisma, vathmos: integer;

• • • • • program vathmologia(input, output); var i, athroisma, vathmos: integer; mesos: real; procedure mesos_oros; begin athroisma: =0; writeln('Dwse vathmous mathimatwn'); for i: = 1 to 5 do begin readln(vathmos); athroisma : = athroisma+vathmos; end; mesos: = athroisma/5; end; begin mesos_oros; writeln('Mesos oros = ', mesos); readln; end. • • Τα αποτελέσματα εκτέλεσης του προγράμματος Dwse vathmous mathimatwn 3 7 10 5 5 Mesos oros = 6. 000. . . 0Ε+000 (δηλαδή = 6)

 • • • • • • program mesos_oros_vathmologias(input, output); var am: integer; •

• • • • • • program mesos_oros_vathmologias(input, output); var am: integer; • mesos: real; procedure mesos_oros; var i, athroisma, vathmos: integer; begin athroisma: =0; writeln('Dwse vathmous mathimatwn'); for i: = 1 to 5 do begin readln(vathmos); athroisma : = athroisma+vathmos; end; mesos: = athroisma/5; end; begin writeln('Grapse AM spoudasth. '); readln(am); writeln('AM=', am); mesos_oros; writeln('Mesos oros = ', mesos); readln; end. Ο σπουδαστής γράφει τον αριθμό μητρώου του (ΑΜ) και μετά βαθμούς πέντε μαθημάτων του. Το πρόγραμμα υπολογίζει το μέσο όρο των μαθημάτων.

program mesos_oros_vathmologia(input, output); • var am: integer; mesos: real; procedure mesos_oros; var i, athroisma,

program mesos_oros_vathmologia(input, output); • var am: integer; mesos: real; procedure mesos_oros; var i, athroisma, vathmos: integer; begin athroisma: =0; writeln('Dwse vathmous mathimatwn'); for i: = 1 to 5 do begin readln(vathmos); athroisma : = athroisma+vathmos; end; mesos: = athroisma/5; end; begin writeln('To programma tha stamathsei an dwseis AM=99999. '); writeln('Grapse AM spoudasth. '); readln(am); while am <> 99999 do begin writeln('AM=', am); mesos_oros; writeln('Mesos oros = ', mesos); writeln('Grapse AM spoudasth. '); readln(am); end; readln end. Το πρόγραμμα διαβάζει κάθε φορά αριθμό μητρώο (ΑΜ) σπουδαστή και μετά βαθμούς πέντε μαθημάτων του. Το πρόγραμμα υπολογίζει το μέσο όρο των μαθημάτων. Μετά διαβάζει το επόμενο ΑΜ και συνεχίζει μέχρι να δώσουμε σαν αριθμό μητρώου 99999.

 • • • • • • • • program mesos_oros_vathmologia(input, output); var am:

• • • • • • • • program mesos_oros_vathmologia(input, output); var am: integer; mesos: real; procedure mesos_oros(kwdikos_spoudasth: integer); var i, athroisma, vathmos: integer; begin athroisma: =0; writeln('Dwse vathmous mathimatwn'); for i: = 1 to 5 do begin readln(vathmos); athroisma : = athroisma+vathmos; end; mesos: = athroisma/5; writeln('AM=', kwdikos_spoudasth); end; begin writeln('To programma tha stamathsei an dwseis AM=99999. '); writeln('Grapse AM spoudasth. '); readln(am); while am <> 99999 do begin mesos_oros(am); writeln('Mesos oros = ', mesos); writeln('Grapse AM spoudasth. '); readln(am); end; readln end.

 • • • • • program mesos_oros_vathmologia(input, output); var arithmos 1, arithmos 2,

• • • • • program mesos_oros_vathmologia(input, output); var arithmos 1, arithmos 2, phliko, ypoloipo: integer; procedure ypologismos(arit 1, arit 2: integer); • begin phliko: = arit 1 div arit 2; ypoloipo: = arit 1 mod arit 2; end; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden i atnhtikos to programma tha stamathsei'); readln(arithmos 1, arithmos 2); while (arithmos 1 > 0) and (arithmos 2 > 0)do begin ypologismos(arithmos 1, arithmos 2); writeln('ypoloipo = ', ypoloipo); writeln('phliko = ', phliko); readln(arithmos 1, arithmos 2); end; readln end. Να γράψετε ένα πρόγραμμα που θα διαβάζει ζευγάρια θετικών ακέραιων αριθμών. Το πρόγραμμα θα καλεί μία διαδικασία (procedure) που θα υπολογίζει το υπόλοιπο και το πηλίκο της διαίρεσης των 2 αριθμών. Το πρόγραμμα θα σταματά αν δώσουμε μηδέν ή αρνητικό αριθμό.

 • • • Υπολογισμός αθροίσματος ζευγαριών αριθμών program ypologismos_athroismatos_2_thetikwn(input, output); var arit 1,

• • • Υπολογισμός αθροίσματος ζευγαριών αριθμών program ypologismos_athroismatos_2_thetikwn(input, output); var arit 1, arit 2, athroisma: integer; function athroisma_akeraiwn(a 1, a 2: integer): integer; begin athroisma_akeraiwn: = a 1+a 2; end; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden i atnhtikos to programma tha stamathsei'); • • • readln(arit 1, arit 2); while (arit 1 > 0) and (arit 2 > 0)do begin athroisma: = athroisma_akeraiwn(arit 1, arit 2); writeln('athroisma = ', athroisma); readln(arit 1, arit 2); end; readln end.

 • • • Υπολογισμός μέσου όρου ζευγαριών αριθμών program ypologismos_mesou_orou_2_thetikwn(input, output); var arit

• • • Υπολογισμός μέσου όρου ζευγαριών αριθμών program ypologismos_mesou_orou_2_thetikwn(input, output); var arit 1, arit 2: integer; mesos: real; function mesos_oros_akeraiwn(a 1, a 2: integer): real; begin mesos_oros_akeraiwn: = a 1+a 2/2; end; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden i atnhtikos to programma tha stamathsei'); • • • readln(arit 1, arit 2); while (arit 1 > 0) and (arit 2 > 0)do begin mesos: = mesos_oros_akeraiwn(arit 1, arit 2); writeln('mesos oros = ', mesos); readln(arit 1, arit 2); end; readln end. • Το ίδιο πρόγραμμα μπορεί να καλέσει, μία ή πολλές φορές, μια ή πολλές διαδικασίες ή συναρτήσεις.

 • • • • • programma_me_function_kai_procedure(input, output); var arit 1, arit 2, min,

• • • • • programma_me_function_kai_procedure(input, output); var arit 1, arit 2, min, max: integer; • function vres_max(a 1, a 2: integer): integer; begin if a 1 > a 2 then vres_max: =a 1 else vres_max: =a 2; end; procedure vres_min(a 1, a 2: integer; var elaxisto: integer); begin if a 1 > a 2 then elaxisto: =a 2 else elaxisto: =a 1; end; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden i atnhtikos to programma tha stamathsei'); • • • readln(arit 1, arit 2); min: =0; while (arit 1 > 0) and (arit 2 > 0)do begin max: = vres_max(arit 1, arit 2); vres_min(arit 1, arit 2, min); writeln('elaxisto = ', min, ' megisto = ', max); readln(arit 1, arit 2); end; readln end. Να γράψετε ένα πρόγραμμα που θα διαβάζει ζευγάρια θετικών ακεραίων. Στη συνέχεια θα χρησιμοποιεί μία συνάρτηση για να βρεί τον μεγαλύτερο από αυτούς. Μετά θα χρησιμοποιεί μία διαδικασία για να βρεί το μικρότερο από αυτούς.

 • programma_me_function_kai_procedure(input, output); • var arit 1, arit 2, min, max: integer; •

• programma_me_function_kai_procedure(input, output); • var arit 1, arit 2, min, max: integer; • function vres_max(a 1, a 2: integer): integer; • begin • if a 1 > a 2 then • vres_max: =a 1 • else • vres_max: =a 2; • end; procedure vres_min(a 1, a 2: integer; var elaxisto: integer); • • begin • if a 1 > a 2 then • elaxisto: =a 2 • • • • else • elaxisto: =a 1; • end; • procedure vres_min_max(n 1, n 2, megisto, elax: integer); • begin • writeln('vriskesai stin procedure vres_min_max'); • megisto: = vres_max(n 1, n 2); • vres_min(n 1, n 2, elax); writeln('kalesses function & procedure kai sou gyrisan times'); • writeln('megisto = ', megisto, ' elaxisto = ', elax); • end; begin writeln('Dwse 2 akeraious aritmous. '); writeln('An kapoios einai mhden i atnhtikos to programma tha stamathsei'); readln(arit 1, arit 2); min: =0; max: =0; while (arit 1 > 0) and (arit 2 > 0)do begin vres_min_max(arit 1, arit 2, max, min); readln(arit 1, arit 2); end; readln end.

 • • • • Αναδρομικές διαδικασίες και συναρτήσεις program paradeigma_anadromis(input, output); var a:

• • • • Αναδρομικές διαδικασίες και συναρτήσεις program paradeigma_anadromis(input, output); var a: integer; procedure antistrofi_arithmou(n: integer); begin write(n: 3); If n < a then antistrofi_arithmou (n+1); write(n: 3); end; begin repeat writeln('Dwse thetiko akeraio'); writeln('An dwseis mhden i atnhtiko to programma tha stamathsei'); • readln(a); • until a>=1; • antistrofi_arithmou(1); • readln • end. Dwse thetiko akeraio An dwseis mhden i atnhtiko to programma tha stamathsei 7 1 2 3 4 5 6 7 7 6 5 4 3 2 1

 • • • • Υπολογισμός Ν!=1*2*3*. . . Ν program ypologismos_paragontikou(input, output); var

• • • • Υπολογισμός Ν!=1*2*3*. . . Ν program ypologismos_paragontikou(input, output); var n, p: integer; ch: char; function paragontiko(k: integer): integer; begin If k = 0 then paragontiko: = 1 else paragontiko: = k*paragontiko(k-1); end; begin repeat writeln('Dwse mh arnhtiko akeraio mikrotero h iso tou 10'); • • • readln(n); until (n >= 0) and (n <= 10); p: = paragontiko(n); writeln(n, '!=', p); writeln('Thes na synexiseis; Y/N'); readln(ch); until (ch='N') or (ch='n'); readln end.

 • • • • • • Παραδείγματα χρήσης Πινάκων: Να γραφτεί πρόγραμμα που

• • • • • • Παραδείγματα χρήσης Πινάκων: Να γραφτεί πρόγραμμα που θα διαβάζει τους βαθμούς του σπουδαστή σε πέντε μαθήματα και θα εμφανίζει το μέσο όρο. • Dwse vathmous 5 mathimatwn • 1 program mesos_oros_5_mathimatwn(input, output); • 2 var a: array[1. . 5] of real; • 3 • 4 k: integer; • 5 sum, mesos: real; • α[1]=1. 00000000 E+000 • α[2]=2. 00000000 E+000 begin • α[3]=3. 00000000 E+000 writeln('Dwse vathmous 5 mathimatwn'); • α[4]=4. 00000000 E+000 sum: =0. 0; • α[5]=5. 00000000 E+000 • αthroisma=1. 50000000 E+001 mesos for k: =1 to 5 do oros=3. 00000000 E+000 begin readln(a[k]); sum: = sum+a[k]; end; mesos: = sum/5; for k: =1 to 5 do begin writeln('a[', k, ']=', a[k]); end; writeln('athroisma=', sum, ' mesos oros=', mesos); readln end.

 • • • • • • • Να γραφτεί πρόγραμμα που θα διαβάζει

• • • • • • • Να γραφτεί πρόγραμμα που θα διαβάζει τα αντίστοιχα στοιχεία δύο πινάκων, θα τα προσθέτει και θα βάζει το αποτέλεσμα σε τρίτο πίνακα. Όλοι οι πίνακες είναι μονοδιάστατοι και έχουν πέντε στοιχεία. program prosthesh_pinakwn(input, output); var a, b, c: array[1. . 5] of real; k: integer; begin writeln('Dwse stoixeia prwtou pinaka'); for k: =1 to 5 do begin readln(a[k]); end; writeln('Dwse stoixeia defterou pinaka'); for k: =1 to 5 do begin readln(b[k]); end; writeln('ypologizw ta stoixeia tou tritou pinaka'); for k: =1 to 5 do begin c[k]: =a[k]+b[k]; end; writeln('deixnw ta stoixeia'); for k: =1 to 5 do begin writeln('c[', k, ']=', a[k], '+', b[k], '=', c[k]); end; readln end.