Exercise 6 Compound Types and Control Flow Daniel Bogado Duffner Slides auf: Informatik I für D-MAVT bodaniel@student.ethz.ch n.ethz.ch/~bodaniel
Agenda Recap/Quiz Structures Unions Enumerations Loops 2
Arrays Mit welcher Zahl beginnt der Index und mit welcher endet er allgemein? int e[3]; int zahl=e[3]; Fehler?! Was kann ein Array alles speichern? 1 2 3 4 5 6 7 //beginnt mit 0, endet mit Anzahl Elemente -1 //ja, aber kein Kompilierfehler //immer nur einen festen Typen, aber vor Definition alle e[3][4]=? e[1][3]=? e[0][4]=? 8 9 10 11 12 13 14 15 16 17 18 19 20 21 //n.d. //11 //5 3
Arrays Deklaration gültig? int blubb[]; int blubb[3]={1,2,3,4}; int blubb[]={1,2}; const int b; int blubb[b]; cin >> b; int blubb[b]; int blubb[2]={13,4}; int b=3; int blubb[b]; int[3][]; //nein, keine Grösse angegeben //nein, zu viele Elemente //ja //nope, da b nicht definiert ist, keine Zahl //ja //ja //ja, aber nicht immer, nicht const (Compiler abhängig) //Nein, geht nur umgekehrt: int[][3] 4
Arrays - Kurzübersicht Arrays sind 0 indexiert (Zählung fängt bei 0 an) Arrays können immer nur Ausdrücke von einem Typ (int, bool, ) speichern int test1[]; geht nicht. Grösse muss bei Compilezeit festliegen! int test2[][3]; geht, aber nicht umgekehrt (int t2[3][] od. int t3[][]) 5
Strings Welche library enthält string anweisungen? char name[]= hallo korrekt? Grossbuchstaben? String kopieren? String Länge? String anfügen? //<cstring> //nein, = hallo //string[0]=toupper(string[0]); //strcpy(string2,string); strcpy(hier wird es reinkopiert, das reinzukopierende); //strlen(string); //strcat(string2,string); 6
Structures are user-defined types Type definition: Variable declaration: Accessing members: Structures struct MyNewDataType { member_type1 member1; member_type2 member2;... }; MyNewDataType mynewdataobject; mynewdataobject.member1 =...; 7
Structures Definition Initialisation struct MyStruct{ int i; float f; }; MyStruct m1; MyStruct m2 = {1, 2.0f}; Assignment m1 = m2; // m1.i = m2.i; m1.f = m2.f; There can also be an struct array (array of struct type) 8
Type of member variables can be arbitrary Self-defined types can also be used Example: Structures: Nesting struct Date{ int day; int month; int year; }; No recursive nesting (no Date struct in Date) Recursive data types: later in course struct Person { Date birthday; string name; int age; }; 9
Definition: Initialisation: Values after initialization: Unions union MyUnion{ char c[4]; // On the same memory int i; // On the same memory }; MyUnion u1; u1.i = 1; c[3] c[2] c[1] c[0] i = 0x00000001 // c[0] == 1, c[1] == 0 // c[2] == 0, c[3] == 0 If they don t have the same size, the union takes the size of the biggest element. 10
Enumerations Definition: enum Months{January, February, March}; enum Months{January = 1, February, March}; enum Months{January = 1, June = 6, March = 3}; Assignment is limited: int a = February; Months a = 2; // ERROR 11
Loops for loops while loops do while loops 12
Loops: for Loop Declare and initialize loop variable: Check continue condition BEFORE: Increment AFTER loop: int i=0; i < 4; i++; for (int i=0; i < 4; i++) { } cout << i is: << i; Sidenote: Shown here is the C99 standard. Older C-code might require this: int i; for (i=0; i < 4; i++) 13
Declare and initialize loop variable: Check continue condition BEFORE: Increment AFTER loop: Loops: while Loop int i=0; while (i < 4) { cout << i is: << i; i++; } int i=0; i < 4; i++; Sidenote: Shown here is the C99 standard. Older C-code might require this: int i; for (i=0; i < 4; i++) 14
Loops: for and while Comparison for and while can be used equivalently Loop variable i stays valid after the loop if declared outside of the braces for loops for known length iteration while loops to loop until complex condition is met int i; for (i=0; i < 4; i++) { cout << i is: << i; } int i=0; while (i < 4) { cout << i is: << i; i++; } 15
Loops: do while Loop Declare and initialize loop variable: Check continue condition AFTER: Increment AFTER loop: int i=0; i < 4; i++; int i=0; do { cout << i is: << i; i++; } while (i < 4); Sidenote: Shown here is the C99 standard. Older C-code might require this: int i; for (i=0; i < 4; i++) 16
Exercise 6 Programming: Extend means program to use loops Programming: Use 3 loop types to calculate factorials Programming: Implement Sieve of Erathostenes Experiment with UNIX processes Hand in: Program source codes for tasks 1 3 Screenshot for task 4 17
Task 1: Durchschnitt Durchschnitt aus 5 Zahlen mittels Schleife (Haben einige von euch auch schon bei Übung 5 gemacht) Task 2: Fakultät Exercise 6 Task 1,2 Hier soll mittels 3 unterschiedlichen Funktionen die Fakultät berechnet warden. Alle Funktionen sollen das gleiche Ergebnis liefern und dieses jeweils durch einen anderen Schleifen-Typ erreichen Fakultät von 5: 5!= 5*4*3*2*1 Benutzt eine Zwischenspeicher Variable und rechnet von oben herab 18
Task 3: Primzahlen Es sollen aus einem Array mit Zahlen von 1 bis n alle Zahlen, die keine Primzahlen sind herausgestrichen werden. Typ des Arrays: Bool, Grösse des arrays durch cin eingelesen Wenn Primzahl dann Ergebnis true, wenn nicht, dann false 2 Schleifen, eine äussere zum überprüfen ob true oder false und eine innere, die wenn das Ergebnis true ist, alle vielfache dieser Zahl herausstreicht Am Ende alle Primzahlen auf die Konsole ausgeben Task 4: UNIX Exercise 6 Task 3 Einfach gemacht und verstanden schreiben, wenn es geklappt hat, ansonsten eventuelle Fragen, kein Screenshot (unnötiger Aufwand) 19