-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.cpp
More file actions
108 lines (99 loc) · 1.27 KB
/
01.cpp
File metadata and controls
108 lines (99 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
using namespace std;
int t;
double w1=0.5,w2=0.5,b=0;
int fnc;
class data
{
public:
int in1;
int in2;
int out;
};
data d[100];
int check_fnc()
{
int flag=0;
for(int i=1;i<=t;i++)
{
if(d[i].out==0 || d[i].out==1)
flag=1;
else if(d[i].out==1 || d[i].out==-1)
flag=2;
}
if(flag==1)
return 0;
else
return 1;
}
void update(int i, int a)
{
double e;
e=d[i].out-a;
w1=w1+e*d[i].in1;
w2=w2+e*d[i].in2;
b=b+e;
}
void set_ukw()
{
int a,tmp;
int count=0;
for(int i=1;i<=t;i++)
{
if(count==t)
{
break;
}
if(fnc==0)
{
tmp=(w1*d[i].in1)+(w2*d[i].in2)+b;
if(tmp>=0)
a=1;
else if(tmp<0)
a=0;
}
else if(fnc==1)
{
tmp=(w1*d[i].in1)+(w2*d[i].in2)+b;
if(tmp>=0)
a=1;
else if(tmp<0)
a=-1;
}
if(a==d[i].out)
count++;
else
{
count=0;
update(i,a);
}
if(i==t)
i=0;
}
}
int main()
{
int x,y,n,r;
cout<<"Enter no. of training sets: ";
cin>>t;
cout<<"Enter the input sets: "<<endl;
for(int i=1;i<=t;i++)
{
cin>>d[i].in1>>d[i].in2>>d[i].out;
}
fnc=check_fnc();
set_ukw();
//cout<<fnc;
cout<<w1<<"\t"<<w2<<"\t"<<b<<endl;
for(int i=1;i<=t;i++)
{
cout<<"Enter your values: ";
cin>>x>>y;
n=w1*x+w2*y+b;
if(n>=0)
r=1;
else
r=0;
cout<<"Result: "<<r<<endl;
}
}