Translate the following C++ code into functioning C code. I will repost this que
ID: 3910404 • Letter: T
Question
Translate the following C++ code into functioning C code. I will repost this question so you may get more points..
//Program to illustrate the perceptron training rule
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int
main()
{
float w [3]; //array to hold perceptron weights
float t, o;
const double eta = .1;
int i;
int count = 0;
bool correct = 0;
//define and initialize training data
int train [4] [3] = { 0, 0, 0,
0, 1, 0,
1, 0, 0,
1, 1, 1 };
int x0, x1, x2; //will hold the inputs
float weightsum, deltaw1, deltaw2;
//initialize random number generator
srand((unsigned)(time(0)));
rand();
//initialize threshold
x0 = -1;
w[0] = fabs((float)(rand())/(32767/2)-1);
for ( i = 1; i < 3; ++i)
w[i] = (float)(rand())/(32767/2) - 1;
cout << "INITIAL WEIGHTS" << endl
<< "---------------" << endl;
cout << "w0 = " << w[0] << endl;
cout << "w1 = " << w[1] << endl;
cout << "w2 = " << w[2] << endl;
//implement perceptron training rule as long as training examples are
//classified incorrectly
while(!correct)
{
correct = 1;
count++;
for (i = 0; i < 4; ++i)
{
x1 = train[i][0]; //input for x1
x2 = train[i][1]; //input for x2
//find weighted sum of inputs and threshold values
weightsum = x0 * w[0] + x1 * w[1] + x2 * w[2];
//determine output of perceptron
if (weightsum > 0) o = 1;
else o = 0;
//determine true ouput
t = train[i][2];
//if the ouput is incorrect adjust the weights.
if (o != t)
{
deltaw1 = eta * (t - o) * x1;
w[1] = w[1] + deltaw1;
deltaw2 = eta * (t - o) * x2;
w[2] = w[2] + deltaw2;
correct = 0;
}
}
}
//OUTPUT
cout << endl << endl;
cout << "FINAL WEIGHTS" << endl
<< "-------------" << endl;
cout << "w0 = " << w[0] << endl;
cout << "w1 = " << w[1] << endl;
cout << "w2 = " << w[2] << endl;
cout << "The delta rules were invoked " << count << " times"<<endl;
return 0;
}