0% found this document useful (0 votes)
3K views

Week 3 - Pseudocode

The pseudocode executes on the "Scores" dataset. Variable A is initialized to 0 and incremented if a student's total marks are greater than the class average total marks. Variables B, C, and D are initialized to False and set to True if a student's marks in a subject are below the average for that subject. At the end, Count will represent the number of students whose total marks are above average but who scored below average in at least two subjects.

Uploaded by

amrc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Week 3 - Pseudocode

The pseudocode executes on the "Scores" dataset. Variable A is initialized to 0 and incremented if a student's total marks are greater than the class average total marks. Variables B, C, and D are initialized to False and set to True if a student's marks in a subject are below the average for that subject. At the end, Count will represent the number of students whose total marks are above average but who scored below average in at least two subjects.

Uploaded by

amrc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Week-3, Graded Assignment solution

Questions (1 & 2)
Statement
The following pseudocode is executed using the “Words” dataset.

1 A = 0, B = 0
2 while(Table 1 has more rows){
3     Read the first row X in Table 1
4     if(X.LetterCount < 3 or X.LetterCount > 5){
5        A = A + 1
6     }
7     else{
8        if(X.PartOfSpeech == "Noun"){
9           B = B + 1
10       }
11     }
12     Move X to Table 2
13 }

Pseudocode Explanation
The given pseudocode is executed on "Words" dataset.

Variables A, and B are initialized with 0.


Line 4: Condition checks if the word in current row has letter count either less than 3 or
greater than 5. If so then A will be incremented. This refers that A represents the number of
words with letter count either less than 3 or greater than 5.
Line 7: else is being used, this means if the condition in filter line is not satisfied then else
block will be executed. Which refers that if the letter count is neither less than 3 nor greater
than 5 then the condition in line 8 will be executed.
If the condition in line 8 is True, this means the word neither has letter count less than 3 and
nor greater than 5 and the word is a noun. If so then B gets incremented. Therefore, B stores
the number nouns with letter count greater than or equal to 3 and letter count less than or
equal to 5.

Question 1 [4 Marks]
Statement
What will A represent at the end of the execution?

Options
(a)

Number of words with letter count either less than 3 or more than 5

(b)

Number of nouns with letter count greater than 3 but at most 5

(c)

Number of words other than noun with letter count either less than 3 or more than 5

(d)

Number of words other than noun with letter count greater than 3 but at least 5

Answer
(a)
Question 2 [4 Marks]
Statement
What will B represent at the end of execution?

Options
(a)

Number of words other than noun with letter count either less than 3 or more than 5

(b)

Number of nouns with letter count either less than 3 or more than 5

(c)

Number of nouns with letter count at most 5

(d)

Number of nouns with letter count 3, 4, and 5

Answer
(d)
Question 3 [4 Marks]
Statement
The following pseudocode is executed using the “Scores” dataset. When will C be True?

1 A = 0, B = 0, C = False
2 while(Table 1 has more rows){
3    Read the first row X in Table 1
4    if(X.Gender == 'F'){
5        A = A + 1
6   }
7    else{
8        if(X.CityTown == "Chennai"){
9            B = B + 1
10       }
11   }
12    Move X to Table 2
13 }
14 if(A > B){
15   C = True
16 }

Options
(a)

If there exist more female students than students from Chennai

(b)

If there exist more Chennai students than female students

(c)

If there exist more female students than male students

(d)

If there exist more female students than male students from Chennai

Answer
(d)

Solution
The question is about finding the case when C will be True? From Line 14 to Line 16 it is very clear
that C will be True when the value of A is greater than the value of B. Now the question is about
finding the case when A will be greater than B.

A and B both are initialized with 0.


