Uploaded by sejit10990

extraPractice

advertisement
Problem 1. The following flowchart receives a natural number greater than 1 and determines if
it’s a prime number or not.
pre-condition: num>1  ℕ .
post-condition: a message to specify
whether num is prime or not is outputted.
Trace the flowchart for when 9 is entered for the number and fill out the following trace table.
We have done the first step for you.
time progress
num
9
prime
d
output
The table shows the time progress
too. Do NOT write more than one
value in each row. Otherwise, you
lose marks.
Not all rows may be used.
Problem 2. The following flowchart receives numbers until a 0 is entered or number of positive
values is more than twice negative ones.
pre-condition: num  ℝ .
post-condition: counts of positive
and negative values are outputted.
Trace the flowchart for when input values are -2,3,1,2; and fill out the following trace table. We
have done a few first steps for you.
num
p
0
n
0
-2
output
time progress
The table shows the time
progress too. Do NOT write
more than one value in each
row. Otherwise, you lose marks.
Not all rows may be used.
Problem 3. The following flowchart receives numbers and determines if each is positive or
negative until a zero is entered.
pre-con: a  ℝ
post-com: outputs whether input a is
positive or negative until 0 is entered.
Trace the flowchart for when input values are -2, 3, 3, -2, 100,0 ; and fill out the following trace
table. We have done the first step for you.
a
-2
output
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
time progress
Not all rows may be used.
Problem 4 . The following flowchart receives coefficients of a quadratic equation and
determines how many roots it has in real numbers.
pre-condition: a, b, c  ℝ represent
coefficients of a quadratic equation
post-condition: a message about
number of roots in ℝ is outputted
Trace the flowchart for when input values are 2,5,3; and fill out the following trace table. We
have done the first step for you.
a
2
b
c
discriminant
ans
output
time progress
The table shows the
time progress too.
Do NOT write more
than one value in
each row.
Otherwise, you lose
marks.
Not all rows may be
used.
Problem 5. The following flowchart calculates sum of numbers 5 to 9, inclusively.
pre-condition: none
post-condition: outputs sum of numbers 5 to 9.
Trace the flowchart and fill out the following trace table. We have done the first step for you.
a
5
sum
output
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
Not all rows may be used.
time progress
Problem 6. The following flowchart receives a natural number and outputs its digits from right
(least significant bit) to left (most significant bit).
pre-condition: a  ℤ+
post-condition: each digit of the number is
outputted, starting with the right-most.
Trace the flowchart for when input values are 853; and fill out the following trace table. We
have done the first step for you.
a
853
r
output
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
time progress
Not all rows may be used.
Problem 7. The following flowchart receives a natural number and outputs how many digit 7s it
contains.
pre-condition: a  ℤ+
post-condition: number of times
digit 7 occurs in a is outputted
Trace the flowchart for when input values are 7377; and fill out the following trace table. We
have done the first step for you.
a
7377
count
r
output
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
time progress
Not all rows may be used.
Problem 8. Devise an algorithm that inputs an array of 10 integers and outputs True if the sum
of any 9 values in the array is always greater than the omitted value, and False otherwise.
Thus, if the array was only of length 3 such as [2,3,4] the algorithm would return True since
2+3 > 4, 2+4 > 3, and 3+4 > 2. For the array [1,2,4,1] the algorithm would return False. You are
required to use a sub-algorithm named sumArrayElements(a) which returns the sum of all
elements in array a.
Problem 9. Devise an algorithm that inputs an array of 10 integers and finds an element with
the maximum frequency (i.e. the count). For example if the array was [-3,-2,0,4,2,5,0,-3,0,4] the
output would be 0 since it occurs three times. You are required to use a sub-algorithm named
frequency(v, a) that calculates the frequency of value v in array a.
Note if there are more than one element with the same maximum frequency, you can output
any of them. For instance, if the array was [-3,-2,0,4,2,5,0,-3,0,-3], you could output 0 or -3 as
both have the maximum frequency.
Problem 10. Devise an algorithm that inputs an array of 10 elements and removes duplicate
elements from the array, creating a new array that has no duplicates. You are required to use a
sub-algorithm named isDuplicate(v, a) that returns true if value v occurs in array a, or
false otherwise. Note that the parameters of the sub-algorithm represent values it must
receive from the main program.
Problem 11 . Devise an algorithm that inputs an array of 10 integers and determines if there is
an element in the array such that the sum of values up to and including that element is equal to
the sum of the remaining values. Such an array is said to be balanced.
For example, the array myArr = [1,5,1,3,1,2,-3,-2,4,2] is balanced since the sum of
values up to and including element myArr[2] is 7 and the sum of the remaining elements is also
7. You are required to use a sub-algorithm named sumArrayElements(a) which returns the
sum of all elements in array a. For the example above, sumArrayElements(myArr)returns 14.
The sum would help you in deciding if the array is balanced or not. For example, you might use
sum/2 and iterate across the array summing elements. Your algorithm should output either
Balanced at index 2 (for the example given, but of course index 2 will be different for
different arrays) or not balanced.
Problem 12 . Devise an algorithm that inputs and array of 10 elements and shifts all zeros
(elements with value zero) to the end of the array, preserving the order of the non-zero
elements. A simple approach is to move each non-zero value to the appropriate position, i.e.
the first non-zero will be moved to index 0, the second non-zero to index 1, the third to index 2,
and so on, and then to fill the rest of the array with zeros. You are required to use a subalgorithm named fillZeros(n, a) that returns an array in which the last n values of array a
are zeros. Note that the parameters of the sub-algorithm represent values it must receive from
the main program.
Example. Assume the inputted array is myArray = [3,-2,0,4,5,0,-3,0,11,-2].
Moving each non-zero value to the appropriate position results in myArray = [3,2,4,5,-3,11,-2,0,11,-2].
Then, calling fillZeros(3, myArray) results in myArray= [3,-2,4,5,-3,11,2,0,0,0].
Problem 13 . Devise an algorithm that inputs an array of 10 elements and calculates the largest
sum of three consecutive elements in that array. You are required to use a sub-algorithm
named sumConsecutive(firstIndex, secondIndex, a) that returns the sum of
consecutive elements in array a between two index values, firstIndex and
secondIndex inclusively. Note that the parameters of the sub-algorithm represent values it
must receive from the main program.
Example. Assume the inputted array is:
index
0
1
2
3
4
5
6
7
8
9
myArray
5
3
-4
2
4
5
2
1
9
-2
For instance, sumConsecutive(5, 7, myArray) returns 8 as myArray[5] +
myArray[6] + myArray[7] is 8.
In the example above, the algorithm should return 12 as the largest sum of three consecutive
elements.
Problem 14 . Devise an algorithm to fill an array with 6 integers chosen randomly between 1
and 49 such that there are no duplicate values in the array. Use a sub-algorithm named
isDuplicate(newValue, randomArray) that returns true if a newly generated random
value is the same as one already in the array, or false otherwise. Note that the parameters of
the sub-algorithm represent values it must receive from the main program.
Hint 1. To get length of an array myArray, you can use myArray.length notation in your
algorithms. Other reasonable notations are acceptable too.
Hint 2. To assign a value to an array myArray at its position i, you can use myArray[i]
notation or myArrayi notation. Other reasonable notations are acceptable too.
Hint 3. To pick a random whole number between x and y inclusively, you can use
random(x,y). Other reasonable notations are acceptable too.
Note 1: you are required to utilize loops in your solution.
Note 2: You must write pre- and post-conditions for both the main algorithm and the subalgorithm.
Note 3: You should use the following reference as flowchart symbols.
Sol 1 .
num
9
prime
d
output
The table shows the time progress
too. Do NOT write more than one
value in each row. Otherwise, you
lose marks.
time progress
true
2
3
false
is not prime
Not all rows may be used.
Sol 2.
num
p
0
n
output
The table shows the time
progress too. Do NOT write
more than one value in each
row. Otherwise, you lose marks.
0
-2
1
time progress
3
1
Not all rows may be used.
1
2
2
3
pos:3 neg:1
Sol 3.
a
-2
output
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
negative
3
positive
time progress
Not all rows may be used.
3
positive
-2
negative
100
positive
0
end
Sol 4.
a
2
b
c
discriminant
ans
output
5
3
1
time progress
2 distinct root
2 distinct root
The table shows the
time progress too.
Do NOT write more
than one value in
each row.
Otherwise, you lose
marks.
Not all rows may be
used.
Sol 5.
a
5
sum
output
0
5
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
Not all rows may be used.
6
time progress
11
7
18
8
26
9
35
5+6+…+9=35
Sol 6.
a
853
r
output
3
85
3,
time progress
5
8
5,
8
0
8,
The table shows the time progress too.
Do NOT write more than one value in
each row. Otherwise, you lose marks.
Not all rows may be used.
Sol7.
a
7377
count
r
output
0
7
737
1
7
73
2
3
7
7
0
3
3
Sol 8.
Sol 9.
Sol 10.
Sol 11.
Sol 12.
Sol 13.
Sol 14.
Download