Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals 4 th lecture Szabolcs Papp.

Similar presentations


Presentation on theme: "Programming Fundamentals 4 th lecture Szabolcs Papp."— Presentation transcript:

1 Programming Fundamentals 4 th lecture Szabolcs Papp

2 ELTE 2/  Text type Text type  Types of problem: array in, array out text in, text out array in, single value out single value in, array out  Examples Examples Content

3 ELTE 3/ Text type Text type and array  similarities: o consist of same typed elements, o can be indexed;  differences: o the array is parameterized with the type of its elements (and index), whilst text always contains characters, o in an algorithm, text index always starts by 1, but array start index depends on declaration, o array length is constant, but text length can vary o in case of text we have length() function and + operator

4 ELTE 4/ Text type (C++)  Most important string operations ( string a; ): o …""… //empty text; e.g.: a=""; o cin >> a;//read until space or endline o getline(cin,a,’\n’); //read until ’\n’ o getline(cin,a,’x’); //read until ’x’ char o …a.length()… //number of characters o …a.size()… //=a.length() o …+… //concatenate o …a[i]… //i th character, 0  i

5 ELTE 5/ Text type (C++)  Most important string operations ( string a; ) o …a.find(what)… //location of what in a o …a.substr(from,to)… //=a[from..to] o a.replace(from,cnt,what); //replace 'cnt' //chars substring starting at 'from' with 'what' o Related character operations ( string a; ) o …isalpha(a[i])… //a[i] ’a’..’z’, // ’A’..’Z’? o …isdigit(a[i])… //a[i] ’0’..’9’? o …isupper(a[i])… //a[i] ’A’..’Z’? o …islower(a[i])… //a[i] ’a’..’z’? o …tolower(a[i])… //convert a[i] to lower //case o …toupper(a[i])… // convert a[i] to upper //case

6 ELTE 6/ Text type Problem: Let's reverse the order of letters in a word (text)! Specification:  Input: S:Text  Output: T:Text  Precondition: – lengthlength lengthlength  Post condtion: length(T)=length(S) and  i (1  i  length(S)): T[i]=S[length(S)–i+1] Predefined function: length length length: Text  Integer length(s):=number of characters in s

7 ELTE 7/ Alogirthm: We cannot modify i th character of a text, if text length is less then i. We need +. or Text type T:= "" i=length(S)..1; by -1 T:=T+S[i] T:= "" i=1..hossz(S) T:=S[i]+T

8 ELTE 8/ Text type Problem: Let's define the initials of an English name (e.g. James Black  JB)! Specification:  Input: Name:Text  Output: Ini:Text  Precondition: IsCorrectName(Name) Comment: a name is correct, if all the initials are capital letters, but only the initials… we assume there exists „IsCorrectName: Text  Boolean” function.

9 ELTE 9/ Text type  Post condition: and  i (1  i  length(Ini)): IsCapital(Ini[i]) and Ini  Text  We assume that there exists 1. „IsCapital:Character  Boolean” function 2. „  ” operator, representing the subsequence operation (x  y is true if we can get x by eliminating some characters in y).

10 ELTE 10/ Text type Algorithm: Ini:= "" i=1..length(Name) IsCapital(Name[i]) Ini:=Ini+Name[i]─ Y N

11 ELTE 11/ Text type Problem: Let's define the initials of a Hungarian name (e.g. Szabó Éva  SzÉ)! Specification:  Input: Name:Text  Output: Ini:Text  Precondition: IsCorrectName(Name)  Post condition: … homework … Problem: in Hungarian we have double letters, where the second one is not capital.

12 ELTE 12/ Text type Idea for solution: Constant Double:Array[1..8:Text]= (”Cs”,”Dz”,”Gy”,”Ly”,”Ny”, ”Sz”,”Ty”,”Zs”) Draft plan for solution: Let's define all two-character parts of Name, then check which one is part of Double array, or starting with a capital letter…

13 ELTE 13/ Text type Algorithm: Can a name be „Nagy A”? Is it an optimal solution? Ini:= "" i=1..length(Name)–1 K:=Name[i]+Name[i+1] K  Double Ini:=Ini+KIsCapital(Name[i]) Ini:=Ini+ Name[i]  Y N Y N

14 ELTE 14/ Comparing characters Problem: We have two letters, let's decide which one precedes the other in the English alphabetical order! Specification:  Input: A,B:Character  Output: APrecedes:Boolean  Precondition: IsLetter(A) and IsLetter(B)  Post condition: APrecedes=A  E B  Definition: x  E y if and only if ???  We suppose:  IsLetter:Character  Boolean function

