Welcome! Log In Create A New Profile

Advanced

Q28 Assignment one pls help

Posted by C-go 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Q28 Assignment one pls help
February 23, 2009 02:35PM
Hello my fellow coding warriors cool smiley

Does any of you guys make sense of question 28 of assignment one. According to my knowledge the relational operators >=, < will be executed first then the

logical operator && is executed before logical | | operator. If I were to rewrite that crit bool variable: bool crit = (age >= 200 || popul < 10000 &&

age > 100);


rewrittten:
Language: C++
bool crit = (((popul < 10000) && (age > 100)) ||(age >= 200))

The rewritten crit variable is the same as as the original one, i just ordered it. Then the answer would be no. 5. i.e. the while loop can only be executed if popul < 1000 and age > 100 OR age > 200.

As I am writing this I see option 2 might be the answer as well...I need a second opinion

Perhaps I am confusing the logic sad smiley....anyone seeing things differently and clearly than me?

Thank you in advance.

C-go
avatar Re: Q28 Assignment one pls help
February 24, 2009 08:56AM
Language: C++
bool crit = (age >= 200 || popul < 10000 && age > 100);

If age >= 200 evaluates to TRUE then the compiler won't even bother to evaluated popul < 10000 due to the logical OR (||) operator (ie. only one condition
is evaluated ... which one ? the one that first evaluates to TRUE) and only thereafter the compiler will evaluate age > 100 which will it will set to TRUE (since age was already >= 200) resulting in the
final answer evaluating to TRUE ( TRUE && TRUE = TRUE)


If age >= 200 evalues to FALSE the compiler evaluates popul < 10000 .If finds it to be TRUE then it depends on whether the compiler evaluates age > 100 = TRUE... (1) or FALSE...(2)

(1) ..... TRUE && TRUE = TRUE
(2) ..... TRUE && FALSE = FALSE

C-go try to evaluate each condition and then have a look at the answers ( the correct answer is there ! ) grinning smiley
avatar Re: Q28 Assignment one pls help
February 24, 2009 09:34AM
Mmm... i was leaning towards option 4 but after reading between the lines i have to agree with your original answer.
Why ?? Option 4 states:
4. The loop is executed only if the town is older than 100 years and has fewer than 10000 residents.

This is not true since my previous post
If age >= 200 evaluates to TRUE then the compiler won't even bother to evaluated popul < 10000 due to the logical OR (||) operator (ie. only one condition
is evaluated ... which one ? the one that first evaluates to TRUE) and only thereafter the compiler will evaluate age > 100 which will it will set to TRUE (since age was already >= 200) resulting in the
final answer evaluating to TRUE ( TRUE && TRUE = TRUE)
contradicts it ...
Do you agree ??smile
avatar Re: Q28 Assignment one pls help
February 24, 2009 03:56PM
I say option2...

The condition that the town must be 200 years or older is overwritten by the next conditions that says the loop must also be executed if the populations is less than 10 000 AND older than 100 years
avatar Re: Q28 Assignment one pls help
February 24, 2009 04:05PM
Yip !!!!
I agree with you!!!
If age >= 200 evaluates to FALSE it means that the age is less than 200 BUT it could well be greater than 100
ie. the age is in the range 100 < age > 200
Good job Renate !!thumbs up
Sorry C-go .. Renate got me thinking !!
Re: Q28 Assignment one pls help
February 25, 2009 06:19PM
Hi guys

I think the way I have rewritten the bool crit just makes it a bit easier to see the logic...read any good C++ text book regarding the precedence of operators, specifically logical operators. I am at work as I write this so I am going to read your posts at home, I will let you know my final verdict tomorrow.....

I am also going to submit my assignment this week...Good luck guys.

May the force of the code be with you..

C-go
Sorry, only registered users may post in this forum.

Click here to login