# Searching array

Discussion in 'C' started by Alex_code, Sep 10, 2009.

1. ### Alex_codeNew Member

Joined:
Sep 10, 2009
Messages:
2
0
Trophy Points:
0
Beginner *
new forum *

3 people (1 2 3) have reviewed books and each person has rated 5 books (10-50). The reviews are from 1 (bad) to 9 (great).

10 20 30 40 50 (# of book)
1 6 5 6 7 5
2 7 8 5 4 2
3 2 8 9 5 7

How do I create a program using a 2D array that lets the customer to enter values and then the code finds the answer that matches closest to input by the customer.

For example, if the user inputs a # of 6 for book 10 and 5 for book 50, then the closest match is choice 1.

Any tips/suggestions to get it started

2. ### aVagueNew Member

Joined:
May 2, 2007
Messages:
34
0
Trophy Points:
0
Occupation:
forex
why the closest matc is choice 1 , isnt' it 3?

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
> Any tips/suggestions to get it started

Start with a main function, declare the array, create the input routine, then the search routine.
Do it a bit at a time and get each part working before you move onto the next bit (that's how I write programs).

You may like to skip the input routine and just use fixed values so that you can debug your search routine without having to re-enter the data every time.

For the search algorithm you may need to implement a number of checks. For example you would want to start with a check for an exact match (6 for 10 and 5 for 50 exactly matches 1). Then you might want to perform fuzzy checks where it looks for numbers that are close enough, but this risks returning multiple rows or surprising results. So if they enter 6 for 10 and 5 for 40 the closest match could be 1 because 6 matches exactly, or it could be row 2 because 5 is closer to 4 than to 7 (you would need to decide which constitutes a "closer" match). Or if they enter 8 for 20 and 7 for 30 then this is exactly half way between 2 and 3 and you might want to return both those results.