Upload
andrei-rotaru
View
60
Download
0
Tags:
Embed Size (px)
Citation preview
Pseudocode
Pseudo-code is structured English that states the steps to the problem solution1. Statements are written in simple English
2. Each instruction/step is written on a separate line
3. Keywords and indentation are used to signify particular control structures
4. Each set of instructions is written from top to bottom, with only one entry and one exit
5. Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules.
Pseudocode
real a,b,xbegin
input a,bif a=0
then if b=0 then output “Equation has infinite solutions” else output “Equation doesn’t have solution” end if
else z b/a output ‘’Solution of equation is ‘’, z
end ifend
Sample Pseudocode
The theorem states that it is possible to write any computer program by using only three basic control structures
These control structures are:◦Linear structure◦Selection or conditional structure
◦Repetitive Structure (Iterative)
Structure Theorem
Input 3 numbers a,b,c and find the arithmetic average of a and b, and arithmetic average of b and c.
integer a,b,creal m1, m2Begin input a,b,c m1 (a+b)/2 m2 (b+c)/2 output m1, m2End
Linear Structure
Input the length of sides and height of a triangle and calculate the area an perimeter.
Problem for Linear Structure
In this structure according to the condition program selects from two or more actions.
There are two types of selection structure:Simple selection structureGeneral selection structure
Selection or conditional structure
In this type of selection structure program selects from two alternatives depending on the given condition.For example: If sales decreased lower the price by %5. If angle is 90o , it’s a right angle. If all sides of a triangle are equal, it’s an equal-sided triangle. If all sides of a quadrilateral are equal and angles are 90o , it’s a square.
Simple Selection Structure
Input a number n by keayboard and calculate the inverse of this number.Condition: If n≠0 then inv=1/n otherwise inv=0
integer nreal invbegin input n if n<>0 then
inv 1/n else
inv 0 end if output invend
Example
Input a number a and output the absolute value of a.Condition:
integer abegin input a if a<0 then
a -a end if output aend
Example
In this structure selections is made from many alternatives depending on the set of values for condition.
If the symbol between two operand is +, add the operands; if it’s -, subtract the second operand from first one; if it’s / divide the first operand by the second; if it’s * multiply the operands; if it’s something else expression is wrong.
General selection structure
Input integer numbers n, a, b and c by keyboard. Calculate the value of e defined as:
(a+b)/c for n=1E= (b+c)/a for n=2 (c+a)/b for n=3
Example
integer n,a,b,creal ebegin input n, a, b, c if n=1 then e(a+b)/c else if n=2
then e(b+c)/aelse if n=3
then e(c+a)/b end if
end if end if output eend
integer n,a,b,creal ebegin input n, a, b, c in case of n case 1: e(a+b)/c case 2: e(b+c)/a case 3: e(c+a)/b end of case output eend
Case of selection method
In this type of structure an action or sequence of actions are repeated while a condition is true or for a definite number of cycles.
We can use two kinds of repetitive structures. Repetitive structure with known number of cycles. Repetitive structure with unknown number of cycles.
Repetitive Structure (Iterative)
If we know the number of repeats, we need to use two variables. A counter variable which will count the number of repeats and a variable which has the total number of repeats.
Usage:for c iv, fv [by v] execute
actionsend for
Repetitive structure with known number of cycles.
c: Counteriv: Initial valuefv: Final value
Input n numbers and find the sum of these numbers.
integer i,nreal s,abegin input n s0 for i1, n execute input as s+a end for output send
Example
1. Input two integer numbers m and n and calculate nm.
2. Input n integer numbers and find the number of negative numbers.
3. Input n numbers and find the arithmetic average.
4. Output the first n numbers which is divisible by 5.
5. Input n integer numbers and find the sum of even numbers and sum of odd numbers.
Problems
Depending on where the condition is checked there are two types of repetitive structures with unknown number of cycles.
Repetitive structure with prior condition Repetitivie structure with post condition.
Repetitive structures with unknown number of cycles
In this type of structure the condition is checked before executing the cycle block.
Usage:While condition do
actionsendwhile
Repetitive structure with prior condition
In this type of structure the condition is checked after executing the cycle block.
Usage:repeat actionsuntil condition
Repetitive structure with post condition
real s,abegin
s 0input awhile a<>0 do
s s+ainput a
endwhileoutput s
end
Examples
real s,abegin
s 0input arepeat
s s+ainput a
until a=0output s
end
1. Input numbers until you input zero and find the number of positive and negative numbers.
2. Input integer numbers until you input zero and calculate the arithmetic average of the numbers.
3. Input a number n and output all the positive numbers smaller than n and divisible by 3.
4. Input integer numbers until you input 0 and find the sum of even and odd numbers.
Problems
1. Write the pseudocode to calculate an arithmetic expression. You will input two operands and an operator and computer should calculate the result.
2. Input a person’s birth year to computer and it should calculate the age and according to the age it should show a message thatif age is 0-2 “Baby”
3-12 “Child” 13-18 “Teenager” 19-50 “Adult” 51- “Old”
3. Write the pseudocodes to calculate the sum of following sequences.
a. S=1x3+2x5+3x7+.................+n x (2n+1)b. S=1+1x2+1x2x3+.................+1x2x3x.......xnc. S=12 - 22 + 32 – 42 + ..............+(-1)n+1 x n2
Pseudocode problems
Exchanging the values of two variables Finding the minimum and maximum
numbers Processing the digits of a number Finding the greatest common divisor of two
numbers Showing the prime numbers Finding the divisors of a number Convertion of number bases Generating recurrent series
Elementary Algorithms
First method: Exchanging the values of two variables by using a third variable. If we have the variables a and b to exchange values, let’s use a variable x.
Pseudocode:real a,b,xbegin input a,b, x a; a b; b x output a,bend
Algortihm for exchanging values
Second method: Exchanging the values of two variables without third variable.
Pseudocode:real a,bbegin input a,b a a-b; b a+b; a b-a output a,bend
Input a 3 digits number and output the minimum number which should be formed by using the digits of that number.For example, if we input 312 the output number must be 123.You should follow these steps for solving this problem:Step 1. Extract the digits of the numberStep 2. Order the number as ascendingStep 3. Form the new number from these ordered digits.
Case study
integer n, a, b, c, xbegin input n a n div 100; b (n div 10) mod 10; c n mod 10 if a>b then xa; ab; bx; end if if b>c then xb; bc; cx; end if if a>b then xa; ab; bx; end if n a*100+b*10+c output nend
Algorithm for finding minimum and maximum valuesFirst variant: Input n numbers and output the maximum number.
real a,max,n,ibegin input n,a; maxa for i2, n do input a
if a>max then maxaend if
end for output maxend
Second variant: Input numbers until you input zero and output the maximum number.
real a,maxbegin input a; maxa while a<>0 do if a>max
then maxa end if input a end while output maxend
Input n numbers and output the maximum and number of occurence.
Step 1. Input the first number aStep 2. Assign the value of a to max, maxa and set the counter to 1, k1Step 3. Input the next value of aStep 4. If a=max then increase the counter k, kk+1Step 5. If a>max then assign the value of a to the max, maxa and initialize the counter to 1, k1Step 6. If there are more numbers to input go to step 3.
Case Study
integer a, max, n, i, kbegin input n, a max a; k 1 for i 2, n do input a if a=maxthen k k+1else if a>max then max a; k 1 end if end if end for output max, kend.
Pseudocode
1. Input n numbers and output the maximum and minimum numbers.
2. Input numbers until you input 0, and output the minimum and maximum.
3. In a contest there are n members in the evaluation comision. Write the algorithm to calculate the average score excluding the maximum and minimum scores.
Problems
For processing the digits of a number you can use the following algorithms:• Algorithm for extracting the digits of a number•Algorithm to form a number from digits•Agorithm for inversing the digits of a number
Processing the digits of a number
Step 1. Extract the right most digit using c n mod 10
Step 2. Output or process the digitStep 3. Remove the digit you extracted using
n n div 10Step 4. If n<>0 go to step 1
Extracting the digits of a number
integer n, cbegin input n while n<>0 do c n mod 10 output c n n div 10 end whileend
Pseudocode
1. Input a number n and output the sum and product of it’s digits.
2. Input numbers until you input 0 and check pairs of consecutive numbers, if the sum of the digits of first number is equal to the second number then output the pairs.
Example problems
integer n, s, pbegin input n s 0; p 1 while n<>0 do s s + n mod 10 p p * (n mod 10) n n div 10 end while output s, pend.
Problem 1
integer a, b, s, xbegin input a, b while b<>0 do s 0; x a while x<>0 do s s + x mod 10 x x div 10 end while if s=b then output a, b end if a b input b end whileend.
Problem 2
integer d, nrbegin nr 0 input d while d>=0 and d<=9 do
nr nr*10 + dinput d
end while output nrend
Forming a number from digits
integer n, invbegin input n inv 0 while n<>0 do
inv inv*10 + n mod 10n n div 10
end while output invend
Inversing a number
Input a number n, and check if the number is palindrome. (A palindrome number is a number which equals to it’s invers, ex. 12321 )
Problem
integer n, nr, invbegin input n nr n; inv 0 while n<>0 do inv inv*10 + n mod 10; n n div 10 end while if nr=inv then output “The number is palindrome” else output “The number is not palindrome” end ifend.
Input n numbers and output the biggest digit of each number.Ex. If n=5 and you input numbers:12345, 2354, 29875, 12312, 56785 The output : 5, 5, 9, 3, 8
Problem
integer n, i, a, maxbegin input n for i1, n do input a max a mod 10; a a div 10 while a<>0 do
if a mod 10 > max then max a mod 10end ifa a div 10
end while output max end forend