Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "Serial.h" 00003 Serial uart1(USBTX,USBRX); 00004 using namespace std; 00005 00006 struct Point 00007 { 00008 int val; 00009 double x, y; 00010 double distance; 00011 00012 }; 00013 00014 bool comparison(Point a, Point b) 00015 { 00016 return (a.distance < b.distance); 00017 } 00018 00019 int classifyAPoint(Point arr[], int n, int k, Point p) 00020 { 00021 for (int i = 0; i < n; i++) 00022 arr[i].distance = 00023 sqrt ((arr[i].x - p.x) * (arr[i].x - p.x) + 00024 (arr[i].y - p.y) * (arr[i].y - p.y)); 00025 00026 //sort (arr, arr+n, comparison); 00027 00028 int freq1 = 0; 00029 int freq2 = 0; 00030 for (int i = 0; i < k; i++) 00031 { 00032 if (arr[i].val == 0) 00033 freq1++; 00034 else if (arr[i].val == 1) 00035 freq2++; 00036 } 00037 00038 return (freq1 > freq2 ? 0 : 1); 00039 } 00040 00041 int main () 00042 { 00043 uart1.baud(9600); 00044 int n = 17; // Number of data points 00045 Point arr[n]; 00046 00047 arr[0].x = 1; 00048 arr[0].y = 12; 00049 arr[0].val = 0; 00050 00051 arr[1].x = 2; 00052 arr[1].y = 5; 00053 arr[1].val = 0; 00054 00055 arr[2].x = 5; 00056 arr[2].y = 3; 00057 arr[2].val = 1; 00058 00059 arr[3].x = 3; 00060 arr[3].y = 2; 00061 arr[3].val = 1; 00062 00063 arr[4].x = 3; 00064 arr[4].y = 6; 00065 arr[4].val = 0; 00066 00067 arr[5].x = 1.5; 00068 arr[5].y = 9; 00069 arr[5].val = 1; 00070 00071 arr[6].x = 7; 00072 arr[6].y = 2; 00073 arr[6].val = 1; 00074 00075 arr[7].x = 6; 00076 arr[7].y = 1; 00077 arr[7].val = 1; 00078 00079 arr[8].x = 3.8; 00080 arr[8].y = 3; 00081 arr[8].val = 1; 00082 00083 arr[9].x = 3; 00084 arr[9].y = 10; 00085 arr[9].val = 0; 00086 00087 arr[10].x = 5.6; 00088 arr[10].y = 4; 00089 arr[10].val = 1; 00090 00091 arr[11].x = 4; 00092 arr[11].y = 2; 00093 arr[11].val = 1; 00094 00095 arr[12].x = 3.5; 00096 arr[12].y = 8; 00097 arr[12].val = 0; 00098 00099 arr[13].x = 2; 00100 arr[13].y = 11; 00101 arr[13].val = 0; 00102 00103 arr[14].x = 2; 00104 arr[14].y = 5; 00105 arr[14].val = 1; 00106 00107 arr[15].x = 2; 00108 arr[15].y = 9; 00109 arr[15].val = 0; 00110 00111 arr[16].x = 1; 00112 arr[16].y = 7; 00113 arr[16].val = 0; 00114 00115 /*Testing Point*/ 00116 Point p; 00117 p.x = 2.5; 00118 p.y = 7; 00119 00120 // Parameter to decide groupr of the testing point 00121 while(1) 00122 { 00123 wait(0.1); 00124 int k = 3; 00125 uart1.printf ("The value classified to unknown point" 00126 " is %d.\n", classifyAPoint(arr, n, k, p)); 00127 //return 0; 00128 } 00129 00130 }
Generated on Tue Jul 12 2022 23:22:43 by
1.7.2