Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals 3 rd lecture Szabolcs Papp.

Similar presentations


Presentation on theme: "Programming Fundamentals 3 rd lecture Szabolcs Papp."— Presentation transcript:

1 Programming Fundamentals 3 rd lecture Szabolcs Papp

2 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.2/41  Loops – specification+algorithm+coding Loops  First example for arrays First example for arrays  The array The array  Array instead of conditional statement Array instead of conditional statement  Constant arrays Constant arrays Content

3 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.3/41 Loops Problem: Let’s calculate the non-1 least divisor of and integer (N>1)! Specification:  Input: N:Integer  Output: D:Integer  Precondition: N>1  Post condition: 1<D  N and D|N and  i (2  i<D): i ł N

4 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.4/41 Loops An idea for solution: Let’s try 2; if not good then try 3; if still not good then try 4, …, worst case N will be a divisor! Algorithm expressing this: i:=2 i ł N i:=i+1 D:=i

5 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.5/41 Loops Problem: Let’s calculate both the non-1 least and the non-N greatest divisor of an integer (N>1)! Specification:  Input: N:Integer  Output: LD,GD:Integer  Precondition: N>1  Post condition: 1<LD  N and 1  GD<N and LD|N and  i (2  i<LD): i ł N and GD|N and  i (GD<i<N): i ł N

6 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.6/41 Loops Note: By knowing LD, GD can be definied in post condition in another way: LD*GD=N! Algorithm expressing this: i:=2 i ł N i:=i+1 LD:=I GD:=N Div LD

7 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.7/41 Loops Problem: Let’s calculate the non-1 and non-N least divisor of and integer (N>1), if exists! Specification :  Input: N:Integer  Output: D:Integer, EXISTS:Boolean  Precondition: N>1  Post condition: EXISTS=  i (2  i<N): i|N and EXISTS  D|N and 2  D<N and  i (2  i<D): i ł N

8 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.8/41 Loops Algorithm: Note: Whenever i is a divisor of N, then ( N Div i) is also a divisor, so we are good to go until the square root of N! i:=2 i<N and i ł N i:=i+1 EXISTS:=i<N EXISTS D:=i  I N 2  i  N Div i  N Div 2 thus i*i  N thus i  N

9 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.9/41 Loops Problem: Let’s calculate the sum of all the divisors of an integer (N>1)! Specification:  Input: N:Integer  Output: S:Integer  Precondition: N>1  Post condition:

10 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.10/41 Loops Algorithm: S:=0 i=1..N i|N S:=S+i  I N

11 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.11/41 Loops Problem: Let’s calculate the sum of all odd divisors of an integer (N>1)! Specification:  Input: N:Integer  Output: S:Integer  Precondition: N>1  Post condition: S=

12 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.12/41 Algorithm 1 : Algorithm 2 : Loops S:=0 i=1..N i|N és odd(i) S:=S+i  S:=0 i=1..N; by 2 i|N S:=S+i  I N I N

13 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.13/41 Loops Problem: Let’s calculate the sum of all prime divisors of an integer (N>1)! Specification:  Input: N:Integer  Output: S:Integer  Precondition: N>1  Post condition: S= isprime(i)???

14 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.14/41 Loops Algorithm: The least divisor is prime for sure; let’s divide N by it as many times as we can; the next divisor of the reduced N will be prime again. S:=0 i:=2 iNiN i|N S:=S+i  i|N N:=N Div i i:=i+1 I N

15 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.15/41 Loops Problem: Let’s determine the count of i,j (i,j>1) integer pairs, where i*j<N! Specification:  Input: N:Integer  Output: S:Integer  Precondition: N>1  Post condition: S=

16 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.16/41 Loops S:=0 i=2..N Div 2 j=2..N Div 2 i*j<N S:=S+1  Algorithm 1 : I N

17 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05. 17/41 Loops S:=0 i=2..N Div 2 j:=2 i*j≤N S:=S+1 j:=j+1 Algorithm 2 : Algorithm 3 : S:=0 i=2..N Div 2 S:=S+(N Div i)-1

