Question 5: fix a function [7.5]
The function below does not do what the comments say it should.
Fix the function appropriately to match the comments.
(Do NOT alter the comments.)
// this function sorts the three parameters so that // the smallest value winds up in a, the largest in c, // and the "mid" value in b void sort(int &a, int &b, int &c) { int small, middle, large; // find the smallest if (a < b) small = a; else small = b; if (small > c) small = c; // find the largest if (a > b) large = a; else large = b; if (large < c) large = c; // find the middle value if (small < a < large) middle = a; else if (small < b < large) middle = b; else middle = c; // put the values in the right parameters a = small; b = middle; c = large; } |
SAMPLE SOLUTION the only changes needed are to the segment computing the middle value // find the middle value if ((small < a) && (a < large)) middle = a; else if ((small < b) && (b < large)) middle = b; else middle = c; (if you also went through adding the { and } brackets enclosing the bodies of the if/else segments that is perfectly acceptable) |