Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-15-2006, 12:10 PM   #1
Beta Member
 
Join Date: Oct 2006
Posts: 1
Default New to C++, IF/ELSE duplicate code

In my last excercise I have an IF/Else statement & a second pass counter, here is the code:

How can I remove the second pass counter (++pass) & the duplicate line of code? I just need a very simple solution. Thanks.

//Students are allowed to fail one exam, not both, this is how we achieve that goal.
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
else if (course >= HONOURS)
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;

}

else
{
//Since pass+1 is set above, need to reset the counter this way
//so it counts the passed results correctly
++pass;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}
__________________

krisl100 is offline   Reply With Quote
Old 10-17-2006, 04:43 PM   #2
Baseband Member
 
Griz803's Avatar
 
Join Date: Jun 2004
Posts: 89
Default Re: New to C++, IF/ELSE duplicate code

Well, first, use code tags next time, it'll make my reading easier. Second, I'm not completely certain what you want removed or remade from your remarks.

Your original code looked like this
Code:
//Students are allowed to fail one exam, not both, this is how we achieve 
//that goal.
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
else if (course >= HONOURS)
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;

}

else
{
//Since pass+1 is set above, need to reset the counter this way
//so it counts the passed results correctly
++pass;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}
I'm going to assume that you'd like to optimize the if - else constructs and maybe loose a counter. If - else statements can often be out of control, unless you take the time to think hard about the actual order that they fall through in under most likely cases. In most classes, grades will be passing most of the time. Failing marks are the exception. Your logic should reflect that.

I'll highlight code that has been moved in red for down, green for up. Code changes will be in blue. I'd optimize something like this

Code:
bool fail_flag = FALSE;
// Breaking out this condition and assigning a bool flag allows it to carry as 
// guard for all tests with less overhead.  It is marginally possible to pass 
// on grade points and still fumble two exams!
if(midterm < EXAMSPASS && final < EXAMSPASS){fail_flag = TRUE;}


//Students are allowed to fail one exam, not both, this is how we achieve 
//that goal.
if ( fail_flag || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
// This test now actually replaces the default in the else.  It will count all
// that passed with the variable called pass, whether they've honours or
// not.  
if (course >= PASSMARK && !fail_flag )
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;
}
if(course >=HONOURS && !fail_flag)
{
honours++;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}

// I leave it to your ingenuity to find a mechnaism within the loop this is 
// contained in to ASSURE that fail_flag always begins a cycle as FALSE.
// It may work that way because of the placement it has already.
I believe that the above structure solves all your problems. Perhaps not exactly as you meant, but I think it shows how just a positional change or a separate flag can be used to streamline code. The point of the flag is that it is very efficient to test and *NOT* enter code blocks, more at least then entering two and using one to correct for a differential numbering problem. Since the fail_flag is set early, you check it at all stages and there are less ops to read a variable and perform a logical op on it than to perform a couple of extra logical and grouping operations each time. It still guards against double exam pass failure, and a check can easily be performed in each filter segment.

The first if sets fail_flag. The second uses fail_flag and a course vs. PASSMARK comparison to find failed students. The third finds those that passed, at any level. The fourth is something that I threw in extra. You don't actually need it to calculate pass / fail students. But it can do the honour students easily and reliably, without affecting the total number passed. It increments a different variable to keep count of only those that are honour students.

As an exercise to yourself, answer me this: Can the if block used to detect failed students be moved to a position lower in the code? Would it be effective to do so? Are there other changes that might optimize execution?

I'll leave it at that. I hope I've helped you and piqued your interest a bit.
__________________
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 01:07 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0