# Implementation of K-Nearest Neighbors Algorithm in C++

```#include<iostream.h>

#include<conio.h>

void main() {
int i, j, l;
int tsc = 12;
char gen = {
'F',
'M',
'F',
'F',
'F',
'M',
'F',
'M',
'M',
'F',
'M',
'F'
};
float h = {
1.6 f,
2.0 f,
1.9 f,
1.88 f,
1.7 f,
1.85 f,
1.6 f,
1.7 f,
2.2 f,
1.8 f,
1.95 f,
1.9 f
};
char op = {
"short",
"tall",
"medium",
"medium",
"short",
"medium",
"short",
"short",
"tall",
"medium",
"medium",
"medium"
};

cout << "\n Initial Set:";
cout << "\nGender\tHeight\tOutput";
for (i = 0; i < 12; i++) {
cout << "\n" << gen[i] << "\t" << h[i] << "\t" << op[i];
}

float nh;
char ng;
cout << "\n Enter tuple to be processed (Height,Gender) :";
cin >> nh >> ng;

int t;
cout << "\n Enter threshold:";
cin >> t;

float d, k;

//calculating distance to each value in training set
for (i = 0; i < 12; i++) {
d[i] = i;
k = h[i] - nh;
if (k < 0) {
d[i] = -k;
} else {
d[i] = k;
}
}

//Sorting
for (i = 0; i < 11; i++) {
for (j = 0; j < 11; j++) {
if (d[j] > d[j + 1]) {
k = d[j];
d[j] = d[j + 1];
d[j + 1] = k;

l = d[j];
d[j] = d[j + 1];
d[j + 1] = l;
}
}
}

int nos = 0; //no of shorts
int nom = 0; //no of mediums
int not = 0; //no of talls

cout << "\nGender\tHeight\tOutput\n";
for (i = 0; i < t; i++) {
l = d[i];
cout << gen[l] << "\t" << h[l] << "\t" << op[l] << "\n";
if (strcmp(op[l], "short") == 0) {
nos++;
}
if (strcmp(op[l], "medium") == 0) {
nom++;
}
if (strcmp(op[l], "tall") == 0) {
not++;
}
}

cout << "\n No of shorts:" << nos;
cout << "\n No of medium:" << nom;
cout << "\n No of tall:" << not;

if (nos > nom &amp;&amp; nos > not) {
cout << "\n New Tuple is classified as Short";
}

if (nom > nos &amp;&amp; nom > not) {
cout << "\n New Tuple is classified as Medium";
}

if (not > nom &amp;&amp; not > nos) {
cout << "\n New Tuple is classified as Tall";
}

getch();
}

/* OUTPUT

Initial Set:
Gender  Height  Output
F       1.6     short
M       2       tall
F       1.9     medium
F       1.88    medium
F       1.7     short
M       1.85    medium
F       1.6     short
M       1.7     short
M       2.2     tall
F       1.8     medium
M       1.95    medium
F       1.9     medium
Enter tuple to be processed (Height,Gender) :1.7 M

Enter threshold:5

Gender  Height  Output
F       1.7     short
M       1.7     short
F       1.8     medium
F       1.6     short
F       1.6     short

No of shorts:4
No of medium:1
No of tall:0
New Tuple is classified as Short

*/
```

## 6 thoughts on “Implementation of K-Nearest Neighbors Algorithm in C++”

1. starlight says:

hi, may i know does it include with euclidean formula too?

2. Adil says:

this program is showing the error
” fatal error: iostream.h: No such file or directory
compilation terminated.
“what should i do?

1. hadeer says:

remove (.h) from (#include (iostream.h)) .let it just be (#include (iostream).

3. jeune says:

#include

4. Anam says:

if(strcmp(op[l],”short”)==0)
error on strcmp();
function prototype missing

5. Disha Agrawal says:

You did not consider gender as feature while classifying right?

This site uses Akismet to reduce spam. Learn how your comment data is processed.