# OR Gate using Perceptron Network

• Difficulty Level : Medium
• Last Updated : 12 Jun, 2019

Perceptron networks come under single-layer feed-forward networks and are also called simple perceptrons. The perceptron network consists of three units, namely, sensory unit (input unit), associator unit (hidden unit), response unit (output unit). The sensory units are connected to associator units with fixed weights having values 1, 0 or -1, which are assigned at random.
The problem is to implement or gate using a perceptron network using c++ code.

 `#include``using` `namespace` `std;``int` `main()``{``    ``//Array for Binary Input``    ``int` `arr = { {0,0},``        ``{0,1},``        ``{1,0},``        ``{1,1}``    ``};`` ` `    ``//Target array for Binary Input``    ``int` `t = {0,1,1,1};`` ` `    ``// Considering learning rate=1``    ``int` `alp = 1;`` ` `    ``// yi = input``    ``// yo = output``    ``int` `w1 = 0, w2 = 0, b = 0, count = 0, i, yi, yo;``    ``int` `dw1,dw2,db;`` ` `    ``while``(1)``    ``{``        ``cout<<``"x1"``<<``" "``<<``"x2"``<<``" "``<<``"b"``<<``" "``<<``"yi"``<<``" "``<<``            ``"yo"``<<``" "``<<``"t"``<<``" "``<<``"dw1"``<<``" "``<<``"dw2"``<<``" "``<<``"db"``<<``            ``" "``<<``"w1"``<<``" "``<<``"w2"``<<``" "``<<``"b"``<= 0)``            ``{``                ``yo = 1;``            ``}``            ``else``            ``{``                ``yo = 0;``            ``}``            ``if``(t[i] == yo)``            ``{``                ``count++;``                ``dw1 = 0;``                ``dw2 = 0;``                ``db = 0;``            ``}``            ``// Calaulating Change in Weight``            ``else``            ``{``                ``dw1 = alp*(t[i] - yo) * arr[i];``                ``dw2 = alp*(t[i] - yo) * arr[i];``                ``db = alp*(t[i] - yo);``            ``}``            ``w1 = w1 + dw1;``            ``w2 = w2 + dw2;``            ``b = b + db;``            ``cout<

Output :

```x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 0 0 -1
0 1 1 -1 0     1 0 1 1 0 1 0
1 0 1 0 1     1 0 0 0 0 1 0
1 1 1 1 1     1 0 0 0 0 1 0

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 0 1 -1
0 1 1 0 1     1 0 0 0 0 1 -1
1 0 1 -1 0     1 1 0 1 1 1 0
1 1 1 2 1     1 0 0 0 1 1 0

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 1 1 -1
0 1 1 0 1     1 0 0 0 1 1 -1
1 0 1 0 1     1 0 0 0 1 1 -1
1 1 1 1 1     1 0 0 0 1 1 -1

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 -1 0     0 0 0 0 1 1 -1
0 1 1 0 1     1 0 0 0 1 1 -1
1 0 1 0 1     1 0 0 0 1 1 -1
1 1 1 1 1     1 0 0 0 1 1 -1

```

My Personal Notes arrow_drop_up