Sie sind auf Seite 1von 42

Pseudo-code is structured English that states the steps to the problem solution

1. 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.

real a,b,x begin input a,b if a=0 then if b=0 then output Equation has infinite solutions else output Equation doesnt have solution end if else z b/a output Solution of equation is , z end if end

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)

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,c real m1, m2 Begin input a,b,c m1 (a+b)/2 m2 (b+c)/2 output m1, m2 End

Input the length of sides and height of a triangle and calculate the area an perimeter.

In this structure according to the condition program selects from two or more actions. There are two types of selection structure: Simple selection structure General selection 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 , its a right angle. If all sides of a triangle are equal, its an equal-sided triangle. If all sides of a quadrilateral are equal and angles are 90o , its a square.

Input a number n by keayboard and calculate the inverse of this number. Condition: If n0 then inv=1/n otherwise inv=0 integer n real inv begin input n if n<>0 then inv 1/n else inv 0 end if output inv end

Input a number a and output the absolute value of a. Condition: integer a begin input a if a<0 then a -a end if output a end

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 its -, subtract the second operand from first one; if its / divide the first operand by the second; if its * multiply the operands; if its something else expression is wrong.

Input integer numbers n, a, b and c by keyboard. Calculate the value of e defined as: (a+b)/c for n=1 E= (b+c)/a for n=2 (c+a)/b for n=3

integer n,a,b,c real e begin input n, a, b, c if n=1 then e(a+b)/c else if n=2 then e(b+c)/a else if n=3 then e(c+a)/b end if end if end if output e end

integer n,a,b,c real e begin 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 e end

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.

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 actions end for

c: Counter iv: Initial value fv: Final value

Input n numbers and find the sum of these numbers. integer i,n real s,a begin input n s0 for i1, n execute input a s s+a end for output s end

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.

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.

In this type of structure the condition is checked before executing the cycle block. Usage: While condition do actions endwhile

In this type of structure the condition is checked after executing the cycle block. Usage: repeat actions until condition

real s,a begin s 0 input a while a<>0 do s s+a input a endwhile output s end

real s,a begin s 0 input a repeat s s+a input a until a=0 output 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.

1.

2.

3.
a. b. c.

Write the pseudocode to calculate an arithmetic expression. You will input two operands and an operator and computer should calculate the result. Input a persons birth year to computer and it should calculate the age and according to the age it should show a message that if age is 0-2 Baby 3-12 Child 13-18 Teenager 19-50 Adult 51Old Write the pseudocodes to calculate the sum of following sequences.
S=1x3+2x5+3x7+.................+n x (2n+1) S=1+1x2+1x2x3+.................+1x2x3x.......xn S=12 - 22 + 32 42 + ..............+(-1)n+1 x n2

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

First method: Exchanging the values of two variables by using a third variable. If we have the variables a and b to exchange values, lets use a variable x. Pseudocode: real a,b,x begin input a,b, x a; a b; b x output a,b end

Second method: Exchanging the values of two variables without third variable. Pseudocode: real a,b begin input a,b a a-b; b a+b; a b-a output a,b end

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 number Step 2. Order the number as ascending Step 3. Form the new number from these ordered digits.

integer n, a, b, c, x begin 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 n end

First variant: Input n numbers and output the maximum number.

real a,max,n,i begin input n,a; maxa for i2, n do input a if a>max then maxa end if end for output max end

Second variant: Input numbers until you input zero and output the maximum number.

real a,max begin input a; maxa while a<>0 do if a>max then maxa end if input a end while output max end

Input n numbers and output the maximum and number of occurence. Step 1. Input the first number a Step 2. Assign the value of a to max, maxa and set the counter to 1, k1 Step 3. Input the next value of a Step 4. If a=max then increase the counter k, kk+1 Step 5. If a>max then assign the value of a to the max, maxa and initialize the counter to 1, k1 Step 6. If there are more numbers to input go to step 3.

integer a, max, n, i, k begin input n, a max a; k 1 for i 2, n do input a if a=max then k k+1 else if a>max then max a; k 1 end if end if end for output max, k end.

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.

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

Step 1. Extract the right most digit using c n mod 10 Step 2. Output or process the digit Step 3. Remove the digit you extracted using n n div 10 Step 4. If n<>0 go to step 1

integer n, c begin input n while n<>0 do c n mod 10 output c n n div 10 end while end

1. Input a number n and output the sum and product of its 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.

integer n, s, p begin 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, p end.

integer a, b, s, x begin 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 ab input b end while end.

integer d, nr begin nr 0 input d while d>=0 and d<=9 do nr nr*10 + d input d end while output nr end

integer n, inv begin input n inv 0 while n<>0 do inv inv*10 + n mod 10 n n div 10 end while output inv end

Input a number n, and check if the number is palindrome. (A palindrome number is a number which equals to its invers, ex. 12321 )
integer n, nr, inv begin 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 if end.

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
integer n, i, a, max begin 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 10 end if a a div 10 end while output max end for end

Das könnte Ihnen auch gefallen