Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 1 ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΓΛΩΣΣΕΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ C & C++ Δ.Π.Θ. - ΤΜΗΜΑ Μ.Π.Δ. ΕΞΑΜΗΝΟ Α’

Παρόμοιες παρουσιάσεις


Παρουσίαση με θέμα: "ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 1 ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΓΛΩΣΣΕΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ C & C++ Δ.Π.Θ. - ΤΜΗΜΑ Μ.Π.Δ. ΕΞΑΜΗΝΟ Α’"— Μεταγράφημα παρουσίασης:

1

2 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 1 ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΓΛΩΣΣΕΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ C & C++ Δ.Π.Θ. - ΤΜΗΜΑ Μ.Π.Δ. ΕΞΑΜΗΝΟ Α’

3 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 2 Από τον οδηγό σπουδών του Τμήματος … χωρίς να παραγνωρίζονται οι πρόοδοι που έχουν συντελεστεί σε επιμέρους τομείς, η πλέον ολοκληρωτική αλλαγή οφείλεται στη διείσδυση των ΗΥ σε όλο το φάσμα της παραγωγικής διαδικασίας. Η εφαρμογή νέων τεχνολογιών στη σχεδίαση προϊόντων, η χρήση προχωρημένων βιομηχανικών αυτοματισμών, η υιοθέτηση μοντέρνων μεθόδων ποιοτικού ελέγχου, η διαχείριση της ροής των τεχνικών και οικονομικών πληροφοριών στην παραγωγή, στηρίζονται άμεσα στη χρήση ΗΥ και απαιτούν αυξημένη τεχνογνωσία και κατάλληλα εκπαιδευμένο προσωπικό…..

4 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 3 Αλγόριθμοι Αλγόριθμος είναι ένα διατεταγμένο και πεπερασμένο σύνολο καθορισμένων αριθμητικών ή λογικών πράξεων ή εντολών, που όταν εφαρμόζονται σε ένα σύνολο δεδομένων εισόδου, ενός συγκεκριμένου και καλώς προσδιορισμένου προβλήματος, παράγουν μια λύση σε πεπερασμένο χρόνο. Κάθε λύση προσδιορίζεται από ένα σύνολο αποτελεσμάτων που λέγονται δεδομένα εξόδου.

