[HRPM] Re:'My Class' Program

Jeff Duffy jduffy at semcor.com
Sat Apr 29 23:14:14 CDT 2000


Valerie wrote:
> I finished correcting my first perl program we reviewed in class today. I
> have another problem... the program only works with whole numbers.  Can you
> give me a hint?  How should I proceed?


 Note how you are generating the average. Nothing wrong with it, but
think carefully about what the result of the math will be:

$average = $total/4;


Let's take a look at your test cases:

  if($average == 4){
        $average = 'A';
   }elsif($average==3){
        $average = 'B';
   }elsif($average==2){
        $average ='C';
   }elsif($average==1){
        $average ='D';
   }else {die
        };


 In any case where the average of the grades works out to be a number
with a decimal part (i.e. not evenly divisible by 4), none of the test
cases match, so it falls through to your default case (the else{}
clause). Since the behavior of your default clause is to 'die', the
program stops.

 This calls for a bit of change in your testing logic. What you want is
to print a letter grade average based on whether your GPA is _at or
above_ a particular score. Try experimenting with the <, >, <=, and >=
numeric test operators to make that work. 

 You can also make your debugging easier by having your else{} clause
print the value of $average before calling die so you can see what is
going on in your decision logic.

 Give this a shot and let me know if you need assistance finding the
answer.



 Thanks again to Val for giving us code to look at. I'd like everyone
else to work on this too, and submit some of their own code for the
class' benefit.

Jeff
-- 
Jeffrey A. Duffy
jduffy at semcor.com
perl -e 'print
pack"H*","4A6566662C20416E6F74686572205065726C204861636B65720A"'



More information about the Norfolk-pm mailing list