Write a generalised function KNearest Neighbor which should take an array and a value T and a
parameter K, and whatever you like to pass (which you should) and return (or whatever) K nearest
neighbours in that array. Note: You should use an extra array. write it in c++
Assistant
Here is a generalized function in C++ to find the K nearest neighbors in a given array for a given value:
cpp
User
do it again without alogrithms vectors with simple method
Assistant
User
Write a generalised function KNearest Neighbor which should take an array and a value T and a
parameter K, and whatever you like to pass (which you should) and return (or whatever) K nearest
neighbours in that array. Note: You should use an extra array. DO IT IN C++ WITH EASY METHOD NO VECTORS ALOGRITHMS
Assistant
Certainly! Here's a simplified version of the KNearestNeighbor function in C++ using arrays instead of vectors or algorithms:
cpp
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
struct Neighbor {
int index;
double distance;
};
bool compareNeighbors(const Neighbor& a, const Neighbor& b) {
return a.distance < b.distance;
}
std::vector<int> KNearestNeighbor(const std::vector<int>& array, int T, int K) {
std::vector<Neighbor> neighbors;
for (int i = 0; i < array.size(); i++) {
Neighbor neighbor;
neighbor.index = i;
neighbor.distance = std::abs(array[i] - T);
neighbors.push_back(neighbor);
}
std::sort(neighbors.begin(), neighbors.end(), compareNeighbors);
std::vector<int> kNearestNeighbors;
for (int i = 0; i < K; i++) {
kNearestNeighbors.push_back(array[neighbors[i].index]);
}
return kNearestNeighbors;
}
int main() {
std::vector<int> array = {5, 1, 10, 3, 6, 9, 4, 2, 8, 7};
int T = 4;
int K = 3;
std::vector<int> nearestNeighbors = KNearestNeighbor(array, T, K);
std::cout << "Nearest neighbors to " << T << " are: ";
for (int neighbor : nearestNeighbors) {
std::cout << neighbor << " ";
}
return 0;
}
In this implementation, we define a struct Neighbor to store the index of an element in the array and its distance from the value T. We calculate the distance as the absolute difference between the element and T. We then populate the neighbors vector with these distances and indices.
After that, we sort the neighbors vector based on the distances in ascending order using the std::sort function and a custom comparison function compareNeighbors.
Finally, we iterate over the sorted neighbors vector to extract the K nearest neighbors from the original array and store them in kNearestNeighbors vector. The function KNearestNeighbor returns this vector as the result.
In the main function, we demonstrate the usage of the KNearestNeighbor function by finding the 3 nearest neighbors to the value 4 in the given array and printing them.
Sure! Here's an alternative implementation of the KNearestNeighbor function without using the <algorithm> library and vectors:
cpp
#include <iostream>
#include <cmath>
struct Neighbor {
int index;
double distance;
Neighbor(int idx, double dist) : index(idx), distance(dist) {}
};
bool compareNeighbors(const Neighbor& a, const Neighbor& b) {
return a.distance < b.distance;
}
void bubbleSort(Neighbor arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j].distance > arr[j+1].distance) {
Neighbor temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int* KNearestNeighbor(int array[], int size, int T, int K) {
Neighbor neighbors[size];
for (int i = 0; i < size; i++) {
neighbors[i] = Neighbor(i, std::abs(array[i] - T));
}
bubbleSort(neighbors, size);
int* kNearestNeighbors = new int[K];
for (int i = 0; i < K; i++) {
kNearestNeighbors[i] = array[neighbors[i].index];
}
return kNearestNeighbors;
}
int main() {
int array[] = {5, 1, 10, 3, 6, 9, 4, 2, 8, 7};
int size = sizeof(array)/sizeof(array[0]);
int T = 4;
int K = 3;
int* nearestNeighbors = KNearestNeighbor(array, size, T, K);
std::cout << "Nearest neighbors to " << T << " are: ";
for (int i = 0; i < K; i++) {
std::cout << nearestNeighbors[i] << " ";
}
delete[] nearestNeighbors;
return 0;
}
In this implementation, we define the Neighbor struct as before to store the index and distance. Instead of using a vector, we create an array of Neighbor objects to store the distances and indices.
We use the bubbleSort function to sort the neighbors array based on the distances in ascending order. This is a basic sorting algorithm that iterates over the array multiple times, swapping elements if necessary to place them in the correct order.
Then, we allocate dynamic memory using new to create an array of size K to store the K nearest neighbors. We iterate over the sorted neighbors array to extract the indices from the original array and store them in the kNearestNeighbors array.
Finally, in the main function, we demonstrate the usage of the KNearestNeighbor function by finding the 3 nearest neighbors to the value 4 in the given array and printing them. Don't forget to release the dynamically allocated memory using delete[] to avoid memory leaks.
#include <iostream>
#include <cmath>
void KNearestNeighbor(const int* array, int size, int T, int K, int* result) {
struct Neighbor {
int index;
double distance;
};
Neighbor neighbors[size];
for (int i = 0; i < size; i++) {
neighbors[i].index = i;
neighbors[i].distance = std::abs(array[i] - T);
}
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (neighbors[j].distance > neighbors[j + 1].distance) {
Neighbor temp = neighbors[j];
neighbors[j] = neighbors[j + 1];
neighbors[j + 1] = temp;
}
}
}
for (int i = 0; i < K; i++) {
result[i] = array[neighbors[i].index];
}
}
int main() {
int array[] = {5, 1, 10, 3, 6, 9, 4, 2, 8, 7};
int size = sizeof(array) / sizeof(array[0]);
int T = 4;
int K = 3;
int* nearestNeighbors = new int[K];
KNearestNeighbor(array, size, T, K, nearestNeighbors);
std::cout << "Nearest neighbors to " << T << " are: ";
for (int i = 0; i < K; i++) {
std::cout << nearestNeighbors[i] << " ";
}
delete[] nearestNeighbors;
return 0;
}
In this approach, we define the Neighbor struct inside the KNearestNeighbor function. We create an array of Neighbor objects to store the indices and distances between the array elements and the value T.
We use a basic bubble sort implementation to sort the neighbors array based on the distances in ascending order.
Finally, we iterate over the sorted neighbors array and store the K nearest neighbors' values in the provided result array.
In the main function, we demonstrate the usage of the KNearestNeighbor function by finding the 3 nearest neighbors to the value 4 in the given array and printing them. Again, don't forget to release the dynamically allocated memory using delete[] to avoid memory leaks.