Please do this in C and test the following Implement the Pe
Please do this in C++ and test the following :
Implement the Perceptron learning algorithm. Test your program, for the following two problems:Solution
weights for storing the weights
Threshold to store the threshold
sum to store the weighted sum
output to store the 0 or 1 after comparing the sum with the threshold
values to store the values
given_value is the value that will entered by the user to test
i, j, k are loop controller
new_start is an indicator whether to start from the beginning or not while weights have been found ok for one of the 16 values
option has been used to take input from the user for program flow control
#include<iostream.h>
#include<conio.h>
int main()
{
double weights[4], Threshold = 0.5, sum = 0.0, output=0.0;
int values[16][4], given_value[4], i, j, k;
char* new_start = \"Not Required\";
char option;
//---(calculating and storing the values for the values)
i=0;
j=0;
while(i<16)
{
j=3;
k=i;
while(j>=0)
{
values[i][j]=k%2;
k=k/2;
j--;
}
i++;
}
//------------------------------------------------------
//starting the main loop
option=\'s\';
while(option==\'s\' || option==\'S\')
{
cout<<\"\ ---No kind of input error has been handled, enter accurate values)-----\";
cout<<\"\ Please input the weights:\ \";
j=0;
while(j<4)
{
cout<<\"\\tWeights: \";
cin>>weights[j]; //taking the weights from the user
j++;
}
cout<<\"Please input the Threshold : \";
cin>>Threshold; //taking threshold from the user
i=0;
while(i<16) //to run the loop 16 times (0-15)
{
sum=0;
j=0;
while(j<4)
{
sum += (values[i][j]*weights[j]); //calculating the weighted sum
j++;
}
if(sum < Threshold) //comparing weighted sum with threshold
output = 0; //and setting the value for the output
else
output = 1;
if(i < 8) //to find which class the value is from, it indicates that this is from class A (0-7)
{
if(output != 0) //The desired output of class A is 0, but if not then we will decrease
{ //that corresponding weight of the input
j=0;
while(j<4)
{ //multiplying with corresponding values because we need to
weights[j] -= ((0.1) * (values[i][j])); //that weight which correspondingly having 1 in its input
j++; //if value is 0, \"((0.1) * (values[i][j]))\" will result 0
} //thus the weight will not decrease, otherwise, it will
new_start = \"Required\"; //as wrong weights has been found, thus after we have decreased the weigths
i=i; //we will test these weights for these inputs again and after ok, this value of
} //new_start will indicate to start from first value with these new corrected weights
else
{
if(new_start==\"Required\") //when weights has been found ok and new_start = \"Required\", it means we will start from first
{
new_start=\"Not Required\"; //Untill we found these weight faulty for any value we will just go on
i=0;
}
else
i++; //weights are ok, so go on
}
}
else //this is class B part and same as previous, only that we will increase weights here
{
if(output == 1)
{
if(new_start==\"Required\")
{
new_start=\"Not Required\";
i=0;
}
else
i++;
}
else
{
j=0;
while(j<4)
{
weights[j] += ((0.1) * (values[i][j]));
j++;
}
new_start = \"Required\";
i=i;
}
}
}
cout<<\"\ \ Weights are \"<<weights[0]<<\" , \"<<weights[1]<<\" , \"<<weights[2]<<\" , \"<<weights[3]<<\" while threshold is \"<<Threshold;
cout<<\"\ \ ---------------------(Weights are set, ready to percept)---------------------\";
option=\'e\';
while(option==\'e\' or option==\'E\')
{
cout<<\"\ \ Please enter an element to find which class is it of : \\t\";
j=0;
while(j<4)
{
cin>>given_value[j]; //taking the value to test from user
cout<<\"\\t\\t\\t\\t\\t\\t\\t\";
j++;
}
sum=0;
j=0;
while(j<4)
{
sum += (given_value[j]*weights[j]); //getting the weight
j++;
}
if(sum < Threshold) //comparing the weight with the threshold and getting the result
cout<<\"\ The percepted element \"<<given_value[0]<<given_value[1]<<given_value[2]<<given_value[3]<<\" is from Class A\ \";
else
cout<<\"\ The percepted element \"<<given_value[0]<<given_value[1]<<given_value[2]<<given_value[3]<<\" is from Class B\ \";
getch();
}
}
return 0;
}




