Download Practice Assignment 6 Solution - GUC MET

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
German University in Cairo
Faculty of Media Engineering and Technology
Prof. Dr. Slim Abdennadher
Dr. Rimon Elias
Introduction to Computer Science, Winter Term 2013-2014
Practice Assignment6
Discussion: 07.12.2013 - 12.12.2013
Exercise 6-1
The simplest algorithm to search a list of Numbers N 1, ..., N m for a given key Key is to test successively
each element.
get m
get N1,..,Nm
get Key
set i to 1
set FOUND to NO
while (i<=m and FOUND = NO)
{
if (Key = Ni) then
set FOUND to YES
else
set i to i+1
endif
}
if (FOUND = NO) then
print "Sorry, key is not in the list"
else
print "Key found"
endif
If a list is already stored in increasing order, a modified sequential search algorithm can be used that
compares aganist each element in turn, stopping if a list element exceeds the target value.
Write a pseudocode for the modified sequential serach.
Solution:
get m
get N1,...,Nm
get key
set i to 1
set FOUND to NO
while (i<=m and FOUND = NO and key>=Ni)
{
if (Key = Ni) then
set FOUND to YES
else
set i to i+1
endif
}
if (FOUND = NO) then
print "Sorry, key is not in the list"
else
print "Key found"
endif
1
Exercise 6-2
Given two lists A and B of the form A1,...,An and B1,...,Bn respectively. Write an algorithm that uses
looping to store the sum of the corresponding elements of the lists A and B in a new list C.
Solution:
get n
get A1,...,An
get B1,...,Bn
set i to 1
while (i <= n)
{
set Ci to Ai+Bi
print Ci
set i to i+1
}
Exercise 6-3
Write an algorithm to find the maximum value stored in an (unsorted) list A1, A2,..., An of n integers.
Solution:
get n
get A1, A2,...,An
set largestSoFar to A1
set i to 2
while (i<= n)
{
if (Ai > largestSoFar) then
set largestSoFar to Ai
endif
set i to i + 1
}
print largestSoFar
Exercise 6-4
Given a list of 1000 non-negative numbers. Write an algorithm to find the number of
• even positive numbers
• odd positive numbers
• Zeros
Additionally, the algorithm should find the sum of
• even positive numbers
• odd positive numbers
Note that a number X is even if the value of 2∗ the integer value of X/2 is equal to X, and is odd
otherwise (except for the zero value).
Example: Testing whether the number 7 is even or odd:
INT(7/2) ∗ 2 = 3 ∗ 2 = 6. Since 6 6= 7, then 7 is an odd number.
Solution:
2
get A1,A2,A3,...,A1000
set i to 1
set evenCount to 0
set oddCount to 0
set zeros to 0
set evenSum to 0
set oddSum to 0
while (i <= 1000)
{
if (Ai = 0) then
set zeros to zeros + 1
else
if ((INT(Ai/2))*2 = Ai) then
set evenCount to evenCount + 1
set evenSum to (evenSum + Ai)
else
set oddCount to oddCount+1
set oddSum to (oddSum + Ai)
endif
endif
set i to (i + 1)
}
print "The number of even numbers is:"
print evenCount
print "The sum of even numbers is:"
print evenSum
print "The number of odd numbers is:"
print oddCount
print "The sum of odd numbers is:"
print oddSum
print "The number of zeros is:"
print zeros
Exercise 6-5
Write an algorithm that given an ordered list of integers A1,A2,...,An prints the elements in the list
that are repeated. If some elements occur more than twice, then these elements should be printed only
once.
For example, for the list
1 1 1 1 4 6 7 7 8
your algorithm should print
1 7
Solution:
• get n
get A1,...,An
set i to 1
set printed to No
while(i<n)
{
if(Ai <> Ai+1) then
set printed to No
set i to i+1
3
else
if( Ai=Ai+1 AND printed =No) then
print Ai
set printed to yes
set i to i+1
else
set i to i+1
endif
endif
}
• get n
get A1,...,An
set flag to A1 - 1
set i to 1
while (i < n)
{
if (flag <> Ai) then
if (Ai = Ai+1) then
set flag = Ai
print Ai
set i to i + 2
else
set i to i + 1
endif
else
set i to i+1
endif
}
Exercise 6-6
Given a list of pair numbers (A1,B1),...,(An,Bn). Write an algorithm that uses looping to print out
all pairs (Ai,Bi) of numbers whose sum is 100. An example of such a pair is (58,42).
For example if given the input
(23,44), (58,42), (10,0), (42,58)
the algorithm would print out
(58,42)
(42,58)
The algorithm could start as follows:
get n
get (A1,B1),...,(An,Bn)
Solution:
get n
get (A1,B1),...,(An,Bn)
set i to 1
while (i <= n)
{
set sum to Ai + Bi
4
if (sum = 100) then
print (Ai,Bi)
endif
set i to i + 1
}
Exercise 6-7
Write an algorithm that reverses the order of elements of the given list.
Solution:
• get n
get A1,...,An
set i to 1
set j to n
while(i <= INT(n/2))
{
set temp to Ai
set Ai to Aj
set Aj to temp
set i to i+1
set j to j-1
print Ai
}
• get n
get A1,...,An
set i to 1
set j to n
while(i <= n)
{
set Bi to Aj
set i to i+1
set j to j-1
print Bi
}
Exercise 6-8
Write an algorithm that given given a list of integers A1, ...,An moves all even elements in a list of
integers to the front of the list and all odd elements to the rear. Hint: you do not have to maintain any
order other than all evens appearing before all odds in the list. For example: if the list is of the form
1,4,5,6,2,10 then the algorithm should create a new list of the form 4,6,2,10,5,1 and prints the elements
of the resulting list.
Solution:
get A1, ..., An
get n
set i to 1
set j to n
set c to 1
while(c =< n) {
if (Ac % 2 == 1)
then set Bj to Ac
set j to j - 1
else set Bi to Ac
5
set i
endif
set c
}
set k
while
print
set k
}
to i + 1
to c + 1
to 1
(k =< n) {
Bk
to k + 1
Exercise 6-9
Write an algorithm that given a list of integers A1, ..., An and a number x prints the number of
occurrences of x in the list. In addition, the algorithm should print the positions where x occurs.
For example, if the list is 1, 2, 4, 1, 3 and x is 1 then the algorithm should print
1 occurs in the following positions: 1, 4
The number of occurences of 1 is 2
If the list is 1, 2, 4, 1, 3 and x is 0 then the algorithm should print
The number of occurences of 0 is 0
Solution:
get n, A1, ..., An
get x
set i to 1
set occurence to 0
while (i =< n) {
if (x = Ai)
then set occurence to occurence + 1
endif
set i to i + 1
}
if (occurence = 0)
then print "The number of occurences of" + x + "is" + 0
else print x + "occurs in the following positions"
set i to 1
while (i =< n) {
if (x = Ai)
then print i
endif
set i to i + 1
}
print "The number of occurences of" + x + "is" + occurence
endif
6