Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hey everyone i'm having trouble trying to write up a function that accepts an array of whole numbers and returns the difference between the biggest number and the smallest. Help would be much appreciated with an example. Thank you
Posted
Comments
phil.o 19-Mar-14 8:37am    
Well:
- find the smallest number in the collection
- find the greatest number in the collection
- calculate the difference between these two numbers
Which part exactly do you have trouble with? Show us what you wrote so far.

you can try like this...

C#
int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 };
int max = array.Max();
int min = array.Min();

int diff = max - min;
 
Share this answer
 
Iterate the array looking for both the smallest and biggest number. At the end of the iteration compute their difference.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900