15 ELTE 15/ Comparing characters Idea for solution: Let's store all the characters in the right order, and if we find A preceding to B in this array, then output is true.  Definition: Constant Letters:Array[1..2*35:Character]= (”a”,”A”,”b”,”B”,…,”z”,”Z”) x  E y if and only if i  j : x=Letters[i] and y=Letters[j] We can have another solution based on text: Constant Letters:Text=”aAbB…zZ”Text Which one is better?

16 ELTE 16/ Comparing characters Algorithm: What if precondition would not be met? i:=1 Letters[i]≠A i:=i+1 j:=1 Letters[j]≠B j:=j+1 APrecedes:=i≤j

17 ELTE 17/ Comparing texts Problem: We have two words, let's decide which one precedes the other in the English alphabetical order! Specification:  Input: A,B:Text  Output: APrecedes:Logical  Precondition: IsWord(A) and IsWord(B)  Post condition: APrecedes=A  B  We suppose:  IsWord:Text  Boolean function

18 ELTE 18/ Comparing texts  Definition (alphabetical sort): (1) A word precedes another word, if its first different letter precedes the other word's same position letter; (2) the  empty word precedes all other words.  Examples: ”apple” ”app”  ”l…”> E  ”apple”≤”apple”   ≤ E  (1) (2)

19 ELTE 19/ Algorithm: For comparing characters, we can use the solution from the previous problem. Comparing texts i:=1 i≤length(A) and i≤length(B) and A[i]=B[i] i:=i+1 APrecedes:=(i>length(A)) or (i≤length(B) and A[i]  E B[i])

20 ELTE 20/ Array in, array out Problem: We have midday temperature measures from the previous N days (in Celsius). Let's define which where positive or 0 (+), and which where negative measures! Specification:  Input: N:Integer; T:Array[1..N:Integer]  Output: S:Array[1..N:Character]  Precondition: N  0  Post cond.:  i(1  i  N): T[i]  0  S[i]="+" and T[i]<0  S[i]="–"

21 ELTE Array in, array out Algorithm: For coding: We can declare the output array as soon as we know the length of the input array. I N 21/ i=1,…,N T[i]  0 S[i]:=”+”S[i]:=”–”

22 ELTE Array in, array out Problem: We have midday temperature measures from the previous N days (in Celsius). Let's calculate the average temperature for all 7- days periods! Specification:  Input: N:Integer; T:Array[1..N:Integer]  Output: A:Array[1..N–6:Real]  Precondition: N  7  Post cond.:  i(1  i  N–6): Comment: By knowing the previous average, we can calculate the next average in a simpler way: A[i]:=(7*A[i–1]–T[i–1]+T[i+6])/7 22/

23 ELTE Array in, array out S:=0 i=1,…,7 S:=S+T[i] A[1]:=S/7 i=2,…,N –6 S:=S–T[i–1]+T[i+6] A[i]:=S/7 Algorithm: 23/

24 ELTE Single value in, array out Problem: Let's define the first N member of the arithmetic sequence which starts by A and steps by B! Specification 1 :  Input: A,B,N:Integer  Output: S:Array[1..N:Egész]  Precondition: N  0  Post cond.:  i (1≤i≤N): S[i]=A+(i – 1)*B Algorithm 1 : i=1,…,N S[i]:=A+(i–1)*B 24/

25 ELTE Single value in, array out We can define post condition in a simpler way (recursive). Specification 2 :  Input: A,B,N:Integer  Output: S:Array[1..N:Integer]  Precondition: N  0  Post condition: S[1]=A és  i (2≤i≤N): S[i]=S[i–1]+B Algorithm 2 : 25/ S[1]:=A i=2,…,N S[i]:=S[i–1]+B

26 ELTE Array in, single value out Problem: We flew by airplane from Europe to America. We measured the height over sea (  0) by X kilometers. We measured 0 where there is sea, and >0 where there is land. Let's define the two sea sides of the ocean! 26/