Line 4: Condition checks if the Gender is female or not. If the gender is female then, A gets
incremented in line 5. This means A stores the number of female students.
Line 7: else block will get executed if the condition given in line 4 is not True. This means else
block will be executed if row does not belong to a Female.
Line 8: Condition checks if the student belongs to Chennai.
Line 9: B will get incremented if and only if the student is not Female and student is from
Chennai. This means B stores the number of male students who are from Chennai.
Therefore, A > B will be True when there are more Female students than Male students from
Chennai.
Question 4 [4 Marks]
Statement
The following pseudocode is executed using the “Shopping Bills” dataset. Procedure countBills
accepts a card Y and returns True if the total amount of the bill is greater than the average total
bill amount of shop from where the card Y is generated otherwise returns False. Choose the
correct code fragments to complete the procedure. It is a Multiple Select Question (MSQ).

1 Procedure countBills(Y)
2    count = 0, avg = 0, amount = 0
3 while(Pile 1 has more cards){
4        Read the top card X from Pile 1
5        if(X.ShopName == Y.ShopName){
6            amount = amount + X.TotalBillAmount
7            count = count + 1
8       }
9 Move card X to Pile 2
10 }
11 avg = amount/count
12    *********************
13    * Fill the code *
14    *********************
15 End countBills

Options
(a)

1 if(X.TotalBillAmount > Avg){


2 return(True)
3 }
4 return(False)

(b)

1 if(Y.TotalBillAmount > Avg){


2 return(True)
3 }
4 return(False)

(c)

1 if(Y.TotalBillAmount <= Avg){


2 return(False)
3 }
4 else{
5    return(True)
6 }
(d)

1 if(X.TotalBillAmount <= Avg){


2 return(False)
3 }
4 return(True)

Answer
(b), (c)

Solution
If a card (let us say Y) is passed as the parameter to the procedure countBills . Let the card is
generated from a shop XYZ, then the procedure will return True if the total bill amount of this
card is the greater than the average bill amount generated from the shop XYZ.

To do so, first we need to collect all the cards which are generated from the same shop i.e., XYZ
and then find its average total bill amount.

We can check that if the card X is from the same shop or not by using the condition X.ShopName
== Y.ShopName. This is what being done in the Line 5.

Line 6: Total amount of every card which are from same shop is being added into variable
amount which was initialized as 0.

Line 7: Variable count stores the number of cards from the same shop.

Line 11: avg stores the average of total bill amount generated from the same shop.

Now we just need to check if the Total Bill Amount of card Y is greater than average or not.

We need to check it for card Y therefore, option (a) and (d) are needed to be checked.

Option b returns True if the Total amount is greater than average therefore, it is correct.

Option c returns False if the Total amount is less than are equal to average otherwise returns
True. This means it returns True if the Total amount is greater than average therefore, it is
correct.

Therefore the correct options are (b) and (c).


Question 5 [4 Marks]
Statement
The following pseudocode is executed using the "Scores" dataset. What will A represent at the
end of the execution?

1 A = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4    B = True
5    if(X.Physics <= 70){
6   B = False
7   }
8    if(X.Chemistry <= 70){
9   B = False
10 }
11    if(X.Mathematics <= 70){
12   B = False
13   }
14    if(B == True){
15   A = A + 1
16   }
17 Move X to Table 2
18 }

Options
(a)

Number of students with all subject marks more than 70

(b)

Number of students with exactly one subject marks less than 70

(c)

Number of students with all subject marks less than 70

(d)

Number of students with all subject marks at least 70

Answer
(a)

Solution
The question is about finding the representation of variable A. A is initialized with 0 and being
incremented in line 15 only when B is True. Now the question is when will be the variable B True.
Line 4: B is initialized with True only but in middle it gets updated to False if any one of the
conditions given in lines 5, 8, and 11 becomes True.
Line 5: The condition checks if the student has scored less or equal to 70 marks in Physics or
not. If so then B becomes False. This means B will still be True if student has scored more
than 70 marks in Physics.
B Becomes False when a student scores less than or equal to 70 marks in any of the three
subjects. This means B will be True if the students has not scored less than or equal 70
marks in any of the three subjects. Which refers that B will be True if the student has scored
more than 70 marks in all the three subjects.
Question 6 [4 Marks]
Statement
The following pseudocode is executed using the “Scores” dataset. At the end of the execution,
variable Count captures the number of students whose total marks are more than the class
average (of total marks) but have scored below the subject average in at least two subjects.
Assume that the variable AvgT holds the value of the average total marks. Similarly, the variables
AvgP, AvgC and AvgM hold the value of the average marks of Physics, Chemistry and
Mathematics respectively. Choose the correct code fragment(s) to complete the pseudocode. It is
a Multiple Select Question (MSQ).

