![]() |
|
#1
|
|||
|
|||
k-Nearest Neighbors SearchYour program must observe the following requirements:
1. Maintain the following constant: MAX SIZE = 1000 You must not ask the user to input this value. Recall that to declare a constant integer, you can use: const int MAX SIZE = 1000; 2. User Input | Your program must ask for the following: An integer space size where 0 < space size MAX SIZE, which denotes the \actual" data set size. An integer k where 0 < k space space An integer key of arbitrary range, for which your program will use as the search query (to be described later). If the value entered for k and space size are not within their respective range, then your program must output an error and repeat this process until the condition is satised. 3. The Search Space|Create an array of integers of size MAX SIZE, but only populate the rst space size elements of this array using the pseudo-random number generator, rand(). All numbers that are generated must be normalized to the range of [0; 5000]. Repetitions are permitted. This list must then be sorted in nondecreasing order (see below). 4. The Result Set | Create an array of integers of size MAX SIZE, populating no elements. This array will eventually contain the k nearest neighbors once your algorithm is nished running. 5. You can write any number of functions that might be of use to you. However, the following two are mandatory: 2 void p r i n tLi s t ( cons t i n t l i s t [ ] , cons t i n t l i s t s i z e ) | Returns nothing and outputs the rst list size elements from the list[] array. void query ( cons t i n t key , cons t i n t l i s t [ ] , cons t i n t l i s t s i z e , i n t r e s u l t [ ] , cons t i n t r e s u l t s i z e ) | This function returns nothing, but places the result size integers from list[] that are closest to key into the result[] array. This function may not perform any of the following: (a) Create arrays of arbitrary size. You may only create arrays of constant size like 4, 13, 100, etc. Only constant sized arrays can be created (remember that MAX SIZE is a constant) (b) Print out anything or ask for user input (c) Alter any of the parameters except for the results[] array 6. After each search, use the printList() function on the array holding your result set to print out the k-NN results. Then ask the user if they would like to continue searching for another key, and repeat the search on the SAME space if armative. That is, do not regenerate the random list after each query. 7. Once the user wants to stop running searches, use the printList() function to print out the search space. I don't know how to start could anyone help me with that code? Thanks... |
|||
|
#2
|
|||
|
|||
Re: k-Nearest Neighbors SearchQuote:
1. Read the complete assignment. Don't worry about how you are going to code it. For goodness sake don't actually start writing code until you understand exactly what the program is required to do. 2. Visualize how you are going to test the program. You can even use pencil and paper (gasp) or a text editor to transcribe a "practice session." That is, think about what you, the user, will give the program to work with and exactly what the program will give you in return. 3. Don't skip step 1 or step 2. This is very important. 4. Be sure that step 3 is complete. This is even more important. 5. You can just implement the requirements of the assignment one step at a time in a top-down fashion. Get inputs and make sure they are correct. Then write whatever functionality that you need for the rest of program (the "Good Stuff"). Do it one step at a time. Compile after every few additional input lines. Don't write tens or hundreds of lines of code before trying to compile. Assuming you have done steps 1, 2, 3, 4, you might do something like: CPP / C++ / C Code:
Now might be a good time to implement the part that requires the program to give the user a chance to try another input if an invalid entry is made. Make sure each step that you have implemented here works exactly as it should. Then go on to the "Good Stuff." Regards, Dave |
|
#3
|
|||
|
|||
Re: k-Nearest Neighbors SearchCPP / C++ / C Code:
I have something like that. As you see I created the vector and I need to fill it with random numbers so I added something like that; however, I always get 0000s when I compile CPP / C++ / C Code:
Is there anything wrong with it? By the way I defined the random number as CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: k-Nearest Neighbors SearchQuote:
Then you push more elements into the vector. The first MAX_SIZE elements are still equal to zero. So you can do the following: 1. Declare the vector with nothing in it and then use push_back to fill it up. CPP / C++ / C Code:
or Declare a vector with as many elements that you need. Then use an iterator or assignment to store values. CPP / C++ / C Code:
N.B. I don't see where your assignment even mentions vectors. I mean, experienced C++ programmers might use vectors instead of arrays, but how important is it for you to follow the directions exactly? If I were an instructor (I'm not), I would insist on exact implementation of my detailed program specification. Period. If some on-line guru suggests that "vectors are better than arrays," I couldn't argue with that, but I would still insist that people follow instructions. (From my experience I would assert that this is at least as likely to happen outside the classroom as in, so is an important part of the learning process. But maybe that's just me.) I mean, if you declare an array (as the assignment stated), then use the loop like the one in my second example to store the values, wouldn't it do what you need? Regards, Dave |
|
#5
|
|||
|
|||
Re: k-Nearest Neighbors SearchIf I did it with vectors, I can get extra point
and the code you wrote... CPP / C++ / C Code:
the value will be always same because the size is 1000; however, i need to see the values inside the vector? How can I do that? CPP / C++ / C Code:
now that works... What should I do next? |
|
#6
|
|||
|
|||
Re: k-Nearest Neighbors SearchQuote:
You can look at values of the elements by using an iterator or by indexed notation. Here's an example based on your (broken) code. CPP / C++ / C Code:
Output: Code:
Regards, Dave |
|
#7
|
|||
|
|||
Re: k-Nearest Neighbors SearchNow, I am really confused :S
I need to push random number in the vector, so shouldn't I need to use CPP / C++ / C Code:
Then I wan to see what is in it?? How can I do that? |
|
#8
|
|||
|
|||
Re: k-Nearest Neighbors SearchQuote:
CPP / C++ / C Code:
CPP / C++ / C Code:
Now the vector has 5001 elements. Elements with index values 0 through 4999 have value zero. The element with index number 5000 (the 5001th element) has whatever value you pushed. Please run the example in my most recent post. New stuff was pushed onto the vector. The initial stuff was unchanged by the push. If you want to create a vector of a particular size and then store stuff without creating new elements, then just store values using indexed notation of the second example of my post number 4. No pushing, please. Bottom line: push_back() creates a new place for the value and stores it there. That is, the size of the vector is increased by one, and previous values are unchanged. Look in your text or look for an on-line reference. For example, http://www.cplusplus.com/reference/stl/vector/ Regards, Dave "Over and out." |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unable to search for the nearest value | Yvonne | C Programming Language | 4 | 07-Oct-2005 10:40 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 09:36 |
| GIDForums Search w/ Google | BobbyDouglas | GIDForums™ | 1 | 09-Feb-2004 17:05 |
| Search Engine Positioning 101 and 201 "How To" Tips... | 000 | Search Engine Optimization Forum | 0 | 29-May-2003 11:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The