View Single Post
  #12   Spotlight this post!  
Unread 31-12-2009, 00:08
rsisk's Avatar
rsisk rsisk is offline
The GURU Channel
AKA: Richard Sisk
FRC #2493 (Robokong)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Riverside, CA
Posts: 2,749
rsisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond repute
Send a message via MSN to rsisk
Re: Kind of off topic c programming question.

Setup some test cases, try to cover all possibilities:

Code:
case 1st 2nd 3rd  expect
1    0   0   0     0
2    1   2   3     3
3    1   1   3     3
4    3   2   1     3
5    1   3   2     3
6    2   3   1     3
7    3   0   0     3
8    3   3   3     3
9    a   2   ?     not sure
Then run the test cases through your program mentally, i.e.,

Case 1

Code:
if (0 >= 0 && 0 >= 0)
     true, high_value = 0
Case 2
Code:
if (1 >= 2 && 2 >= 3)
     false    
else if (2 >= 2 && 2 >= 3)
          true       false
else if (3 >= 1 && 3 >= 2)
          true      true  high_value = 3
After case 2, you may see your typo in the second test where you compare input_2 to input_2

Run all your test cases and make sure get the results you expect.

Try to include edge conditions, like the case 1 and case 8, where you test upper and lower limits

Force error conditions with the unexpected, like case 9 where non numeric values are input

After all of this, see if you can simplify the code. For example, what if you were dealing with more than three input numbers? The code would get really complex really fast. Maybe there is a better way?
What whould happen if you set high_value to input_1, then compared high_value to input_2, if less, set high_value to input_2. Do the same for input_3.
You could further simplify by putting the values into a loop until an end condition occured.
As you can see, there is more than one way to approach the defined problem.

Hope that helps

Last edited by rsisk : 31-12-2009 at 00:12. Reason: Learned about the CODE tag, sweet!