Вы находитесь на странице: 1из 7

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY

ISLAMABAD
Assignment No. 1:FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
Subject: Design and Analysis of Algorithm

Assigned: September 9, 2016

Assignment Marks: --

Instructor: Tanveer Ahmed Siddiqui

Due Date: September 19, 2016

Attached Files: Non

Objectives and Learning Outcome:


CLO 1: Identify the properties and prove correctness of an algorithm.
The objective of this assignment is to give you some practice regarding:
How to designing a flow chart of a given problem
Determine which characteristics of an algorithm a given procedure have and which it
lack.
Instructions:
Please read the following instructions before attempting to solve this assignment
1. This is an individual assignment. You will submit your work individually through your logins (course
portal) and also submit hard copy.
2. Try to get the concepts, consolidate your concepts and ideas from these questions
3. You should concern recommended books for clarify your concepts as handouts are not sufficient.
4. Try to make solution by yourself and protect your work from other students. If I found the
solution files of some students are same then I will reward zero marks to all those students.
5. Deadline for this assignment is September 19, 2016. This deadline will not be extended.
Question # 1
For EVEN Roll Number Group
Q1. What is flowchart? What are its advantages and limitations/disadvantages?
Q2 Draw a flowchart of the logical steps needed to print the name and age of the oldest and the youngest
student in a class. The input records contain the name and age of the students. Assume a sentinel value of 99
for the age field of the trailer record.
Q3. Input Data regarding the information of employees of a company has been supplied. The first field of
each input record contains the employee number (EmployeeNo). Assume that the input data of all the
employees is terminated by a trailer record having a sentinel value of 99999 for EmployeeNo. Draw a
flowchart for the logic to count and print the total number of input records, that is, the total number of
employees.
Q4. A population survey has been carried out in a given city, and the information received from the survey
has been stored in computer, one record per citizen. That is, each record contains the name, address, sex, age,
profession, etc of one citizen. We want to print the details of all the adults (aged 18 years or more). Finally
we also want to print the total number of adults. Assume a suitable sentinel value for any field in the trailer
record, and draw a flow chart for the algorithm to do this task.
Q5. Each paper in a set of examination papers includes a grade of A, B, C, D or F. A count is to be made of
how many papers have the grade of A, and how many have the grade F. The total count of both types has to
be printed at the end. Prepare a flowchart to perform this task. Assume a suitable sentinel value for the trailer
record.
Q6. An employer plans to pay bonus to each employee. Those earning Rs. 2000 or above are to be paid 10
percent of their salary and those earning less than Rs. 2000 are to be paid Rs.200. The input records contain
Design and Analysis of Algorithm