18 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.18/41 Loops Let’s observe that:  If there is , , or  sign in post condition, then the solution always contains loop!  If there is  or  sign in post condition, then the solution is mostly a conditional loop!  If there is  sign in post condition, then the solution is mostly a for loop! (  also)  In case of two embedded  signs, we will have two for loops embedded in each other.  In case of conditional , there will be a conditional statement within the loop.

19 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.19/41 Loops algorithm – code Conditional loop: For loop: condition statement while (condition){ statements } i=1..N statement for (int i=1;i<=N;i++){ statements } i=1..N; by x statement for (int i=1;i<=N;i+=x){ statements } statement condition do{ statement }while (condition) Typical usage at console inputs (see 2 nd lecture) 2 nd lecture2 nd lecture

20 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.20/41 Sample exercise for conditional statement, or do we need something else ? Problem: The Japanese calendar contains 60 years cycles. The years are paired, and a color is assigned to each pair (green, red, yellow, white and black). o 1,2,11,12, …,51,52: green years o 3,4,13,14,…,53,54: red years o 5,6,15,16,…55,56: yellow years o 7,8,17,18,…57,58: white years o 9,10,19,20,…,59,60: black years We know that the last cycle has started in 1984 that will end in 2043. Let’s write a computer program which determines the color assigned to a given M year (1984≤M≤2043)!

21 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.21/41 Conditional statement or do we need something else ? Specification 1 :  Input year:Integer  Output: c:Color  Precondition: 1984≤year and year≤2043  Post cond. ((year-1984) Mod 10) Div 2=0 and c=”green” or ((year-1984) Mod 10) Div 2=1 and c=”red” or …  Definition: Color:=(”green”,”red”, ”yellow”,”white”, ”black”)  Text This is still an “unknown” set. We define the set Color, which is derived from Text

22 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.22/41 Conditional statement or do we need something else ? Specification 2 :  Input: year:Integer  Output: c:Color Color=(”green”,”red”,”yellow”, ”white”,”black”)  Text  Precondition: 1984≤year and year≤2043  Post cond: ((year-1984) Mod 10) Div 2=0 and c=”green” or ((year-1984) Mod 10) Div 2=1 and c=”red” or … We can define Color set here, too

23 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.23/41 Conditional statement or do we need something else ? y:=((year-1984) Mod 10) Div 2 y=0y=1y=2y=3y=4 sc:= ”green” s:= ”red” s:= ”yellow” s:= ”white” s:= ”black” Question: Would we do the same if we had 90 ways instead of 5? Before answering this, a new structure: array Algorithm:

24 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.24/41 Arrays Related concepts:  Sequence: a countable sequence of same typed data elements  Array: finite length sequence, we can define operations for i th element (we know the least and the greatest index, or the count).  Index: sometimes 1..N, sometimes 0..N–1.  Operations on elements of an array: reference to an element, modification of an element (the element is selected by a given index).

25 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.25/41 Arrays (in algorithm) Examples for declaration: X:Array[1..N:Integer] Y:Array[0..4:Text] We can represent the Color set from the previous example with a constant array: Constant Color:Array[0..4:Text]= (”green”,”red”,”yellow”,”white”,”black”) Examples for referencing: X[1]:=X[N]; Y[i]:=Color[0]

26 ELTE Declaration: X:Array[1..N:Integer] Y:Array[0..4:Text] We can represent the Color set from the previous example with a constant array: Constant Color:Array[0..4:Text]= (”green”,”red”,”yellow”,”white”,”black”) 2016. 01. 05.2016. 01. 05.2016. 01. 05.26/41 Arrays (in algorithm and code) string Y[5] const string Color[5]={"green","red", "yellow","white","black"}; int X[N+1] Tömb-elemszám  indexelés 0..N index 0..???  count of elements in C++ :

27 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.27/41 Arrays (C++ code – overview) Static arrays:  Declaration:  References : const int MaxN=???;//maxcount type array[MaxN]; //array declaration //with indices 0..MaxN-1 … … array[ind] //reference to an element … array[ind]=expr;//modification of elem. … This is a difference between usual algorithm and code!

28 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.28/41 Arrays (C++ code – overview) Static array constants:  Declaration: or const int N=???;//number of elements const type array[N]={t1,t2,…,tN}; //declaration of constant array, //with indices 0..N-1 const type array[]={t1,t2,…,tN}; //declaration of const. array const int N=sizeof array/sizeof(type); //number of elements //indices: 0..N-1 Note that syntax is different in case of identifiers and types!

