c++ - Would These Be Considered Magic Numbers? -


I have finished writing programs for the programming category, and I do not want to use magic numbers, so here's mine Question:

In the function given below, will my array index be considered a magic number?

Code:

  String CuckooGrid (int s1, intel s2, int s3, double medians) {int int SIZE = 23; Const int LETTER_GRADE_BARRIERS [SIZE] = {400, 381, 380, 361, 360, 341, 340, 321, 320, 301, 300, 281, 280, 261, 260, 241, 240, 221, 220, 201, 200, 181, 180}; Double final grade; String sheet; Final grade = s1 + s2 + s3 + average; If (final grade> = LETTER_GRADE_BARRIERS [1] & amp; amp; and & amp; amp; end grades & lt; = LETTER_GRADE_BARRIERS [0]) {letterGrade = "A"; } And if (final grade> = LETTER_GRADE_BARRIERS [3] & amp; and & amp; final grades & lt; = LETTER_GRADE_BARRIERS [2]) {letterGrade = "A-"; } And if (final grade> = LETTER_GRADE_BARRIERS [5] & amp; amp; and & amp; amp; amp; amp; amp; amp; & amp; last grade & lt; = LETTER_GRADE_BARRIERS [4]) {letterGrade = "B +"; } And if (final grade> = LETTER_GRADE_BARRIERS [7] & amp; amp; and & amp; amp; amp; amp; amp; end & rdquo; = LETTER_GRADE_BARRIERS [6]) {letterGrade = "B"; } And if (final grade> = LETTER_GRADE_BARRIERS [9] and final grades & lt; = LETTER_GRADE_BARRIERS [8]) {letterGrade = "B-"; } And if (Last Grade> = LETTER_GRADE_BARRIERS [11] & amp; amp; & amp; amp; amp; amp; & amp; amp; amp; amp; amp; amp;;; Last Grades & lt; = LETTER_GRADE_BARRIERS [10]) {letterGrade = "C +"; } And if (final grade> = LETTER_GRADE_BARRIERS [13] & amp; & amp; & amp; amp; amp; & amp; amp; amp; amp; amp; amp; end & amp;;; grades & lt; = LETTER_GRADE_BARRIERS [12]) {letterGrade = "C"; } And if (final grade> = LETTER_GRADE_BARRIERS [15] & amp; amp; & amp; amp; amp; amp; & amp; amp; amp; amp; amp; amp; end & rdquo; = LETTER_GRADE_BARRIERS [14]) {letterGrade = "C-"; } And if (final grade> = LETTER_GRADE_BARRIERS [17] & amp; amp; & amp; amp; amp; & amp; amp; amp; amp; amp; amp;; & amp; last grade & lt; = LETTER_GRADE_BARRIERS [16]) {letterGrade = "D +"; } And if (final grade> = LETTER_GRADE_BARRIERS [19] & amp; amp; and & amp; amp; amp; amp; & amp; amp; last grade & lt; = LETTER_GRADE_BARRIERS [18]) {letterGrade = "D"; } And if (final grade> = LETTER_GRADE_BARRIERS [21] & amp; amp; & amp; amp; amp; amp; amp; amp; amp; amp;; & amp; last grade & lt; = LETTER_GRADE_BARRIERS [20]) {letterGrade = "D-"; } And if (final grade & lt; = LETTER_GRADE_BARRIERS [22]) {letterGrade = "failed"; } Return letter grade; }  

Thanks!

Yes, 10 or 1 is probably a magic number.

As long as you are not a real guru, then you are probably allowed to use the powers of independence of two: -)

On one hand, Below it may be a bit more understandable, like something:

  string caggrid (intel s1, intel s2, intel s3, double median) {// grade lookup arrays. If the grade is & gt; = Border [N], string is grade [N] // anything below D is a failure. Static set int range [] = {400, 380, 360, 340,320, 300, 280,260, 240, 220,200,180}; Static const int grade [] = {"A +", "A", "A -", "B +", "B", "B -", "C +", "C", "C", " D + ", 'D', 'D'}; Double Final Grad = s1 + s2 + s3 + average; // Check each element of the array and, if the final grade is equal to or greater then return the grade string. (Final Grad> = Borders [I]) Return Grades [I]; (IITs) = (ii) LIT; Size (Limits) / Size (* Limits); I ++); // Otherwise, failed. Return "unsuccessful"; }  

This removes the magic number that spreads all those suspects, where it is immediately apparent how they work (you align them well).

This also removes a problem in your basic solution that we do with people who achieve a score of 380.5 - it is not fair to fail those BOSs <- > It is not - or to assign a grade to "" / code> 400 as above (because "A +" does not appear to return ).


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -