You are on page 1of 7

NATIONAL UNIVERSITY OF COMPUTER & EMERGING SCIENCES KARACHI CAMPUS

INTRODUCTION TO COMPUTERS (CS-102)


TOPIC: ALGORITHM & FLOWCHART
ASSIGNMENT # 2
SOLUTION
Question #1: Draw a flowchart to add up all odd numbers between 0 and 100. Before
ending, print the result of calculation.

STEP 1: Initialize n by 1 and sum by 0


STEP 2: Add value of n in sum
STEP 3: Check;
a) if n is less than 99 then
i) Add 2 in value of n
ii) Move to Step 2
b) otherwise
i) Move to Step 4
STEP 4: Print ‘Sum of all odd numbers between 0 and 100 is:’ sum
STEP 5: Stop

Question #2: Write the logical steps (pseudo-code) to calculate and print the total
number of students receiving 86 and above marks in the final examination. The class
strength is 20. Input of each student includes only marks ranging from 0 to 100. Draw a
flowchart on the logical steps deduced.
STEP 1: Initialize total_students by 0, marks by 0, strength by 1
STEP 2: Print ‘Please enter marks for student #’, strength
STEP 3: Read value in marks
STEP 4: Check;
(a) if (strength is less than or equal to 20) then
i)Move to Step 5
(b)otherwise
ii)Move to Step 7
STEP 5: Check;
(a)if (marks are less than 0) or (marks are greater than 100) then
i)Move to Step 2
(b)otherwise
ii)Move to Step 6
STEP 6: Check;
(a)if (marks are greater than or equals to 86) then
i)Add 1 in value of total_students
ii)Add 1 in strength
iii)Move to Step 2
(b)otherwise
i) Add 1 in strength
ii)Move to Step 2
STEP 7: Print ‘Total students who scored above or equal to 86 marks are:’ total_students
STEP 8: Stop
Question #3: Write the logical steps (pseudo-code) to print the average age of all
students in a class (class strength is not known). The input is name and age of each
student. To continue for the next input ask the user, answered as either yes or no. Draw a
flowchart on the logical steps deduced.

STEP 1: Initialize total_students by 0, total_age by 0, avg_age by 0, age by 0, name by


““, continue by ‘y’
STEP 2: Print ‘Do you want to enter the data of students, please enter y for yes and n for
no’
STEP 3: Read value in continue
STEP 4: Check;
(b) if (continue is equal to ‘y’) then
i)Move to Step 5
(b)otherwise
ii)Move to Step
STEP 5: Print ‘Enter name and age of student’
STEP 6: Read value in name, age
STEP 7: Check;
(a)if (age are less than 0) then
i)Move to Step 5
(b)otherwise
ii)Move to Step 8
STEP 8: Add 1 in total_students
STEP 9: Add age in total age
STEP 10: Move to Step 2
STEP 11: Divide tota_age by total_students and assign the result in avg_age
STEP 12: Print ‘Average age of students who are:’ total_students ‘ in strength is:’,
avg_age
STEP 13: Stop

Question #4: Draw a flowchart for the logical steps (pseudo-code) to find out whether a
given triangle ABC is a right-angle triangle. Assume that the sides of the triangle are
supplied as input. Print the answer as ‘yes’ or ‘no’.

STEP 1: Initialize side1, side2, side3 by 0 and sq_side1, sq_side2, sq_side3 by 0


STEP 2: Print ‘Enter the three sides’
STEP 3: Read value in side1, side 2, side3
STEP 4: Perform square operation on three sides:
(a)Perform multiplication of side1 with itself and assign the result in sq_side1
(b)Perform multiplication of side2 with itself and assign the result in sq_side2
(c)Perform multiplication of side3 with itself and assign the result in sq_side3
STEP 5: Check;
(a)if (sq_side1 is equal to sum of sq_side2 and sq_side3) then
i)Move to Step 6(i)
(b)otherwise check;
1)if (sq_side2 is equal to sum of sq_side1 and sq_side3) then
i)Move to Step 6(i)
(c)otherwise check
1)if (sq_side3 is equal to sum of sq_side1 and sq_side2) then
i)Move to Step 6(i)
(d) otherwise
1)Move to Step 6(iii)
STEP 6:
i)Print ‘Yes it’s a right angle triangle’
ii) Move to Step 7
iii)Print ‘No it is not a right angle triangle’
STEP 7: Stop

Question #5: Draw a flowchart for the logical steps (pseudo-code) to find out whether a
given quadrilateral ABCD is a rectangle. Assume that all the four angles and sides of the
quadrilateral are supplied as input. Print the answer as ‘yes’ or ‘no’.

STEP 1: Initialize angle1, angle2, angle3, angle4, side1, side2, side3 and side4 by 0
STEP 2: Print ‘Enter the four sides’
STEP 3: Read value in side1, side 2, side3, side 4
STEP 4: Print ‘Enter the four angles’
STEP 5: Read value in angle1, angle2, angle3, angle4
STEP 6: Check;
(a)if (angle1 is equal to 90) and (angle2 is equal to 90) and (angle3 is equal to 90) and
(angle4 is equal to 90) then
i)Move to Step 7
(b)otherwise
i)Move to Step 8(iii)
STEP 7: Check;
(a)if (side1 is equal to side2) and (side3 is equal to side4) then
i)Move to Step 8(i)
(b)otherwise check;
1) if (side1 is equal to side3) and (side2 is equal to side4) then
i)Move to Step 8(i)
(c)otherwise check;
1) if (side1 is equal to side4) and (side2 is equal to side3) then
i)Move to Step 8(i)
(d)otherwise
1)Move to Step 8(iii)
STEP 8:
i)Print ‘Yes it’s a rectangle’
ii)Move to Step 9
iii)Print ‘No it is not a rectangle’
STEP 9: Stop
Question #6: Draw a flowchart for the logical steps that converts a number from base 10
to base 2 by using the division-remainder technique.