5 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 4 Ιδιότητες αλγορίθμων Καθοριστικότητα (οι πράξεις που χρησιμοποιεί ένας αλγόριθμος πρέπει να είναι καλώς καθορισμένες Περατότητα ( το πρόβλημα επιλύεται σε πεπερασμένο πλήθος πράξεων ή εντολών). Μια διαδικασία που δεν πληροί την ιδιότητα της περατότητας λέγεται υπολογιστική μέθοδος. Αποτελεσματικότητα (όλες οι πράξεις πρέπει να είναι ικανοποιητικά απλές ώστε να εκτελούνται από μια μηχανή ή από έναν άνθρωπο με μολύβι και χαρτί).

6 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 5 Προβλήματα και στιγμιότυπα Ένα πρόβλημα εκφράζεται συνήθως με μια εντολή ή ένα ερώτημα που πρέπει να απαντηθεί. Με την εξειδίκευση του προβλήματος ορίζονται τα αποδεκτά δεδομένα εισόδου που εκφράζονται συνήθως με τις παραμέτρους του. Ορίζονται επίσης επακριβώς τα αναμενόμενα αποτελέσματα εξόδου, ως συνάρτηση επί των δεδομένων εισόδου. Ένα πρόβλημα επιλύεται όταν έχει αναπτυχθεί ο κατάλληλος αλγόριθμος που μετασχηματίζει όλα τα αποδεκτά δεδομένα εισόδου σε ορθά αποτελέσματα εξόδου.

7 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 6 Στιγμιότυπο Ένα στιγμιότυπο ενός προβλήματος είναι μια ανάθεση τιμών στις παραμέτρους του. Πεδίο ορισμού ενός προβλήματος είναι το σύνολο των στιγμιοτύπων του. Ένας αλγόριθμος λέγεται ορθός αν και μόνον αν επιλύει κάθε στιγμιότυπο ενός προβλήματος.

8 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 7 Εισαγωγή στη γλώσσα προγραμματισμού C 1.Ιστορική εξέλιξη των γλωσσών προγραμματισμού – δημιουργία της γλώσσας – σκοπός – εξελίξεις – Turbo C – C++ - ANSI C – Visual C++ - C++ builder 2.Διαδικαστικές γλώσσες – αντικειμενοστραφείς γλώσσες – περιβάλλοντα ανάπτυξης 3.Γιατί χρησιμοποιούμε γλώσσα C; (δυναμική, δημοφιλής, μεταφερσιμότητα σε πολλά λειτουργικά συστήματα, λιτή σε λέξεις, δομημένη) 4.Τι είναι η C++ (object oriented programming) – αποτελεί υπερσύνολο της C

9 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 8  Διαχείριση μνήμης σε προσωπικούς υπολογιστές – χάρτης μνήμης (memory map)  Διαχείριση μνήμης στους μεταγλωττιστές της C – αρχεία επικεφαλίδων  ΔΟΜΗ ΕΝΟΣ ΠΡΟΓΡΑΜΜΑΤΟΣ C  ΛΑΘΗ – ΣΥΝΤΑΚΤΙΚΑ – ΕΚΤΕΛΕΣΗΣ  Η C είναι γλώσσα case sensitive (τα κεφαλαία και τα πεζά γράμματα θεωρούνται διαφορετικά)

10 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 9 Το 1ο πρόγραμμα σε C #include int radius; float area; void main () { printf("enter radius ="); scanf("%d", &radius); area=3.14159*radius*radius; printf("\n\nArea= %f",area); printf("\n"); }

11 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 10 Συναρτήσεις Τι είναι μια συνάρτηση; –Είναι ένα ανεξάρτητο τμήμα προγράμματος που υλοποιεί μια καθορισμένη σειρά ενεργειών και έχει δικό της όνομα. –Όταν στο πρόγραμμα αναφέρεται το όνομα της συνάρτησης εκτελείται η σειρά εντολών που αποτελούν τη συνάρτηση. –Σε μια συνάρτηση μπορούμε να δώσουμε πληροφορίες (ορίσματα) και η συνάρτηση να επιστρέψει στο κυρίως πρόγραμμα αποτελέσματα που χρησιμοποιούν τις πληροφορίες εισόδου. Υπάρχουν δύο τύποι συναρτήσεων : Library functions (συναρτήσεις βιβλιοθήκης) User-defined functions (συναρτήσεις του χρήστη)

12 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 11 Συστατικά ενός προγράμματος C 1.the main () function 2.the #include directive 3.variable definition 4.function prototype 5.program statements 6.function definition 7.program comments

13 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 12 Παράδειγμα με χρήση συνάρτησης /* programma gia ton ypologismo toy ginomenoy dyo arithmwn */ #include int a,b,c; int product (int x, int y); main () { /* EISAGOGI TOY PROTOY ARITHMOU */ printf("enter a number between 1 and 100 :"); scanf("%d", &a ); /* EISAGOGI TOY DEYTEROY ARITHMOU */ printf("enter a number between 1 and 100 :"); scanf("%d", &b ); /* ypologismos kai emfanish toy apotelelsmatos */ c=product(a,b); printf("\n%d times %d = %d",a,b,c); } /* main body of the function product */ int product (int x, int y) { return (x*y); }

14 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 13 Τύποι αριθμητικών μεταβλητών & σταθερών

15 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 14 Παραδείγματα ονομάτων int count; float rate; double percent=0.01, taxrate=25.5; int weight=10000; const float PI=3.141; int age = 18; double GPA = 3.25, credits; char letterGrade = 'A';

16 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 15 Πραγματικοί αριθμοί Μορφή σταθερής υποδιαστολής (fixed-point) : -0.333, 0.5, 1.414, Μορφή κινητής υποδιαστολής (floating-point) : 2.998e8, 0.2998e9,

17 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 16 STATEMENTS – EXPRESSIONS – OPERATORS { printf(“TMHMA MHXANIKWN”); printf(“ PARAGWGHS & DIOIKHSHS”); } Απλές εντολές : επιτρέπεται η χρήση κενών διαστημάτων για ευκολία στην ανάγνωση Σύνθετες εντολές: περισσότερες από μια εντολές περικλείονται σε { }

18 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 17 ΤΕΛΕΣΤΕΣ (αριθμητικοί) +, -, *, /, % (modulus) προτεραιότητα τελεστών ++, -- *, /, % +, -

19 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 18 Τελεστές (συνέχεια) Εκχώρησης (assignment) : x=y unary mathematical operators ++-- ++x;  x=x+1; --y;  y=y-1;

20 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 19 Διάκριση των τελεστών ++ και -- Μορφές ++x, --y : prefix mode –η αύξηση ή η μείωση των τιμών των μεταβλητών γίνεται πριν αυτές χρησιμοποιηθούν. Μορφές x++, y-- : postfix mode –η αύξηση ή η μείωση των τιμών των μεταβλητών γίνεται αφού χρησιμοποιηθούν Παραδείγματα x=10; y=x++;(x=11, y=10) x=10; y=++x;(x=11, y=11)

21 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 20 ΠΑΡΑΔΕΙΓΜΑΤΑ x=a*b++ισοδύναμο με x=a*b b=b+1 x=--i*(a+b) ισοδύναμο με i=i-1 x=i*(a+b) i+=10  i=i+10

22 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 21 ΣΧΕΣΙΑΚΟΙ ΤΕΛΕΣΤΕΣ Equal == Greater than > Less than < Greater equal >= Less equal <= Not equal !=

23 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 22 Παραδείγματα x == y x != y x = y x > y x <= y Το συχνότερο λάθος είναι η χρήση του = ως τελεστή εκχώρησης τιμής αντί του == ως τελεστή της ισότητας (σχεσιακός).

24 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 23 Λογικοί τελεστές &&AND ||OR !NOT

25 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 24 Προτεραιότητα Υψηλότερη προς χαμηλότερη: –παρενθέεις –Not ( ! ) –σύγκριση (, = ) –ισότητα ( == ) –ανισότητα ( != ) –And ( && ) –Or ( || ) ΠΡΟΣΟΧΗ: Ο ΤΕΛΕΣΤΗΣ ΕΚΧΩΡΗΣΗΣ (=) είναι χαμηλότερης προτεραιότητας από τους Boolean / Logical τελεστές.

26 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 25 int-s και float-s float : είναι ο τύπος που υπερισχύει παράδειγμα: 1 + 2 * 3 - 4.0 / 5 = 1 + (2 * 3) - (4.0 / 5) = 1 + 6 - 0.8 = 6.2

27 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 26 int-s και float-s – παράδειγμα 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

28 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 27 int-s και float-s – παράδειγμα 2 (συνέχεια) (1 + 2.0) * (3 - 4) / 5 = ((1 + 2.0) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

29 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 28 int-s και float-s – παράδειγμα 3 (1 + 2.0) * ((3 - 4) / 5) = (1 + 2.0) * (-1 / 5) = 3.0 * 0 = 0.0

30 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 29 Αριθμητική μικτού τύπου #include int n,p; float k,q; void main (){ n=6/4.0; k=6/4.0; p=6/4; q=6/4; printf("n = %4d\n",n); printf("k = %4.2f\n",k); printf("p = %4d\n",p); printf("q = %4.2f\n",q);}

31 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 30 Είσοδος – Έξοδος scanf printf –%d  decimal integer –%f  floating point number

32 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 31 scanf() παράδειγμα: scanf(“%d”, &x); printf() παράδειγμα: printf(“The value of x is %d\n”, x); #include

33 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 32 Εισαγωγή δεδομένων -scanf #include #define euro 340.75 int code; float salary,salary_euro; void main () { printf("code="); scanf("%d",&code); printf("salary="); scanf("%f",&salary); salary_euro=salary/340.75; printf("\n\ncode = %5d\n",code); printf("salary = %12.0f\n",salary); printf("salary_euro = %12.3f\n",salary_euro); }

34 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 33 Streams: Input -- Example 135 25.5 _ 135 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); input buffer

35 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 34 Streams: Input -- Example (cont) 135 25.5 _ 135 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); itemcost