1 Count = 0
2 while(Table 1 has more rows){
3    Read the first row X from Table 1
4    A = False, B = False, C = False, D = False
5    if(X.Total > AvgT){
6        A = True
7   }
8    if(X.Mathematics < AvgM){
9        B = True
10   }
11    if(X.Physics < AvgP){
12        C = True
13   }
14    if(X.Chemistry < AvgC){
15        D = True
16   }
17    *************************
18    **    Fill the code    **
19    *************************
20    Move X to Table 2
21 }

Options
(a)

1 if(A and (B or C) and (C or D) and (D or B)){


2    Count = Count + 1
3 }

(b)

1 if(A and ((B and C) or (C and D) or (D and B))){


2    Count = Count + 1
3 }

 
(c)

1 if(A or (B or C) and (C or D) and (D or B)){


2    Count = Count + 1
3 }

(d)

1 if(A or (B and C) or (C and D) or (D and B)){


2    Count = Count + 1
3 }

Solution
Let us first understand the meaning of each variable using the given pseudocode:

AvgT: Average total marks


AvgM: Average Mathematics marks

AvgP: Average Physics marks

AvgC: Average Chemistry marks

A: Initially False but it will become True if Total > AvgT

B: Initially False but it will become True if MathematicsMarks < AvgM

C: Initially False but it will become True if PhysicsMarks < AvgP

D: Initially False but it will become True if ChemistryMarks < AvgC

Now, as per the question statement Count will be incremented only if A is True and at least two
among B, C and D are True.

Option a: A and (B or C) and (C or D) and (D or B) --

Let A is True
Let B and C are True and D is False
(B or C) = True, (C or D) = True, and (D or B) = True
Overall - True and True and True and True = True
This will be True for any two variables are True like C and D or D and B.

Option b: A and ((B and C) or (C and D) or (D and B))

Let A is True
Let B and C are True and D is False
(B and C) = True, (C and D) = False, and (D and B) = False
(B and C) or (C and D) or (D and B) = True or False or False = True
Overall - True and True = True
This will be True for any two variables are True like C and D or D and B.

Option c: A or (B or C) and (C or D) and (D or B)

Let A is True
Let B and C are True and D is False
(B or C) = True, (C or D) = True, and (D or B) = True
Overall - True or True and True and True = True
This will be True for any two variables are True like C and D or D and B.
But even if A = False, then It will also give True, which we did not want.

Option c: A or (B and C) or (C and D) or (D and B)

Let A is True
Let B and C are True and D is False
(B and C) = True, (C and D) = False, and (D and B) = False
Overall - True or True or False or False = True
This will be True for any two variables are True like C and D or D and B.
But even if A = False, then It will also give True, which we did not want.

Therefore, the correct options are (a) and (b).


Question 7 [4 Marks]
Statement
The following pseudocode is executed using the “Shopping bills” dataset. What will the values of
the variables A and B represent at the end of the execution?

