Post on 19-Jan-2016
Programming Fundamentals
3rd lecture
Szabolcs Papp
ELTEELTE
23.04.21.23.04.21. 22/41/4123.04.21.23.04.21.
Loops –
specification+algorithm+codingFirst example for arraysThe arrayArray instead of conditional statement
Constant arrays
Content
ELTEELTE
23.04.21.23.04.21. 33/41/4123.04.21.23.04.21.
Loops
Problem:Let’s calculate the non-1 least divisor of and integer (N>1)!
Specification:Input: N:IntegerOutput: D:IntegerPrecondition: N>1Post condition: 1<DN and D|
N and i (2i<D): i ł N
ELTEELTE
23.04.21.23.04.21. 44/41/4123.04.21.23.04.21.
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:=2i ł Ni:=i+1
D:=i
ELTEELTE
23.04.21.23.04.21. 55/41/4123.04.21.23.04.21.
Loops
Problem:Let’s calculate both the non-1 least and the non-N greatest divisor of an integer (N>1)!
Specification:Input: N:IntegerOutput: LD,GD:IntegerPrecondition: N>1Post condition: 1<LDN and
1GD<N andLD|N andi (2i<LD): i ł N andGD|N andi (GD<i<N): i ł N
ELTEELTE
23.04.21.23.04.21. 66/41/4123.04.21.23.04.21.
Loops
Note:By knowing LD, GD can be definied in post condition in another way: LD*GD=N!
Algorithm expressing this:i:=2
i ł Ni:=i+1
LD:=IGD:=N Div LD
ELTEELTE
23.04.21.23.04.21. 77/41/4123.04.21.23.04.21.
Loops
Problem: Let’s calculate the non-1 and non-N least divisor of and integer (N>1), if exists!
Specification:Input: N:IntegerOutput: D:Integer,
EXISTS:BooleanPrecondition: N>1Post condition: EXISTS=i (2i<N): i|
N andEXISTS D|N and2D<N andi (2i<D): i ł N
ELTEELTE
23.04.21.23.04.21. 88/41/4123.04.21.23.04.21.
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:=2i<N and i ł
Ni:=i+1
EXISTS:=i<N
EXISTSD:=i
Ni
I N 2i N Div i N Div 2
thusi*i N
thusi N
ELTEELTE
23.04.21.23.04.21. 99/41/4123.04.21.23.04.21.
Loops
Problem:Let’s calculate the sum of all the divisors of an integer (N>1)!
Specification:Input: N:IntegerOutput: S:IntegerPrecondition: N>1Post condition:
N
Ni
1i
iS
ELTEELTE
23.04.21.23.04.21. 1010/41/4123.04.21.23.04.21.
Loops
Algorithm:
S:=0i=1..N
i|NS:=S+i
I N
ELTEELTE
23.04.21.23.04.21. 1111/41/4123.04.21.23.04.21.
Loops
Problem: Let’s calculate the sum of all odd divisors of an integer (N>1)!
Specification:Input: N:IntegerOutput: S:IntegerPrecondition: N>1Post condition: S=
N
i
i
odd(i) and Ni
1
ELTEELTE
23.04.21.23.04.21. 1212/41/4123.04.21.23.04.21.
Algorithm1:
Algorithm2:
Loops
S:=0i=1..N
i|N és odd(i)S:=S+i
S:=0i=1..N; by 2
i|NS:=S+i
I N
I N
ELTEELTE
23.04.21.23.04.21. 1313/41/4123.04.21.23.04.21.
Loops
Problem:Let’s calculate the sum of all prime divisors of an integer (N>1)!
Specification:Input: N:IntegerOutput: S:IntegerPrecondition: N>1Post condition: S=
isprime(i)???
N
isprime(i)andNi
2i
i
ELTEELTE
23.04.21.23.04.21. 1414/41/4123.04.21.23.04.21.
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:=2iN
i|NS:=S+i
i|NN:=N Div i
i:=i+1
I N
ELTEELTE
23.04.21.23.04.21. 1515/41/4123.04.21.23.04.21.
Loops
Problem:Let’s determine the count of i,j (i,j>1) integer pairs, where i*j<N!
Specification:Input: N:IntegerOutput: S:IntegerPrecondition: N>1Post condition: S=
2 Div N
2i
2 Div N
Nji2j
1
ELTEELTE
23.04.21.23.04.21. 1616/41/4123.04.21.23.04.21.
Loops
S:=0i=2..N Div 2
j=2..N Div 2i*j<N
S:=S+1
Algorithm1:
I N
ELTEELTE
23.04.21.23.04.21. 1717/41/4123.04.21.23.04.21.
Loops
S:=0i=2..N Div 2
j:=2i*j≤N
S:=S+1j:=j+1
Algorithm2:
Algorithm3:S:=0
i=2..N Div 2S:=S+(N Div i)-1
ELTEELTE
23.04.21.23.04.21. 1818/41/4123.04.21.23.04.21.
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.
ELTEELTE
23.04.21.23.04.21. 1919/41/4123.04.21.23.04.21.
Loopsalgorithm – code
Conditional loop:
For loop:
conditionstatement
while (condition){ statements}
i=1..Nstateme
nt
for (int i=1;i<=N;i++){ statements}
i=1..N; by xstatement
for (int i=1;i<=N;i+=x){ statements}
statement
condition
do{ statement}while (condition)
Typical usage at Typical usage at console inputsconsole inputs
((seesee 2 2ndnd lecture lecture))
ELTEELTE
23.04.21.23.04.21. 2020/41/4123.04.21.23.04.21.
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 yearso 3,4,13,14,…,53,54: red yearso 5,6,15,16,…55,56: yellow yearso 7,8,17,18,…57,58: white yearso 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)!
ELTEELTE
23.04.21.23.04.21. 2121/41/4123.04.21.23.04.21.
Conditional statement
or do we need something
else?Specification1: 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
ELTEELTE
23.04.21.23.04.21. 2222/41/4123.04.21.23.04.21.
Conditional statement
or do we need something
else?Specification2: 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
ELTEELTE
23.04.21.23.04.21. 2323/41/4123.04.21.23.04.21.
Conditional statement
or do we need something
else?
y:=((year-1984) Mod 10) Div 2y=0 y=1 y=2 y=3 y=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:
ELTEELTE
23.04.21.23.04.21. 2424/41/4123.04.21.23.04.21.
Arrays
Related concepts: Sequence: a countable sequence of
same typed data elements Array: finite length sequence, we can
define operations for ith 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).
ELTEELTE
23.04.21.23.04.21. 2525/41/4123.04.21.23.04.21.
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]
ELTEELTE
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”)
23.04.21.23.04.21. 2626/41/4123.04.21.23.04.21.
Arrays(in algorithm and code)
string Y[5]
const string Color[5]={"green","red", "yellow","white","black"};
int X[N+1]
Tömb-Tömb-elemszámelemszám
indexelésindexelés 0 0..N..N
indexindex 0 0..??? ..??? count of count of elementselements
in C++:
ELTEELTE
23.04.21.23.04.21. 2727/41/4123.04.21.23.04.21.
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 This is a difference difference between usual between usual
algorithm and code!algorithm and code!
ELTEELTE
23.04.21.23.04.21. 2828/41/4123.04.21.23.04.21.
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 Note that syntax is different in case of different in case of
identifiers and types!identifiers and types!
ELTEELTE
23.04.21.23.04.21. 2929/41/4123.04.21.23.04.21.
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. …
ELTEELTE
23.04.21.23.04.21. 3030/41/4123.04.21.23.04.21.
Array input (with check):
bool err; //is there an error? … do{ cout << "Count:"; cin >> N; err=!!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err);
bool err; //is there an error? … do{ cout << "Count:"; cin >> N; err=!!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err);
IsCorrectN does both IsCorrectN does both syntax and semantic syntax and semantic
checkcheck
Arrays (C++ code – overview)
In algorithmIn algorithm::InputInput:: N N [[IsCorrectIsCorrectNN(N))(N))]]
ELTEELTE
Array input (with check):
Let’s get back to our question…
bool err; //is there an error? … do{ cout << "Count:"; cin >> N; err=!!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err);
bool err; //is there an error? … do{ cout << "Count:"; cin >> N; err=!!IsCorrectN(N); if (err) { cout << „error message" << endl; } }while (err);
23.04.21.23.04.21. 3131/41/4123.04.21.23.04.21.
for (intfor (int i i==00;;ii<N<N;;++i++i)){{ do{ do{ cout << cout << ii+1+1 << << ""..:":"; cin >> ; cin >> v[i]v[i];; errerr=!=!CorrectCorrect(v[i])(v[i]);; if ( if (errerr)) { { cout << " cout << "hibaüzenethibaüzenet" << endl;" << endl; }; }; }while ( }while (hibahiba););}}
for (intfor (int i i==00;;ii<N<N;;++i++i)){{ do{ do{ cout << cout << ii+1+1 << << ""..:":"; cin >> ; cin >> v[i]v[i];; errerr=!=!CorrectCorrect(v[i])(v[i]);; if ( if (errerr)) { { cout << " cout << "hibaüzenethibaüzenet" << endl;" << endl; }; }; }while ( }while (hibahiba););}}
In algorithmIn algorithm::InputInput:: v(1..N) v(1..N) [[HelyesHelyes(v(1..N))(v(1..N))]]
Correct does both Correct does both syntax and semantic syntax and semantic
checkcheck
Arrays (C++ code – overview)
ELTEELTE
23.04.21.23.04.21. 3232/41/4123.04.21.23.04.21.
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]
ELTEELTE
23.04.21.23.04.21. 3333/41/4123.04.21.23.04.21.
Array instead of conditional statement
Algorithm:
y:=((year-1984) Mod 10) Div 2c:=Colors[y]
ELTEELTE
23.04.21.23.04.21. 3434/41/4123.04.21.23.04.21.
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: 1N99
This is a This is a better place better place
here.here.Constant Constant
array is an array is an output from output from the point of the point of view of the view of the algorithm.algorithm.
ELTEELTE
23.04.21.23.04.21. 3535/41/4123.04.21.23.04.21.
Applying constant arrays
Post condition: N<20 S=ones[N] and
N≥20 S=tens[N Div 10]+ ones[N Mod 10]
Algorithm:
N<20 otherwhiseS:=ones[N]
S:=tens[N Div 10]+ ones[N Mod 10]
ELTEELTE
23.04.21.23.04.21. 3636/41/4123.04.21.23.04.21.
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: MMonName Post cond.: 1S12 and
MonName[S]=M
ELTEELTE
23.04.21.23.04.21. 3737/41/4123.04.21.23.04.21.
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
ELTEELTE
23.04.21.23.04.21. 3838/41/4123.04.21.23.04.21.
Constant arrays – What do we store?
Problem:We have a non leap year. What is the serial number of a given day (month, day)?
Specification1: 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:
1M
1i
mon[i]DS
ELTEELTE
23.04.21.23.04.21. 3939/41/4123.04.21.23.04.21.
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]
ELTEELTE
23.04.21.23.04.21. 4040/41/4123.04.21.23.04.21.
Constant arrays – What do we store?
Another solution:For each months, let’s store the total number of days preceding the given month!
Specification2: 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?
Programming Fundamentals
End of 3rd lecture