36 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 35 Streams: Input – Example (cont) 135 25.5 _ 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost

37 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 36 Streams: Input – Example (cont) 135 25.5 _ \n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost 25.5

38 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 37 Streams: Output -- Example Hello!\n printf(“Hello!\n”); output buffer

39 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 38 Hello!\n printf(“Hello!\n”); Streams: Output – Example (cont)

40 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 39 ello!\n H printf(“Hello!\n”); Streams: Output – Example (cont)

41 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 40 llo!\n He printf(“Hello!\n”); Streams: Output – Example (cont)

42 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 41 lo!\n Hel printf(“Hello!\n”); Streams: Output – Example (cont)

43 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 42 o!\n Hell printf(“Hello!\n”); Streams: Output – Example (cont)

44 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 43 !\n Hello printf(“Hello!\n”); Streams: Output – Example (cont)

45 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 44 \n Hello! printf(“Hello!\n”); Streams: Output – Example (cont)

46 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 45 Hello! _ printf(“Hello!\n”); Streams: Output – Example (cont)

47 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 46 Προδιαγραφές Εμφάνισης #include #define euro 340.75 int code; float salary,salary_euro; void main() { code=17344; salary=247894.65; salary_euro=salary/euro; printf("CODE = %8d\n",code); printf("MISTHOS = %12.3f\n",salary); printf("euro = %12.3f\n",salary_euro); }

