week 3 activity 6 q. 2

203 views
Skip to first unread message

Inquisitive Learner

unread,
Nov 2, 2020, 6:38:01 AM11/2/20
to Discussion forum for Computational Thinking
Q. 2.What would be the value stored in the variable marks?   marks = SumMaths(Z) ? 

Answer is 0 because Z doesnt exist in the dataset?

Anand Iyer

unread,
Nov 2, 2020, 7:36:51 AM11/2/20
to Discussion forum for Computational Thinking, Inquisitive Learner
For answering this, look carefully into the code again.

Procedure SumMaths(gen)
Sum = 0
while (Pile 1 has more cards) {
Pick a card X from Pile 1
Move X to Pile 2
if (X.Gender == gen) {
Sum = Sum + X.Maths
}
}
return(Sum)
end SumMaths

In the first step, Sum has been initialized to 0.  

In the next step, we compare the gender in the card and the parameter;  if they compare equal, then math marks are added to Sum, otherwise, Sum is left as it is (at 0).

In our case, none of the cards have Z as gender, so it doesn't compare equal for any card, and Sum doesn't get updated for any card.  It remains 0.  

Finally, it returns the current value of Sum (0) for every card.

Computational Thinking Support 2

unread,
Nov 2, 2020, 7:42:00 AM11/2/20
to Discussion forum for Computational Thinking, anandd...@gmail.com, Inquisitive Learner
Hi,
The above explanation is correct.
If you have still doubt please write us again.

Regards,
Deepak
IITM Online Degree Team
Reply all
Reply to author
Forward
0 new messages