1 SumSV = 0, SumBB = 0
2 CountSV = 0, CountBB = 0
3 while(Pile 1 has more cards){
4      Read the top card X from Pile 1
5      if(X.ShopName == "SV Stores"){
6          SumSV = SumSV + X.TotalBillAmount
7          CountSV = CountSV + 1
8     }
9      if(X.ShopName == "Big Bazaar"){
10          SumBB = SumBB + X.TotalBillAmount
11          CountBB = CountBB + 1
12     }
13      Move card X to Pile 2
14 }
15
16 MSV = SumSV / CountSV
17 MBB = SumBB / CountBB
18 A = 0, B = 0
19 while(Pile 2 has more cards){
20      Read the top card X from Pile 2
21      if(X.ShopName == "SV Stores" and X.TotalBillAmount < MBB){
22          A = A + 1
23     }
24      if(X.ShopName == "Big Bazaar" and X.TotalBillAmount > MSV){
25          B = B + 1
26     }
27      Move card X to Pile 1
28 }

Options
(a)

A = Number of bills from Big Bazaar with total bill amount greater than the average total bill
amount of SV Stores

B = Number of bills from SV Stores with total bill amount less than the average total bill amount of
Big Bazaar

 
(b)

A = Number of bills from SV Stores with total bill amount greater than the average total bill
amount of SV Stores

B = Number of bills from Big Bazaar with total bill amount less than the average total bill amount
of Big Bazaar

(c)

A = Number of bills from SV Stores with total bill amount less than the average total bill amount
of Big Bazaar

B = Number of bills from Big Bazaar with total bill amount greater than the average total bill
amount of SV Stores

(d)

A = Number of bills with total bill amount greater than the average total bill amount

B = Number of bills with total bill amount less than the average total bill amount

Answer
(c)

Solution
In the first while loop we are computing values of SumSV, CountSV, SumBB and CountBB. Let us
understand what these variable represent in this pseudocode:

SumSV: Sum of total bill amount of SV Stores

CountSV: Number of bills of SV Stores

SumBB: Sum of total bill amount of Big Bazaar

CountBB: Number of bills of Big Bazaar

After the first while loop we are calculating values of MSV and MBB which represents average bill
amount of SV Stores and Big Bazaar respectively.

In the second while loop we are iterating over Pile 2 where all cards are stacked as a result of first
while loop. In this loop, value of A will be incremented if ShopName == “SV Stores” and
TotalBillAmount < MBB. Similarly, value of B will be incremented if ShopName == “Big Bazaar” and
TotalBillAmount > MSV. Therefore, the option (c) is the correct answer.
Question 8 [4 Marks]
Statement
The following pseudocode is executed using the "Scores" dataset. At the end of the execution, A
captures the number of male students who are below average in at most one subject. Assume
that the variables M, P and C hold the average marks of the subjects Mathematics, Physics and
Chemistry respectively. The pseudocode may have mistakes. Identify all such mistakes (if any).
Assume that all statements not listed in the options below are free of errors. It is a Multiple Select
Question (MSQ).

1 A = 0
2 while(Table 1 has more rows){
3    B = 0
4    Read the first row X from Table 1
5    if(X.Gender == 'M'){
6        if(X.Mathematics >= M){
7            B = B + 1
8       }
9        if(X.Physics >= P){
10            B = B + 1
11       }
12        if(X.Chemistry >= C){
13            B = B + 1
14       }
15   }
16    if(B >= 1){
17        A = A + 1
18   }
19    Move X to Table 2
20 }

Options
(a)

Line 5: Incorrect condition to check the gender

(b)

Line 6: Incorrect condition to update B

(c)

Line 7: Incorrect Update of B

(d)

Line 16: Incorrect condition to Update A


(e)

No error in the code

Answer
(d)

Solution
The variable A counts the number of male students who have scored less than the subject
average at most in one subject (either in one subject or in none of the subjects).

Line 16: A is being incremented whenever B is greater than or equal to 1. This means
question is now about to find if the given condition is correct or not.
Line 5 - Line 15: It is totally clear that B will be greater than 1 if and only if we Enter into the
given if block. In Line 5 it will enter into the if block if and only if the gender is male. Which is
required. Therefore line 5 is correct.
Line 6, 9, and 12: Conditions are to check if the student has scored less than average in the
particular subject. If student scores less than average then B will be 1, if in two subjects then
B will be 2, and if three subjects then B will be 3.
To say at most one subject B should be less than or equal to 1. One more thing needs to be
done for students who are male but have scored more than average in all the three subjects.
That is either the condition should be added to check if the student is male or this if block
should be inside the if block started at line 5. Therefore line 16 is incorrect.
Question 9 [4 Marks]
Statement
The following pseudocode is executed using the “Scores” dataset. Let variable maxTotal stores
the maximum total marks obtained by any student. What will the value of Flag be at the end of
the execution?