48 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 47 Formatted Input and Output General form: printf(format-control-string, other-arguments); scanf(format-control-string, other-arguments); Examples: printf("%s\n%f\n%c\n%d\n",name,age,gender,idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber);

49 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 48 Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); printf -- Format-Control-String

50 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 49 Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); conversion specifiers printf -- Format-Control-String (cont)

51 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 50 printf(“%s is %d years old.\n”, name, age); literal characters Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example : printf -- Format-Control-String (cont)

52 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 51 For printf : variables containing data for output Example: (“ printf(“%s is %d years old.\n”, name, age); printf -- Other-Arguments

53 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 52 Describes the format of the data given as input Contains “conversion specifiers” scanf -- Format-Control-String Example: scanf("%s %f %c %d", name, &age, &gender,&id); conversion specifiers

54 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 53 For scanf: “pointers” to variables where the input will be stored scanf -- Other-Arguments scanf("%s %f %c %d", name, &age, &gender, &id); Example:

55 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 54 ‘&’ is for scanf only! scanf("%s %f %c %d", name, &age, &gender, &id); scanf -- Other-Arguments (cont) For scanf : “pointers” to variables in which the input will be stored Example: Variables of type int, float or char need ‘&’ Do NOT use ‘&’ with strings!

56 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 55 Common Conversion Specifiers for Numerical Information decimal integer: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum); float: %f printf(“%f squared is...? ”, x); scanf(“%f”, &ans); double : printf(“%f squared is...? ”, x); scanf(“%lf”, &ans);

57 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 56 Conversion Specifiers for Alphanumeric Information char : %c printf(“What letter follows %c?\n”,ch); scanf(“%c”, &nextchar); string : %s printf(“Name: %s\n”, name); scanf(“%s”, name);

58 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 57 i or d: display a signed decimal integer f: display a floating point value e or E: display a floating point value in exponential notation g or G: display a floating point value in either f form or e form L: placed before any float conversion specifier to indicate that a long double is displayed printf : Conversion Specifiers

59 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 58 scanf : Conversion Specifiers d: read an optionally signed decimal integer i: read an optionally signed decimal, octal, or hexadecimal integer i and d: the argument is a “pointer” to an integer int idNumber; scanf("%d", &idNumber);

60 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 59 scanf : Conversion Specifiers (cont) h or l: placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int idNumber; scanf("%ld", &idNumber); l or L: placed before any float conversion specifiers to indicate that a double or long double is to be input

61 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 60 Escape Characters (ειδικοί χαρακτήρες) '\n' newline character '\t' horizontal tab '\v' vertical tab '\f' form feed '\a' alert/bell '\\' backslash char '\'' apostrophe '\"' double quote

62 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 61 Εντολή επιλογής (if) if ( expression ) statement1; if (temperature < 0) print("Frozen\n"); if( expression ) statement1; else statement2 ; if (result >= 45) printf("Pass\n"); else printf("Fail\n");

63 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 62 Παραδείγματα if (z>x) { z=5; x=3; } else { z=3; x=5; }

64 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 63 if (result >= 75) printf("Passed: Grade A\n"); else if (result >= 60) printf("Passed: Grade B\n"); else if (result >= 45) printf("Passed: Grade C\n"); else printf("Failed\n");

65 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 64 Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Example: oddnum.c

66 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 65 #include /* Read in a number, and echo it if it is odd. */ int main() { return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

67 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 66 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

68 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 67 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

69 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 68 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put “then” here!

70 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 69 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put semicolon here!

71 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 70 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

72 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 71 Notes on if Which of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); } A B C

73 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 72 Notes on if Which of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); } A B C

74 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 73 Notes on if Common mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n");

75 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 74 Notes on if Common mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n"); No semi- colon here!

76 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 75 Notes on if Common mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number);

77 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 76 Notes on if Common mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number); Should be ==

78 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 77 The else statement Can only occur after an if statement Is only executed when the if block does not execute

79 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 78 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

80 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 79 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

81 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 80 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” } No semicolons here!

82 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 81 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

