Welcome! Log In Create A New Profile

Advanced

Need Help in Assignment 2 2010 Semester 2 Question 1

Posted by Ntobeko 
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
Need Help in Assignment 2 2010 Semester 2 Question 1
October 27, 2010 11:02AM
Hi there, I need help in the following code fragment taken from Assignment 2 2010 Question 1:

cout<<" Does the client want normal size or jumbo photos? " <<endl;
do
{
cout<<" Please enter 'n' for normal or 'j' for jumbo) :";
cin >> type;
}while (type != 'n' && type != 'j'winking smiley;
cout<<" Please key in the film exposure:" << endl;
do
{
cout<<" Valid exposures are 12, 24 and 36 :";
cin>>exposure;
}while (exposure !=12 && exposure !=24 && exposure != 36);

My question is that I do not understand how the do while conditions came about. Or I do not understand how the condition: while (exposure !=12 && exposure !=24 && exposure != 36);
came about, 12 and 24 really confuses me.

Regards

Fellow cos112v student.
avatar Re: Need Help in Assignment 2 2010 Semester 2 Question 1
October 27, 2010 12:28PM
Firstly, you are missing type definitions. The first loop requires type to be defined and looking at what it's being compared with, I'd say that it's a char. In the second loop, I'd say that exposure must be an int. If I redo the code, it becomes.

Language: C++
cout<<" Does the client want normal size or jumbo photos? " <<endl; char type; do { cout<<" Please enter ';n'; for normal or ';j'; for jumbo) :"; cin >> type; }while (type != ';n'; && type != ';j';); cout<<" Please key in the film exposure:" << endl; int exposure; do { cout<<" Valid exposures are 12, 24 and 36 :"; cin>>exposure; }while (exposure !=12 && exposure !=24 && exposure != 36);

Now that that's out the way, the first while loop's condition says, "while type is not the character 'n' and type is not the character 'j' keep looping". Basically, keep looping if neither of those characters are entered by the user.

The second conditional says, "while exposure is not 12 and exposure is not 24 and exposure is not 36, keep looping". Keep looping till one of those three options is entered by the user.

I know that sometimes logical statements might not seem intuitive because we usually don't talk like that amongst ourselves. You would probably say something like, "keep looping till type is either 'n' or 'j'". If you flip this round, you would say "keep looping while type is neither 'n' nor 'j'".

In logic, this last statement would be NOT ('n' OR 'j'winking smiley. Using de Morgan's equivalence[1], this transforms to (NOT 'n' AND NOT 'j'winking smiley

Similarly, you can get the second one by saying, "keep looping till exposure is either 12, 24 or 36". Flipping the logic, but keeping it equivalent, gives you, "keep looping while exposure is neither 12, 24, nor 36".


[1] de Morgan proved that NOT (X OR Y) == NOT X AND NOT Y. The converse is also true.
Also NOT X OR NOT Y == NOT (X AND Y). This form of the proof is not needed in your example but is good to remember.
Re: Need Help in Assignment 2 2010 Semester 2 Question 1
October 29, 2010 02:39PM
I clearly understand the explanation given above and the DeMorgans Laws associated with it thanks for that, but my real question was how do they get 12 and 24 because we were not given in the question ?
avatar Re: Need Help in Assignment 2 2010 Semester 2 Question 1
November 01, 2010 09:51AM
If a question tells you to ask the user to make a choice about something but doesn't tell you what the options are, I think it's safe to assume that they don't mind you using your imagination to determine what the choices must be.

Just remember that whenever you make an assumption in an assignment or an exam, make sure to tell the lecturers that you have made one and why.
Sorry, you do not have permission to post/reply in this forum.