STEP 1: Initialize value, quotient, remainder, counter by 0


STEP 2: Print ‘Enter the value’
STEP 3: Read value in value
STEP 4: Assign quotient and remainder - value
STEP 5: Check;
(a)if (quotient is equal to 0) then
i) Print ‘0’
ii) Move to Step 9
STEP 6: Check;
(a)if (quotient is equal to 1) then
i) Print ‘1’
ii) Move to Step 9
STEP 7: Check;
(a)if (quotient is greater than or equal to 2) then
i) Move to Step 8
(b) otherwise
i) Move to Step 5
STEP 8: Check;
(a)if (remainder is greater than or equal to 2) then
i) Subtract 2 from remainder
ii) Add 1 in counter
iii)Move to Step 8(a)
(b) otherwise;
i) Print remainder
ii) Assign counter to quotient
iii)Assign quotient to remainder
iv) Initialize counter by 0
v) Move to Step 5
STEP 9: Stop

Question #7: Write the logical steps (pseudo-code) and draw a flowchart that takes two
input values x and n, then find the value of xn using successive multiplication.

Note: For n = 0, x0 = 1

STEP 1: Initialize x, n, y, m, counter by 0 and output by 1


STEP 2: Print ‘Enter x for xn’
STEP 3: Read value in x
STEP 4: Print ‘Enter n for xn’
STEP 5: Read value in n
STEP 5: Check;
(a)if (n is equal to 0) then
i) Print ‘1’
ii) Move to Step
(b) if (x is equals to 0) then
i) Print ‘0’
ii) Move to Step
(c)if(x is less than 0) and (n is greater than 0) then
i) Move to Step 6
(d) if(x is greater than 0) and (n is less than 0) then
i) Move to Step 7
(e) if(x is less than 0) and (n is less than 0) then
i) Move to Step 8
(f) if(x is greater than 0) and (n is greater than 0) then
i) Assign x to y
ii) Assign n to m
iii) Move to Step 9
STEP 6: Perform the following operations:
i) Multiply x by -1 and assign the result to y
ii) Move to Step 9
STEP 7: Perform the following operations:
i) Multiply n by -1 and assign the result to m
ii) Move to Step 9
STEP 8: Perform the following operations:
i) Multiply n by -1 and assign the result to m
ii) Multiply x by -1 and assign the result to y
iii) Move to Step 9
STEP 9: Perform the following operations:
i) Multiply output by y and assign the result to output
ii) Add 1 in counter
iii) If (counter is less than or equals to m)
1) Move to Step 9(i)
iv) Otherwise;
1) Move to Step 10
STEP 10: Check;
(a)if(x is less than 0) and (n is greater than 0) and (n mod (%) 2 is not equal to 0) then
i) Multiply output by -1
ii) Print output
iii) Move to Step 11
(b) if(x is less than 0) and (n is greater than 0) and (n mod (%) 2 is equal to 0) then
i) Print output
iii) Move to Step 11
(c)if(x is greater than 0) and (n is less than 0) then
i) Divide output by 1 and assign the result to output
ii) Print output
iii) Move to Step 11
(d)if(x is less than 0) and (n is less than 0) and (n mod (%) 2 is not equal to 0) then
i) Multiply output by -1
ii) Divide output by 1 and assign the result to output
iii) Print output
iv) Move to Step 11
(e)if(x is less than 0) and (n is less than 0) and (n mod (%) 2 is equal to 0) then
ii) Divide output by 1 and assign the result to output
iii) Print output
iv) Move to Step 11
(f) if(x is greater than 0) and (n is greater than 0) then
i)Print output
ii) Move to Step 11
STEP 11: Stop

Question #8: Write the logical steps (pseudo-code) and draw a flowchart that takes two
input values x and y, then find the GCD (greatest-common divisor) for the two input
values.

The logical steps and flowchart is in the slides of the lectures.

Question #9: Assume a shelf of 10 rows (each row having one column). Each row is
storing a student file identified (labeled) by his/her student identification number (SID).
The files are placed in the shelf in an unordered manner. Write the logical steps (pseudo-
code) and draw a flowchart that orders (sorts) the files in the shelf in descending order.

Step1: Initialize the variables representing rows on shelf as; A1, A2, A3, A4, A5, A6, A7,
A8, A9, A10 by 0
Step2: Use i, j to represent each row and Ai / Aj to represent each SID (where i and j
cover the range from 1 to 10)
Step3: Inititalize i by 1
Step4: Check;
(a) If(i is less than or equal to 10) then
i) Read Ai
ii) Add 1 in i
iii) Move to Step 4(a)
Step5: Initialize i by 1 and j by 2
Step6: Check;
(a) If (i is less than 10) then
i) if(j is less than or equal to 10) then
1) Check; if(Ai is less than Aj) then
1(i) Assign Ai to temp
1(ii) Assign Aj to Ai
1(iii) Assign temp to Aj
2)Add 1 in j
3) Move to Step 6(a)(i)
ii)otherwise;
1) Add 1 in i
2) Initialize j by adding 1 in i
3) Move to Step 6(a)
(b)Otherwise;
i)Print ‘ The rows on the shelf has files of each SID in descending order’
ii) Move to Step 7
Step 7: Stop

-----------------------------------------------The
End--------------------------------------------------

You might also like