83 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 82 Example: months.c Determine the number of days in a given month: 30 days hath September, April, June and November. All the rest hath 31, Excepting February alone, Which hath 28 days clear, And 29 in each leap year. output “Enter an integer” input month if (month is September, or April, or June, or November) then { output “30 days” } else if (month is February) { output “28 or 29 days” } else { output “31 days” }

84 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 83 int main() { return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

85 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 84 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

86 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 85 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

87 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 86 #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0; } Example: months.c Common mistake: if (month==September || April || June || November )

88 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 87 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

89 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 88 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

90 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 89 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; “Default” block.

91 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 90 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

92 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 91 ΕΠΑΝΑΛΗΠΤΙΚΕΣ ΕΝΤΟΛΕΣ WHILE ( ο έλεγχος για τη συνέχεια της επανάληψης γίνεται στην αρχή) FOR (το πλήθος των επαναλήψεων καθορίζεται ρητά) DO – WHILE ( ο έλεγχος για τη συνέχεια της επανάληψης γίνεται στο τέλος)

93 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 92 Η εντολή while Υλοποιεί την επαναληπτική διαδικασία ενός αλγορίθμου –Εκτελεί επαναληπτικά μια ομάδα (block) εντολών –Ελέγχει μια συνθήκη (λογική έκφραση) στην αρχή κάθε επανάληψης –Τερματίζεται όταν η συνθήκη γίνει ψευδής (μηδέν)

94 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 93 Εντολή while (επανάληψη υπό συνθήκη) while ( expression ) statement... counter=1; while (counter<5) { printf("counter=%i",counter); counter++; }

95 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 94 Example: addnum.c Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

96 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 95 Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Initialize Check condition Update Iteration Control Example: addnum.c (cont)

97 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 96 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

98 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 97 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count only the variables sum and count are initialized to 0 Example: addnum.c (cont)

99 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 98 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); return 0; }. Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

100 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 99 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

101 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 100 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

102 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 101 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Same as: sum = sum + nextNum; Others: -=, *=, /=, etc. (King, Table 4.2) Example: addnum.c (cont)

103 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 102 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Same as: count = count + 1; Decrement: count --; (King, Table 4.2) Example: addnum.c (cont)

104 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 103 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

105 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 104 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

106 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 105 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNumberscountnextNumsum ???? 0 0.0

107 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 106 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ???? 0 0.0 3

108 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 107 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ???? 0 0.0 3 4 4.0 1

109 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 108 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ???? 0 0.0 3 4 4.0 1 3.0 2

110 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 109 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ???? 0 0.0 3 4 6.2 4.0 1 3.0 2 3 9.2

111 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 110 Συνήθη σφάλματα στη while while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); while (num < minimum) { scanf(“%d”, &num); } printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”);

112 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 111 while (num < minimum) { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); } while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); Συνήθη σφάλματα στη while

113 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 112 while (num < minimum); { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); } Συνήθη σφάλματα στη while Τέλος του block της while – συνήθως η αιτία για ατέρμονες ανακυκλώσεις

114 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 113 do statement... while( expression ); counter=1; do { counter++; printf("%i",counter); } while ( counter<100 ); Εντολή do - while Έλεγχος στο τέλος

115 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 114 Εντολή for (καθορισμένο πλήθος επαναλήψεων) for ( expression1 ; expression2 ; expression3 ) statement... for (loop=0; loop<1000; loop++) printf ("%i\n",loop);

116 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 115 Η εντολή for Δημιουργεί ένα βρόγχο που επιτρέπει τον καθορισμό των αρχικών και τελικών ορίων καθώς και τον έλεγχο της επανάληψης Σύνταξη : for ( initialization ; condition ; update ) { block εντολών } Προσοχή! Ένα ερωτηματικό εδώ σημαίνει τέλος του block των εντολών!

117 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 116 Example: addfor.c Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

118 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 117 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont)

119 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 118 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Initialize Example: addfor.c (cont)

120 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 119 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Check condition Example: addfor.c (cont)

121 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 120 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont) Update (Increment Step) IMPORTANT!! The Update is performed AFTER the body of the loop

122 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 121 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont)

123 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 122 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } while and for #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; }

124 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 123 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } while and for (cont) #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Check condition Initialize

125 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 124 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Check condition while and for (cont)

126 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 125 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Update while and for (cont)

127 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 126 Εμφωλιασμένες επαναλήψεις(nested loops) Οι επαναλήψεις μπορούν να περιέχονται η μία μέσα στην άλλη το συνολικό πλήθος τoυ block των εντολών που εκτελούνται είναι το γινόμενο του πλήθους επαναλήψεων κάθε επαναληπτικής εντολής που συμμετέχει.

