0% found this document useful (0 votes)
6 views

CS111 2020 Assignment2 AnswersGradedv4

Uploaded by

qwer353666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS111 2020 Assignment2 AnswersGradedv4

Uploaded by

qwer353666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Question 1 (10 points)

(4 points for 1. and 6 points for 2. )

1- Add a new part to the above flowchart to get the average of only numbers
in sum
I. Avgcounter = Avgcounter +1 (1.5 points)
II. Numavg= sum/ Avgcounter (1.5 points)

III. Print Average Number (1 points)

2- Add a new part to the above flowchart to get the max of only numbers in
sum
I. Max = 0 (1.5 points)
II. If num >max (1.5 points)
III. Max=num (1.5 points)
IV. Print Max Number(1.5 points)
Question 2 (10 points)
The algorithm in pseudo code

1. This algorithm converts a decimal number to a binary one. 6 points


2. N = 21 4 points, ½ for each line
1- Step 1 N = 21
2- Step 2 N equals 0 is false, so Binary = "0"
4- Remainder = 1 Binary = 1 N = 10
4- Remainder = 0 Binary = 01 N=5
4- Remainder = 1 Binary = 101 N=2
4- Remainder = 0 Binary = 0101 N=1
4- Remainder = 1 Binary = 10101 N=0

6- Print 10101

Question 3: based on the below pseudo code, (10 points)


Step 1: start
Step 2: get n value
Get desiredAge value (2.5 points)
Step 3: set initial value i=1,
Set rejected = 0 (1 point)
Set accepted = 0 (1 point)
Step 4: check i value if(i<=n) go to step 5 else go to step 11
Step 5: get age value
Step 6: check age value if(age >= 18) go to step 7 else go to step 8
check age value if(age >= desiredAge) go to step 7 else go to step
8 (2.5 points)
Step 7: Print “You can issue driving license”
Increment accepted value by 1 (1 point)
Step 8: Print “You cannot issue driving license”
Increment rejected value by 1 (1 point)
Step 9: increment i value by 1
Print rejected, accepted (1 point)

Step 10: go to step 4


Step 11: stop

Question 4 (10 points)


pseudo code (5 points) (0.5 point for each step)
Flowchart (5 points) (0.5 point for each step)

Solution 1:

Step 1: start
Step 2: get binv value
Step 3: set decv =0, base=1
Step 4: set temp = binv
Step 5: check if temp >=1 go to step 6 else go to step 10
Step 6: set last_digit = temp % 10;
Step 7: temp = temp / 10;
Step 8: decv =decv +( last_digit * base);
Step 9: base = base * 2;
Step 10: Print “Decimal Value is” + decv
Solution 2:

Step 1: start
Step 2: get binv value
Step 3: set decv =0, base=1
Step 4: set len = binv.length
Step 5: set iterator = len - 1
Step 6: check if iterator >=0 go to step 7 else go to step 10
Step 7: if (binv[iterator] == '1')
Step 8: decv = decv + base;
Step 9: base = base * 2;
Step 10: Print “Decimal Value is” + decv

Question 5 (10 points, 1 for each line) With Loop

1. Start 1. Start
2. Read n 2. Read n
3. i = 1 and S = 0 3. i = 1 and S = 0 and m = 0
4. m = (2^i) 4. While m < n
5. S = S + (1/m) 5. m = (2^i)
6. i = i + 1 6. S = S + (1/m)
7. If (m < n) 7. i=i+1
Go to line 4 Endwhile
Endif 8. If (m > n)
8. If m > n 9. S = S – (1 / m)
9. S = S – (1 / m) 10. S = S + (1 / n)
10. S = S + (1 / n) Endif
Endif 11. Print S
11. Print S 12. End
12. End

Here if the n=32 So the output S = ½ + ¼ + 1/8 + 1/16 + 1/32

But if the n=33 So the output S = ½ + ¼ + 1/8 + 1/16 + 1/32 + 1/33


(Note: No problem in case of the answer not handing a test case like this, and the students
consider that the input n will be always equal to 2^i exactly. So steps 8 to 10 are optional)

Question 6: (10 points)


Pseudo-code: (5 points, 1/2 for each line)
1. Start
2. Read X and N
3. i = 0 and S = 0 and F = 1
4. While i <= N
5. S = S + (X^i)*F
6. F = F * -1
7. i=i+1
Endwhile
8. Print S
9. End
Flowchart: (5 points, ½ for each symbol)
Question 7: (10 points)
Write pseudo code and draw flowchart to find the smallest N prime
numbers. Trace the algorithm when N = 4.
pseudo code (3 points)
Step 1: Start

Step 2: Declare Variable num, i, flag

Step 3: input n

Step 4: Intialize num=1,count=0

Step 5: Repeat until count< n

5.1: Initialize Variable i=1 and flag=0

5.2: Repeat Until i<=num

5.2.1 if(num%i==0),
set flag=flag+1

5.2.2 increment i=i+1

5.3 If(flag<=2),
Print num,
set count=count+1
5.4 increment num=num+1
Step 6: Stop
Flowchart (3 points)

Trace (4 points)

count num i flag n Count < n Num%i ==0 Flag <=2


0 1 1 0 4 true true
2 1 True false
0 2 1 0 4 true
2 2 1 true
2 3 2 false true
1 3 1 0 4 true true
3 2 1 true false
3 3 1 true true
3 4 2 false true
2 4 1 0 4 true true
4 2 1 true
4 3 2 false
4 4 2 true
4 5 3 false
2 5 1 0 4 true true
5 2 1 false
5 3 1 false
5 4 1 false
5 5 1 true
5 6 2 true
3 6 1 0 4 true true
6 2 1 true
6 3 2 true
6 4 3 false
6 5 3 false
6 6 3 true
6 7 4 false
3 7 1 0 4 true true
7 2 1 false
7 3 1 false
7 4 1 false
7 5 1 false
7 6 1 false
7 7 1 true
7 8 2 true
4 8 false
Output:
2, 3, 5, 7

Question 8: (10 points)


Pseudo-code: (5 points)
1. Start
2. Read n
3. S = 0 and i = 3 (1 point)
4. While i <= n (1 point)
5. S=S+I (1 point)
6. i=i+3 (1 point)
Endwhile
5. Print S (1 point)
6. End

Flowchart: (5 points, ½ for each symbol)

You might also like