Cool, so if I get a 69.5 in your class, I get an 'A'. I wish I had you for a professor in schoolElegant in its simplicity.
Cool, so if I get a 69.5 in your class, I get an 'A'. I wish I had you for a professor in schoolElegant in its simplicity.
*cough* I guess I should have read your code before changing it.
Of course, bithub, do you realize how difficult it would have been to get an A that way? But joking aside, the boundaries are correct now.Code:// declaring the determine_grade function char determine_grade(float test_score) { if (test_score >= 80 && test_score < 90) return 'B'; else if (test_score >= 70 && test_score < 80) return 'C'; else if (test_score >= 60 && test_score < 70) return 'D'; else if (test_score >= 0 && test_score < 60) return 'F'; else return 'A'; }
wipe on -A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.- wipe off
someone said something about simplicity?Code:char determine_grade(float test_score) { if (test_score >=90) return 'A'; else if (test_score >= 80) return 'B'; else if (test_score >= 70) return 'C'; else if (test_score >= 60) return 'D'; else return 'F'; }
of course with the way our education system works I think
would be more efficientCode:if (test_score < 60) return 'F'; else if (test_score < 70) return 'D'; else if (test_score < 80) return 'C'; else if (test_score < 90) return 'B'; else return 'A';
Last edited by ಠ_ಠ; 07-27-2009 at 04:43 PM.
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
Haha! Very true on both counts. One could simply change the way grades are calculated.
Then one could use an array, which in this case is probably not any less expensive given the few possible outcomes for a grade.Code:float calc_average(float score_1, float score_2, float score_3, float score_4, float score_5) { float average; average = (score_1 + score_2 + score_3 + score_4 + score_5) / 5.0; if(average < 0) average = 0.0f. return (average <= 100) ? average : 100.0f; }
Hmmmm perhaps?Code:char determine_grade(float test_score) { char *grades = "FDCBA"; int index = (int)(test_score/10) - 6; if(index < -1) index = 0; return grades[index]; }
Code:char determine_grade(float test_score) { char *grades = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDDDDDDDDDDCCCCCCCCCCBBBBBBBBBBAAAAAAAAAAA"; return grades[(int)(test_score)]; }
Last edited by Cooloorful; 07-27-2009 at 05:08 PM.
wipe on -A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.- wipe off
you run the risk of blowing the bounds of your array
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
There, now it doesn't ;-)
wipe on -A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.- wipe off
what if the score is less then 60 in the first case?
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
True dat, ok its becoming overly convoluted even bothering with the first way. Well... both are overly convoluted, but the first method exceeds even my usual way of doing it. Fun is fun and all, but I highly recommend against either. A computer is never so overly bogged down that it can't handle calculating a letter grade without some crazy optimization.
wipe on -A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.- wipe off