![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
find the value in the array that is closest in value to the second parameterI have no clue where to start on this one.
The function finds the value in the array that is closest in value to the second parameter. For example, if the array had the values {5, -3, 18, 9, 4} andn the second parameter was 11, then the function would return 9. If two values are equally distant, return either. this is how i started. CPP / C++ / C Code:
Last edited by LuciWiz : 04-Nov-2005 at 01:46.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|||
|
#2
|
||||
|
||||
Re: find the value in the array that is closest in value to the second parameterHi Jack,
Please read the guidelines before posting again. You should insert your code between [c++] and [/c++] tags. Thank you. To find the closest value, we must use the abs function. The abs function will return the absolute value of an integer. For example, if we have an integer say CPP / C++ / C Code:
Now to the program: You started in a good way. Now the variable num holds the user input value. We should check which number is the closest to the variable num. Here is the logic: 1. Use two variable difference and index. CPP / C++ / C Code:
Index is used to hold the position of the number which has the lowest difference. 3. Initialize index to 0. Initialize difference to absolute value of num - Array[0]. CPP / C++ / C Code:
If it is true, then assign difference as the absolute value of num and Array[i] to difference and index to i. CPP / C++ / C Code:
Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: find the value in the array that is closest in value to the second parameterParamesh,
your code doesn't work. Array[MAX} = {2,3,4,5,7,10,12,15,18,20}; if the user enters num = 13 i'll get index = 6, difference = 1....instead of index = "12". any suggestion?? |
|
#4
|
||||
|
||||
Re: find the value in the array that is closest in value to the second parameterHi Jack,
It works correctly. What is index? it is the position of the array. So, index = 6 means Array[6] = 12! it is correct isnt it? Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#5
|
|||
|
|||
Re: find the value in the array that is closest in value to the second parameteroh..i c...thanks
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 10:25 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 21:30 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 10-Mar-2004 00:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The