CS111 2020 Assignment2 AnswersGradedv4
CS111 2020 Assignment2 AnswersGradedv4
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)
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
6- Print 10101
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
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
Step 3: input n
5.2.1 if(num%i==0),
set flag=flag+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)