27 ELTE Array in, single value out Specification:  Input: N:Integer, Heig:Array[1..N:Integer]  Output: E,A:Integer  Precondition: Heig[1]>0 és Heig[N]>0 and  i(1

28 ELTE Array in, single value out E:=2 Heig[E]>0 E:=E+1 A:=N–1 Heig[A]>0 A:=A–1 Algorithm: Idea: start both from the beginning and from the end of the array. 28/

29 ELTE Further examples Problem: We flew by airplane from Europe to America. We measured the height over sea (  0) by X kilometers. We measured 0 where there is sea, and >0 where there is land. Let's find the islands! 29/

30 ELTE Further examples Specification:  Input: N:Integer, Heig:Array[1..N:Ingeter]  Output: Cnt:Integer, S,F:Array[1..Db:Integer]  Precondition: Heig[1]>0 and Heig[N]>0 and  i(1

31 ELTE Further examples  Definition: IsIslandStart:[2..N]  Boolean IsIslandStart(i):=Heig[i]>0 and Heig[i–1]=0 IsIslandEnd:[1..N–1]  Boolean IsIslandEnd(i):=Mag[i]>0 and Heig[i+1]=0 Question: What about costs at Europe and America? Solution: Instead of 2 and N–1, we search for islands only between E and A values returned by the algorithm of the previous excercise. 31/

32 ELTE Further examples Specification:  Input: N:Integer, Heig:Array[1..N:Integer]  Output: Cnt:Integer, S,F:Array[1..Cnt:Integer]  Precondition: Heig[1]>0 and Heig[N]>0 and  i(1

33 ELTE Further examples … calculate E and A …calculate E and A Cnt:=0 i=E,…,A IsIslandStart(i) Cnt:=Cnt+1  S[Cnt]:=I IsIslandEnd(i) E[Cnt]:=i  Algorithm: Let's take advantage of the fact the starts and ends of islands can occur only in turn. Y N Y N 33/

34 ELTE Further examples For coding: Problem: The length of output array will be determined only during processing, but meanwhile we also have to collect its elements. Solution 1 : We previously define the array S and E with a maximum number of elements (MaxCnt) (see 3 rd lecture) 3 rd lecture Soltution 2 : We differ from the algorithm: first we determine Cnt, then collect the elements of S and E. 34/

35 ELTE Further examples Problem: There are measurements for water level at N places of a river, which are given in centimeters relative to a reference level. We need to command a first instance flood protection preparedness, if the height exceeds 800 cms, second instance, if the height exceeds 900 cms, and third instance, if the height exceeds 10 meters. We have flood on those river sections where all the consecutive measurements imply at least first instance flood protection level. Let's count how many river sections there are with flood! 35/

36 ELTE Further examples Specification:  Input: N:Integer, F:Array[1..N:Integer]  Output: Cnt:Integer  Precondition: N  0  Post condition: Cnt= 36/

37 ELTE Algorithm 1 : Comment: There are as many floods as count of "flood starts". Further examples F[1]  800 Cnt:=0Cnt:=1 i=1,…,N–1 F[i]  800 and F[i+1]>800 Cnt:=Cnt+1  YN Y N 37/

38 ELTE Further examples Algorithm 2 : There are as many floods as count of "flood ends". F[N]  800 Cnt:=0Cnt:=1 i=2,…,N F[i]  800 and F[i-1]>800 Cnt:=Cnt+1  NI N I 38/

39 ELTE Further examples Problem: There are measurements for water level at N places of a river, which are given in centimeters relative to a reference level. … Let's define the length of all the floods! We assume that we don't have floods at the first and the last measurements.… 39/

40 ELTE Further examples Specification:  Input: N:Input, F:Array[1..N:Integer]  Output: Cnt:Integer, L:Array[1..Cnt:Integer]  Precondition: N  1 and F[1]  800 and F[N]  800  Post cond.: Cnt= and  i (1≤i≤Cnt): L[i]=floodstart[i]–floodend[i]+1  Definition: floodstart:Array[1..Cnt:Integer] … floodend:Array[1..Cnt:Integer] … 40/

41 ELTE Further examples Cnt:=0 i=1,…,N–1 F[i]  800 and F[i+1]>800 Cnt:=Cnt+1  L[Cnt]:=0 F[i]>800 L[Cnt]:=L[Cnt]+1  Algorithm: Idea: we reset the counter at flood start, and increment at each flood measurements. Y N Y N 41/

42 ELTE Further examples Problem: We visit horseraces and bet. Over N days we register how much we win (  0) or how much we loose (<0). Let's suppuse we have X money in the beginning. Which is the first day when we had to borrow money? 42/

43 ELTE Further examples Specification:  Input: N,X:Integer, P:Array[1..N:Integer]  Output: Exists:Boolean, Day:Integer  Precondition: N  0 and X  0  Post cond.: Exists=  i(1  i  N): and Exists  ( 1  Day  N and and  i(1  i

44 ELTE Further examples S:=X i:=1 i  N and S+P[i]>0 S:=S+P[i] i:=i+1 Exists:= i  N Exists Day:=i  Algorithm: Y N 44/

45 Programming Fundamentals End of 4 th lecture


Download ppt "Programming Fundamentals 4 th lecture Szabolcs Papp."

Similar presentations


Ads by Google