Page 1

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD
the employee number, name, and salary of the employees. The output to be printed should contain the
employee number, name and the amount of bonus to be paid to each employee. Draw a flowchart for the
algorithm to do this job. Assume a suitable sentinel value for any of the fields of the trailer record.
Q7. A data file contains a set of examination scores and is followed by a trailer record with a value of -1.
Draw a flowchart for the logic to calculate and print the average of the scores.
Q8. Five numbers denoted by the variables A, B, C, D and E are supplied as input. Draw a flowchart for the
logic to print these numbers in descending order of magnitude.
Q9. Draw a flowchart for the logic to find out whether a given triangle ABC is isosceles. Assume that the
angles of the triangle are supplied as input. Print the answer as yes or no.
Q10. Draw a flow chart for the logic to find out whether a given quadrilateral ABCD is a rectangle. Assume
that all the four angles and four sides of the quadrilateral are supplied as input data. Print the answer as yes
or no.
Q11. Draw a flow chart for the logic to convert a number to decimal from another base.
For ODD Roll Number Group
Q1. How does a flowchart help a programmer in a program development?
Q2. Draw a flowchart of the logical steps needed to produce a printed listing of all students over the age of
20 in a class. The input records contain the name and age of the students. Assume a sentinel value of 99 for
the age field of the trailer record.
Q3. The first 20 records in a data set are to be read and printed. Draw a flowchart for the algorithm to do this
job. Make sure that the processing stops after the twentieth record.
Q4. For the employees problem of even group Question 3(Q3), we want to count and print the number of
only male employees in the age group of 25 to 30. Assume that the input records contain SexCode and Age
fields to provide this information. Draw a flowchart for the algorithm to perform this job.
Q5. A set of examination papers, which have been graded with scores form 0 to 100 is to be searched to find
how many of them are above 90. The total has to be printed. Prepare a flowchart to do this job. Assume a
suitable sentinel value for the trailer record.
Q6. A shopkeeper wants to have a general program for his personal computer, which will prepare bills for
each customer as and when he sells goods to them. His idea is that as soon as the customer purchases some
goods from his shop, he will supply the description, unit price, and the quantity purchased for each item. as
input to the computer. He wants that with this information, the computer should print each item along with
its unit price, quantity purchased and the total price. Finally the computer should also print the total cost of
all the items purchased by the customer. Assuming a sentinel value of zero for the quantity purchased field in
the trailer record, draw a flowchart for the logic to do this job.
Q7. Each employee pay record includes the hours worked and the pay rate. The gross pay is to be determined
as hours worked times pay rate, and is to be printed for each employee. For all hours worked in excess of 40,
the overtime rate, which is 1.5 times the regular rate, is to be paid. Draw a flowchart for the problem logic to
do this. Assume a suitable sentinel value for any of the input fields of the trailer record.
Q8. The data file of even group Question (Q7) is expanded to include several sets of data, each requiring
calculation of its average. Each data set is followed by a trailer record with a value of -1; however the last
data is followed by a trailer record with a value of -2. Draw a flowchart for the logic to perform this task.
Q9. Draw a flow chart to add up all the even numbers between 0 and 100. Before ending, print the result of
calculation.
Q10. Draw a flowchart for the logic to find out whether a given triangle ABC is a right angled triangle.
Assume that the sides are supplied as input and data. Print the answer as yes or no.
Q11. Draw a flowchart for the logic to convert a number from base 10 to new base using division remainder
Design and Analysis of Algorithm

Page 2

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD
technique.
Question # 2 (For all students)
Goldbachs conjecture states that every even number greater than 2 is the sum of two prime numbers. Here
is the procedure that checks whether Goldbachs conjecture is true:
1.
Let n =4.
2.
If n is not the sum of two primes, output no and stop.
3.
Else increase n by 2 and continue with step 2.
4.
Output yes and stop.
a) Which properties of an algorithm input, output, precision, determinism, finiteness, correctness,
generality does this procedure have?
Solution
Procedur
e
1

Input Outpu

Precision Finiteness Definitenes

Correctness Generality

b) Which properties of an algorithm input, output, precision, determinism, finiteness, correctness,


generality if any, are lacking in the following? Explain. The input is a set S of integers and an
integer m. The output is all subsets of S that sum to m.
1. List all subsets of S and their sums.
2. Proceed through the subsets listed in step 1 and output each whose sum is m.
Solution
Procedur
e
1

Input Outpu

Precision Finiteness Definitenes

Correctness Generality

Question # 3 (For all students)


Consider the following algorithm
ALGORITHM Euclid(m, n)
// This algorithm computes the Greatest Common Divisor of two positive integers by Euclids algorithm
// Input: Two non-negative integers m and n, not both zero.
// Output: Greatest Common Divisor of m and n
while n 0 do
rm%n
mn
nr
return m
a) What does Euclids algorithm do for a pair of numbers in which the first number is smaller than the
second one?
b) What is the largest number of times this can happen during the algorithms execution on such an
input?
c) Modify Euclids algorithm in the light of your answer ( part (a) and (b) )
Question # 4 (For all students)
We discussed the following procedure for computing GCD in class room

Design and Analysis of Algorithm

Page 3

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD
Step 1
Step 2
Step 3
Step 4
a)

Assign the value of min{m,n} to t


Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4
Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4
Decrease t by 1 and go to Step 2
How do we know that the process will eventually stop?
b) The above procedure, in the form presented, does not work correctly when one of its input numbers is zero.
Write the above method in pseudo code form that incorporates the improvement so that it works for all valid
inputs.

Question # 5 (For all students)