29 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.29/41 Arrays (C++ code – overview) Array variables:  Declaration:  Creation:  References (the same) : int N; //actual number of elements … N=???;//N to be defined, e.g. input type array[N];//an array containing N //elements with type “type” … array[ind] //reference to an element … array[ind]=expr;//modification of elem. …

30 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.30/41 Array input (with check) : bool err; //is there an error? … ! do{ cout > N; err=!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err); bool err; //is there an error? … ! do{ cout > N; err=!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err); IsCorrectN does both syntax and semantic check Arrays (C++ code – overview) In algorithm: Input: N [IsCorrectN(N))]

31 ELTE Array input (with check) : Let’s get back to our question… bool err; //is there an error? … ! do{ cout > N; err=!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err); bool err; //is there an error? … ! do{ cout > N; err=!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err); 2016. 01. 05.2016. 01. 05.2016. 01. 05.31/41 for (int i=0;i > v[i]; err=!Correct(v[i]); if (err) { cout > v[i]; err=!Correct(v[i]); if (err) { cout << "hibaüzenet" << endl; }; }while (hiba); } In algorithm: Input: v(1..N) [Helyes(v(1..N))] Correct does both syntax and semantic check Arrays (C++ code – overview)

32 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05. 32/41 Array instead of conditional statement Specification:  Input: year:Integer  Output: c:Color Color=(”green”,”red”,”yellow”, ”white”,”black”)  Text Constant Colors:Array[0..4:Color]= (”green”,”red”,”yellow”,”white”,”black”)  Precondition: 1984≤year and year≤2043  Post condition: c=Colors[((year-1984) Mod 10) Div 2]

33 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.33/41 Array instead of conditional statement Algorithm: y:=((year-1984) Mod 10) Div 2 c:=Colors[y]

34 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.34/41 Applying constant arrays Problem: Let’s create a computer program, which writes an integer between 1 and 99 with letters! Specification:  Input: N:Integer Constant ones:Arrays[0..19:Text]= (””, ”one”,…,”nineteen”) Constant tens: Array[2..9:Text]= (”twenty”,…,”ninety”)  Output: S:Text  Preconditon: 1  N  99 This is a better place here. Constant array is an output from the point of view of the algorithm.

35 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.35/41 Applying constant arrays  Post condition: N<20  S=ones[N] and N≥20  S=tens[N Div 10]+ ones[N Mod 10] Algorithm: N<20otherwhise S:=ones[N]S:=tens[N Div 10]+ ones[N Mod 10]

36 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.36/41 Applying constant arrays Problem: Let’s create a computer program that writes the serial number of a month based on the name of the month. Specification:  Input: H:Text Constant MonName:Array[1..12:Text]= (”January”,…,”December”)  Output: S:Integer  Precondition: M  MonName  Post cond.: 1  S  12 and MonName[S]=M

37 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.37/41 Algorithm: Question: What would happen if we didn’t meet the precondition? Runtime error? Infinite loop? Applying constant arrays i:=1 MonName[i]  M i:=i+1 S:=i

38 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.38/41 Constant arrays – What do we store? Problem: We have a non leap year. What is the serial number of a given day (month, day)? Specification 1 :  Input: M,D:Integer Constant mon:Array[1..12:Integer]= (31,28,31,…,31)  Output: S:Integer  Precondition: 1≤M≤12 és 1≤D≤mon[M]  Post condition:

39 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.39/41 Constant arrays – What do we store? Algorithm: Homework: How to modify this if we allow leap year? S:=D i=1..M–1 S:=S+mon[i]

40 ELTE 2016. 01. 05.2016. 01. 05.2016. 01. 05.40/41 Constant arrays – What do we store? Another solution: For each months, l et’s store the total number of days preceding the given month! Specification 2 :  Input: … Constant mon:Array[1..12:Integer]= (0,31,59,90,…,334)  Post condition: S=D+mon[M] Question: Is it a better solution? How do we express the precondition?

41 Programming Fundamentals End of 3 rd lecture


Download ppt "Programming Fundamentals 3 rd lecture Szabolcs Papp."

Similar presentations


Ads by Google