1 A = 0, B = 0, C = 0, D = 0
2 Flag = False
3 while(Pile 1 has more cards){
4      Read the top card X from Pile 1
5      A = replaceByMax(A, X.Mathematics)
6      B = replaceByMax(B, X.Physics)
7      C = replaceByMax(C, X.Chemistry)
8      Move X to Pile 2
9 }
10
11 D = A + B + C
12 if((D − maxTotal) >= 0){
13    Flag = True
14 }
15
16 Procedure replaceByMax(M, subMarks){
17     if(subMarks > M){
18         M = subMarks
19     }
20     return(M)
21 End replaceByMax

Options
(a)

False

(b)

True

(c)

Can not be determined

Answer
(b)
Solution
The given pseudocode has two part: the main and the procedure part. In the main part procedure
replaceByMax is called with four parameters X, A, B and C for the every card X. Now let us check
the procedure part of the pseudocode. The variables A, B and C are checked with the
Mathematics, Physics, and Chemistry marks respectively. At the end of the while loop variables A,
B and C will store the maximum marks in Mathematics, Physics and Chemistry respectively.

Next, variable D stores sum of highest marks in Mathematics, Physics and Chemistry. The variable
maxTotal stores the maximum total marks. The value of D will be always greater than or equal to
maxTotal. The value of D will be equal to maxTotal when a student gets the maximum marks in
each three subjects. Therefore, the quantity (D - maxTotal) >= 0 will be always True. Hence value
of Flag will be True at the end of the execution.
Question 10 [4 Marks]
Statement
The following pseudocode is executed using the “Words” dataset. What will A represent at the end
of the execution?

1 SumT = 0, CountT = 0
2 while(Table 1 has more rows){
3       Read the first row X in Table 1
4       CountT = CountT + 1
5       SumT = SumT + X.LetterCount
6       Move X to Table 2
7 }
8 B = SumT / CountT
9    
10 SumS = 0, CountS = 0, A = 0
11 while(Table 2 has more rows){
12      Read the first row X in Table 2
13      CountS = CountS + 1
14      SumS = SumS + X.LetterCount
15      if(X.Word ends with a full stop){
16          C = SumS / CountS
17          if(C < B){
18              A = A + 1
19         }
20          SumS = 0, CountS = 0
21     }
22      Move X to Table 1
23 }

Options
(a)

Number of sentences with average letter count more than the average letter count of dataset

(b)

Number of sentences with average letter count less than the average letter count of dataset

(c)

Number of words with average letter count more than the average letter count per word of
dataset

(d)

Number of words with average letter count less than the average letter count per word of dataset
Answer
(b)

Solution
The pseudocode consists two parts. In the first part variable B is calculated by calculating
variables SumT and CountT.

If we follow carefully, then B stores the average letter count of the words of the "Words" dataset.
Variables SumS and CountS are like same as the variables SumT and CountT respectively, but
these two variables get re-initialized whenever a sentence ends. Therefore, SumS and CountS
represent the number of letters and number of words in a sentence (the current sentence).

Line 16: Variable C is calculated as variable B was calculated but C is calculated as sentence basis
while B was calculated for the whole "Words" dataset. Therefore, C represents the average letter
count per word in a sentence.

Line 18: A is incremented whenever C is less than B, that means A is incremented whenever the
average letter count per word of a sentence is less than the average letter count of the whole
dataset.

Therefore, A represents the number of sentences with average letter count less than the average
letter count of the dataset.

You might also like