128 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 127 Example: rect.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

129 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 128 Example: rect.c (cont) #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

130 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 129 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

131 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 130 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { for (colc=0; colc < numcol; colc++) { } } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

132 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 131 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { for (colc=0; colc < numcol; colc++) { printf("*"); } } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

133 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 132 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { for (colc=0; colc < numcol; colc++) { printf("*"); } printf("\n"); } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

134 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 133 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { for (colc=0; colc < numcol; colc++) { printf("*"); } printf("\n"); } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

135 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 134 Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); for (rowc=0; rowc < numrow; rowc++) { for (colc=0; colc < numcol; colc++) { printf("*"); } printf("\n"); } return 0; } program Example: rect.c (cont) algorithm

136 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 135 #include /* Print an m-by-n rectangle of asterisks */ int main() { int rowc, colc, numrow, numcol; printf("\nEnter width: "); scanf("%d", &numcol); printf("\nEnter height: "); scanf("%d", &numrow); rowc = 0; while (rowc < numrow) { for (colc=0; colc < numcol; colc++) { printf("*"); } printf("\n"); rowc++; } return 0; } Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

137 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 136 Εμφωλιασμένες εντολές επανάληψης (1) Η παρακάτω δομή ΔΕΝ επιτρέπεται for ( count = 1; count < 100; count++) { do { /* the do...while loop */ } /* end of for loop */ }while (x != 0);

138 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 137 Εμφωλιασμένες εντολές επανάληψης (2) Η παρακάτω δομή επιτρέπεται : for (count = 1; count < 100; count++) { do { /* the do...while loop */ }while (x != 0); } /* end of for loop */

139 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 138 Υπάρχει λάθος ? void main() { int record = 0; while (record < 100) { printf( "\nRecord %d ", record ); printf( "\nGetting next number..." ); record++; }

140 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 139 Ποιο είναι το λάθος ? #include int count, MAXVALUES; void main() { for (count = 1;count <MAXVALUES; count++); printf("\nCount = %d", count); }

141 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 140 Επανάληψη - παραδείγματα #include main() { printf("ayto einai ena paradeigma ektypvshs!"); printf("ektypwsh enos xaraktira, %c\n enos arithmou, %d\n enos pragmatikoy arithmou %f",'b',271,345.67899); }

142 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 141 #include int x,y; main() { for (x=0;x<10;x++, printf("\n") ) for (y=0; y<10; y++) printf("x"); }

143 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 142 #include main () {int i,j; scanf("%d",&i); scanf("%d",&j); printf("sum = %d ",i+j); printf("ginomeno= %d ",i*j); }

144 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 143 #include int loop; void main() { for(loop=0;loop<1000;loop++) printf("%i\ ",loop); }

145 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 144 #include #define KILOS_PER_POUND.45359 void main() { int pounds; printf(" US lbs UK st. lbs INT Kg\n"); for(pounds=10; pounds < 250; pounds+=10) { int stones = pounds / 14; int uklbs = pounds % 14; float kilos = pounds * KILOS_PER_POUND; printf(" %3d %4d %3d %10.4f\n", pounds, stones, uklbs, kilos); }

146 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 145 # include main () { double x,y,z,max; printf("enter three values ="); scanf("%lf %lf %lf",&x,&y,&z); if (x>y) if (x>z) max=x; else max=z; else if (y<z) max=z; else max=y; printf("the maximum values is %f\n ",max); }

147 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 146 #include void main () {int i,n,sum,sumpos,sumneg; sum=sumpos=sumneg=0; for (i=0; i<=10; i++) {scanf("%d",&n); sum=sum+n; if (n>0) sumpos+=n; else sumneg+=n; } printf;printf("sum = %d\n",sum); printf("sumpos = %d\n",sumpos); printf("sumneg = %d\n",sumneg); }

148 ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 147 #include main () { int i; scanf("%d",&i); if (i>=4) while (i--) ; printf("%d",++i); }


Κατέβασμα ppt "ΔΠΘ-ΤΜΗΜΑ ΜΠΔ: ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ 1 ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΓΛΩΣΣΕΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ C & C++ Δ.Π.Θ. - ΤΜΗΜΑ Μ.Π.Δ. ΕΞΑΜΗΝΟ Α’"

Παρόμοιες παρουσιάσεις


Διαφημίσεις Google