a) Find gcd(31415, 14142) by applying Euclids algorithm.
b) Estimate how many times faster it will be to find gcd(31415, 14142) by Euclids algorithm compared
with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n).
Question # 6 (For all students)
a) What is the smallest number of divisions made by Euclids algorithm among all inputs 1 m, n 10?
b) What is the largest number of divisions made by Euclids algorithm among all inputs 1 m, n 10?
Note: Student will not earned marks if they just write answer (i.e. smallest number/ largest number).
Explain your complete solution with the help of example.
Question # 7 (For all students)
Euclids algorithm, as presented in Euclids paper, uses subtractions rather than integer divisions. Write a
pseudo-code for this version of Euclids algorithm.
Question # 8 (For all students)
You are a software engineer at Techlogix Lahore. A year ago, your manager announced that your next
software product would comprise of 10 modules. As a system analyst, you analyzed the dependency of
module and time required to complete specific module. On the above analysis you compiled the following
table:
Module
Prerequisites Duration
M1
None
14
M2
M1
28
M3
M2
42
M4
M3
21
M5
M4, M9
21
M6
M2
20
M7
M6
10
M8
M5, M9
35
M9
M3
50
M10
M7,M8
00
Your manager want to know the minimum possible time for completing project according to the activities
described in above table. He assigns this task to you. You are designing an algorithm that returns the
minimum possible time for completing the project. As a software engineer/programmer, which data
structure would you select in order to solve the given problem algorithmically?
Question # 9 (For all students)
Suppose you are given a set S of n real numbers. Let x and y be largest and smallest element of S

Design and Analysis of Algorithm

Page 4

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD
respectively. We define range of S as:
Range = x y
For each representation of S given below, design an algorithm to compute the range.
[Specify your algorithms in Pseudo code]
1) An unsorted array.
2) A sorted array.
3) An array which is a concatenation of two sorted portions.
4) A sorted singly linked list.
A binary search tree.

STRATEGY IF WE ARE STUCK


If we are stuck and do not seem to go anywhere, we try the following:
1. Persevere. Keep trying different strategies and stay open to creative ideas. Try not to

get frustrated.
2. Be more active in the solution.
i.

Ponder over questions about the problem. Is this problem a routine one? What
data are missing? What equations can we use? Explore the problem.

ii.

Draw sketches of the solution that is most likely to be.

iii.

Write equations.

iv.

Keep track of your progress

3. Refocus on the fundamentals. Look for similar examples.


4. Break the problem into parts. Analyze the parts of the problem. Concentrate on the parts
of the problem that can be solved.
5. Verbalize the problem. Describe:
i.

What the problem is

ii.

What has been done

iii.

Where it got stuck

6. Paraphrase. Re-describe the problem. Think of simpler ways to describe the problem
7. Look at extreme cases that could give insight and understanding. For instance, what
happens if x = 0, x = infinity?

Design and Analysis of Algorithm

Page 5

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD
8. Simplify the problem and solve a limiting case. Break up the problem into simpler
pieces and solve each piece by itself. Find a related but simpler example and work from
there.
9. Try substituting numbers to see if a term can be neglected.
10. Try solving for ratios to drop out parameters that are not given. We may find that we do
not need to find some parameters because they cancel out!
11. Look for hidden assumptions or for information that has not been used. After reading
each phrase or sentence of the problem statement, we should look for any assumptions
that can be inferred from the phrase.
12. Alternate between working forward towards a solution and backwards from a solution we
assumed. Working backwards may at least give us clues as to how we should approach
the problem while working forward.
13. We should take a break, incubate and let the subconscious work on the problem.
Sometimes what we need is a break to achieve that final breakthrough.
14. Brainstorm. Think of different approaches to the problem, no matter how strange. Guess
the solution to the problem and then check the answer.
15. Check again to make sure that the problem being worked on is the right one. Double
check all the values, assumptions, and approaches to make sure that nothing has been
missed out.
16. Try using a different strategy. There is usually more than one way to solve a problem, and
we may find a method which is much easier or efficient, compared to methods considered
earlier.

Design and Analysis of Algorithm

Page 6

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY


ISLAMABAD

*************************GOOD

Design and Analysis of Algorithm

LUCK *************************

Page 7

Вам также может понравиться