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;
}
Explanation / Answer
#include #include #include #include int main() { float w [3]; //array to hold perceptron weights float t, o; const double eta = .1; int i; int count = 0; int 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; printf("INITIAL WEIGHTS "); printf("--------------- "); printf("w0 = %f ", w[0]); printf("w1 = %f ", w[1]); printf("w2 = %f ", w[2]); //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 printf(" "); printf("FINAL WEIGHTS "); printf("------------- "); printf("w0 = %f ", w[0]); printf("w1 = %f ", w[1]); printf("w2 = %f ", w[2]); printf("The delta rules were invoked %d times ", count); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.