You are on page 1of 3

Abdullah Umer

70067110

#include <iostream>

#include <cmath>

using namespace std;

int main()

float data[8][7]={ {2,3,1,2,4,1,0},

{1,5,6,4,6,7,0}, {2,1,4,2,3,1,0},

{4,5,1,2,3,1,0}, {2,4,3,5,6,0,1},

{1,5,4,3,3,0,1}, {1,4,2,6,3,1,0},

{2,1,4,1,5,1,0} };

// first 5 are distance inputs, 6 is 1(Y)

or 0(N), 7 is the euclidean distance

int k, temp;

float inData[6];

cout << "Enter number of neighbours

(k): ";
Abdullah Umer
70067110

cin >> k;

cout << "Enter data to find the nearest

" << k << " neighbours: ";

cin >> inData[0] >> inData[1] >>

inData[2] >> inData[3] >> inData[4];

inData[5]=0;

for (int counter=0; counter<8;

counter++)

temp=0;

for (int counter1=0; counter1<5;

counter1++)

temp+=pow(inData[counter1]-

data[counter][counter1],2);

data[counter][6]=sqrt(temp);

//Sort

for (temp=0; temp<7; temp++)

for (int temp1=0; temp1<8-temp-1;

temp1++)

if (data[temp1]

[6]<data[temp1+1][6])

for (int temp2=0; temp2<7;

temp2++)
Abdullah Umer
70067110

swap(data[temp1][temp2],data[temp1+1

][temp2]);

cout << "\nThe " << k << " neighbours:

";

for (temp=0; temp<k; temp++)

cout << data[temp][5] << " ";

You might also like