GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2005, 16:28
jack223 jack223 is offline
New Member
 
Join Date: Nov 2005
Posts: 15
jack223 is on a distinguished road

find the value in the array that is closest in value to the second parameter


I 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:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

const int MAX = 10; 

int main()
{
    int Array[MAX] = {2, 3, 4, 5, 7, 10, 12, 15, 18, 20}; 
    int num;


    cout << "The following numbers are the values inside of MyArray: " << endl;
for (int i = 0; i < MAX; i++) 
    
        cout << Array[i] << " ";
        cout << endl;

    cout << "Please enter an integer range beteween 1 to 20: ";
    cin >> num;
    cout << endl;

    cout << "what number from inside of Array[] is the closest to?? " << num << endl;


    return 0;
}
Last edited by LuciWiz : 04-Nov-2005 at 01:46. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Nov-2005, 17:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: find the value in the array that is closest in value to the second parameter


Hi 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:
int i = -5;
Then abs(i) will return the absolute value. i.e 5.

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:
int difference, index;
2. difference is used to hold the difference between the num and the Array[i].
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:
index = 0;
difference = abs( num - Array[0]);
4. In a for loop, check whether difference is greater than absolute value of num - any other number Array[i].
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:
for( int i = 0; i < MAX; i++ )
{
    if (difference > abs( num - Array [ i ] ))
    {
        difference = abs( num - Array [ i ] );
        index = i;
    }
}
5. At the end, Array[index] will give you the nearest value and difference will give you the lowest difference.

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  
Old 04-Nov-2005, 07:41
jack223 jack223 is offline
New Member
 
Join Date: Nov 2005
Posts: 15
jack223 is on a distinguished road

Re: find the value in the array that is closest in value to the second parameter


Paramesh,

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  
Old 04-Nov-2005, 07:45
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: find the value in the array that is closest in value to the second parameter


Hi 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  
Old 04-Nov-2005, 07:58
jack223 jack223 is offline
New Member
 
Join Date: Nov 2005
Posts: 15
jack223 is on a distinguished road

Re: find the value in the array that is closest in value to the second parameter


oh..i c...thanks
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.