I'm programing on Code Composer Studio a program that generate and show a sinusoid, this program should normally be implemented in a DSP, but since I don't have the DSK I'm just compiling it and trying to show the result in CCS.
I'm having a problem in the line 18 it shows that an expression is expected and I don't know why. I checked all comas and () {} and it seems correct.
#include <math.h>
#include <stdio.h>
const int sine_table[40] = { 0, 5125, 10125, 14876, 19260, 23170, 26509, 29196, 31163, 32364, 32767, 32364, 31163, 29196, 26509, 23170, 19260, 14876, 10125, 5125, 0, -5126, -10126, -14877, -19261, -23171, -26510, -29197, -31164, -32365, -32768, -32365, -31164, -29197, -26510, -23171, -19261, -14877, -10126, -5126 };
int i = 0;
int x1 = 0;
int x2 = 0;
float y = 0;
float sin1(float phase) {
x1 = (int) phase % 40; if (x1 < 0) x1 += 40; x2 = (x1 + 1) % 40;
y = (sine_table[x2] - sine_table[x1]) * ((float) ((int) (40 * 0.001 * i * 100) % 4100) / 100 - x1) + sine_table[x1];
return y;
}
int main(void) {
double pi = 3.1415926535897932384626433832795;
for (int i = 0; i < 1000; i++) {
float x = 40 * 0.001 * i;
float radians = x * 2 * pi / 40;
printf("%f %f %f\n", x, sin1(x) / 32768, sin(radians));
i = i + 1;
}
}
I tried to implement this code and it works to a certain point (x<0.6). I am just wondering why it ouputs 'inf' although the stop criteria should terminate the program when it reaches the maximum accuracy of double.
#include <stdio.h>
#include <math.h>
double fak(int n) {
int f = 1;
int i = 0;
do {
i++;
f *= i;
} while(i<n);
return f;
}
double func_e() {
double res = 0;
double res_old = 0;
double x, k;
x = 1;
k = 0;
do {
res_old = res;
res += ((pow(x,k)) / fak(k));
k++;
} while(res != res_old);
return res;
}
int main(void) {
//printf("power %f", pow(3,3));
printf("%f", func_e());
//printf("%f", fak(3));
printf("\n");
return 0;
}
Check the return value of your function fak. It will overflow and at a certain point return 0. The division by 0.0 results in inf.
When I modify function fak as
double fak(int n) {
int f = 1;
int i = 0;
do {
i++;
f *= i;
} while(i<n);
printf("fak(%d) = %d\n", n, f);
return f;
}
and run it on https://onlinegdb.com/ZxaXfI5xcG, the output is
fak(0) = 1
fak(1) = 1
fak(2) = 2
fak(3) = 6
fak(4) = 24
fak(5) = 120
fak(6) = 720
fak(7) = 5040
fak(8) = 40320
fak(9) = 362880
fak(10) = 3628800
fak(11) = 39916800
fak(12) = 479001600
fak(13) = 1932053504
fak(14) = 1278945280
fak(15) = 2004310016
fak(16) = 2004189184
fak(17) = -288522240
fak(18) = -898433024
fak(19) = 109641728
fak(20) = -2102132736
fak(21) = -1195114496
fak(22) = -522715136
fak(23) = 862453760
fak(24) = -775946240
fak(25) = 2076180480
fak(26) = -1853882368
fak(27) = 1484783616
fak(28) = -1375731712
fak(29) = -1241513984
fak(30) = 1409286144
fak(31) = 738197504
fak(32) = -2147483648
fak(33) = -2147483648
fak(34) = 0
fak(35) = 0
inf
This means your loop ends when both res and res_old have the value inf.
Additional remark:
In func_e you use double k; and pass this to double fak(int n) which converts the value to int. Function fak does the calculation in int and implicitly converts the result to double in the return statement.
I suggest to avoid these conversions. (Or at least think about the possible problems.) The compiler may warn about this if you enable all warnings.
I am trying to make a C program that calculates the value of Pi from the infinite series, aka Leibniz series, and display it to the user. My problem is that I need to display a special message that appears when the program hits the first 3.14, and the first 3.141. That special message should include in which iteration of the loop did the the number become 3.14 and 3.141. I am not lazy so a found a way to make the infinite series but the second part I couldn't figure out, so what should I add to my code to make it display the special message?
#include <stdio.h>
int main(void) {
int i, den; // denominator and counter
double pi = 4;
for (i = 0; i < 10000; i++) {
den = i * 2 + 3;
// (4 - 4/3 + 4/5 -4/7 + 4/9 -......)
if (i % 2 == 0) {
pi = pi - (4.0 / den);
}
else {
pi = pi + (4.0 / den);
}
printf("pi = %lf\n", pi);
}
}
Here's a possible solution:
#include<stdio.h>
#include <math.h>
int
main (void)
{
int i, den; //denominator and counter
int prec = 0;
double pi = 4;
for (i = 0; i < 10000; i++)
{
den = i * 2 + 3;
//(4 - 4/3 + 4/5 -4/7 + 4/9 -......)
if (i % 2 == 0)
pi -= 4.0 / den;
else
pi += 4.0 / den;
//printf ("pi = %lf\n", pi);
if (prec < 1 && trunc (100 * pi) == 314)
{
printf ("Found 3.14 at iteration %d\n", i);
prec++;
}
if (prec < 2 && (int)trunc (1000 * pi) == 3141)
{
printf ("Found 3.141 at iteration %d\n", i);
prec++;
}
}
}
The output is:
pi = 2.666667
pi = 3.466667
pi = 2.895238
...
pi = 3.150140
pi = 3.133118
pi = 3.149996
Found 3.14 at iteration 117
...
pi = 3.141000
pi = 3.142185
pi = 3.141000
Found 3.141 at iteration 1686
...
Here is a version that compares the first n digits of a double cmp_n(). Variables use minimal scope. The variable oracle holds the truncated pi to n decimals. The values of oracle must be stored in ascending order. I tweaked the pi formula to be a bit more compact format.
#include <math.h>
#include <stdio.h>
int cmp_n(double d1, double d2, size_t n) {
return fabs(trunc(pow(10, n) * d1) - trunc(pow(10, n) * d2)) < 1.0;
}
int main() {
double pi = 4;
size_t o = 0;
struct {
double pi[;
size_t n;
} oracle[] = {
{ 3.14, 2 },
{ 3.141, 3 }
};
for (int i = 0; i < 10000; i++) {
int den = i * 2 + 3;
//(4 - 4/3 + 4/5 -4/7 + 4/9 -......)
pi += ((i % 2) ? 4.0 : -4.0) / den;
int special = 0;
if(
o < sizeof(oracle) / sizeof(*oracle) &&
cmp_n(pi, oracle[o].pi, oracle[o].n)
) {
special = 1;
o++;
}
printf("pi = %.15f%2s\n", pi, special ? "*" : "");
}
}
and the relevant data (with line numbers);
$ ./a.out | nl -v0 | grep '*'
117 pi = 3.149995866593470 *
1686 pi = 3.141000236580159 *
Note: you need to add the "%.15lf" format string other the pi output is rounded. double only gives you about 15 digits, and the cmp_n() scales the number and this may not work as expected as you get close to the precision supported by double.
The problem is the following: Given "ABC+DEF=GHI" format string, where A,B,C etc. represent unique digits, find the expression that gives maximum GHI. Ex: Input string is AAB+AAB=AAB, then there's no solution. If it is instead AAA + BBB = AAA, a solution is 999 + 000 = 999. Another example string: ABC + CBA = GGG, a result is => 543 + 345 = 888.
I have ruled out impossible cases easily. The algorithm I have in mind is a bruteforce, that simply tries maximizing the rhs first. However my problem was doing this fast, and also watching out for the unique digits. What's an efficient way to solve this problem?
Notes: I wish to solve this in a singlethreaded approach, and my current problem is detecting if a unique digit is used in "assign_value" function. Perhaps a better method to assign values is there?
EDIT: As per smci's suggestion, here's what I want to achieve, in the very end: ABRA + CADABRA + ABRA + CADABRA == HOUDINI ; 7457 + 1797457 + 7457 + 1797457 == 3609828 -- A system that can handle not only strings of the form I provided in the beginning (3 digit number + 3 digit number = 3 digit number) but also those. However it doesn't hurt to start simple and go with the solution of format I gave :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXPRESSION_SIZE 11 + 1
#define MAX_VARIABLES 9
int variables_read[MAX_VARIABLES] = { 0 };
struct variable {
int coefficient;
int* ptr;
int side;
int canhavezero;
unsigned value_max;
};
typedef struct variable Variable;
struct equation {
Variable* variables[9]; // max
unsigned distinct_on_rhs;
unsigned var_count;
};
typedef struct equation Equation;
int int_pow(int n, int k) {
int res = 1;
for(int i = 0; i < k; ++i)
res *= n;
return res;
}
void AddVariable(Equation* E, Variable* V) {
E->variables[E->var_count++] = V;
}
int IsImpossible(char* expression) {
// if all letters are same or end letters are same, no solution
if(
(expression[0] == expression[4] && expression[0] == expression[8]) ||
(!strncmp(expression, expression + 4, 3) && !strncmp(expression, expression + 8, 3))
)
return 1;
return 0;
}
int assign_value(Equation* E, int pos, int* values) {
if(!E->variables[pos]->value_count) {
if(pos < 0)
return 2;
// if no possible values left, reset this, but take one value count from the closest variable
E->variables[pos - 1]->value_count--;
E->variables[pos]->value_count = E->variables[pos]->value_max;
return 0;
}
int i;
for(i = 9; i >= 0 && values[i] == -1; --i)
printf("Assigning %d to %c\n", E->variables[pos]->value_set[E->variables[pos]->value_count - 1], 'A' + (E->variables[pos]->ptr - E->variables[0]->ptr));
*(E->variables[pos]->ptr) = values[i];
values[i] = -1; // we have unique numbers
return 0;
}
int isSolved(Equation E) {
int sum = 0, coeff = 0;
printf("Trying...\n");
for(int i = 0; i < E.var_count; ++i) {
coeff = E.variables[i]->coefficient * (*E.variables[i]->ptr);
printf("%d ", *E.variables[i]->ptr);
if(E.variables[i]->side)
coeff *= -1;
sum += coeff;
}
printf("\nSum was %d\n", sum);
return !sum;
}
char* evaluate(char* expression) {
char* res;
// check for impossible cases first
if(IsImpossible(expression)) {
res = (char *) malloc(sizeof(char) * strlen("No Solution!"));
strcpy(res, "No Solution!");
return res;
}
res = (char *) malloc(sizeof(char) * MAX_EXPRESSION_SIZE);
// now try to find solutions, first describe the given characters as equations
Equation E;
E.var_count = 0;
E.distinct_on_rhs = 0;
int side_mode = 0, powcounter = 0;
int a = -1, b = -1, c = -1, d = -1, e = -1, f = -1, g = -1, h = -1, i = -1;
int* max_variables[MAX_VARIABLES] = { &a, &b, &c, &d, &e, &f, &g, &h, &i };
for(int j = 0; j < MAX_EXPRESSION_SIZE - 1; ++j) {
if(expression[j] == '+')
continue;
if(expression[j] == '=') {
side_mode = 1;
continue;
}
Variable* V = (Variable *) malloc(sizeof(Variable));
// we know we always get 3 digit numbers but we can easily change if we need to
V->coefficient = int_pow(10, 2 - (powcounter % 3));
V->ptr = max_variables[expression[j] - 'A'];
V->side = side_mode;
E.distinct_on_rhs += side_mode && !variables_read[expression[j] - 'A'];
if(!(powcounter % 3)) { // beginning of a number
V->value_count = 9;
V->value_max = 9;
V->canhavezero = 0;
}
else {
V->value_count = 10;
V->value_max = 10;
V->canhavezero = 1;
}
AddVariable(&E, V);
variables_read[expression[j] - 'A'] = 1;
++powcounter;
}
for(int j = 0; j < E.var_count; ++j)
printf("%d %c %d\n", E.variables[j]->coefficient, 'A' + (E.variables[j]->ptr - max_variables[0]), E.variables[j]->side);
// we got a representaion of the equation, now try to solve it
int solved = 0;
// O(9^N), where N is number of distinct variables.
// An optimization we can do is, we first assign possible max values to rhs number, then go down. We need max number.
printf("Distincts: %d\n", E.distinct_on_rhs);
do {
// try to assign values to all variables and try if it solves the equation
// but first try to assign rhs as max as possible
int values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int temp = E.var_count - E.distinct_on_rhs;
while(temp < E.var_count) {
solved = assign_value(&E, temp, values);
++temp;
}
for(int j = E.var_count - 1 - E.distinct_on_rhs; j >= 0; --j)
solved = assign_value(&E, j, values);
if(solved) // can return no solution
break;
printf("Solving...\n");
solved = isSolved(E);
system("PAUSE");
} while(!solved);
if(solved == 2) {
res = (char *) malloc(sizeof(char) * strlen("No Solution!"));
strcpy(res, "No Solution!");
}
else {
}
return res;
}
int main() {
char expression[MAX_EXPRESSION_SIZE] = { 0 };
do {
printf("Enter the formula: ");
scanf("%s", expression);
char* res = evaluate(expression);
printf("%s\n", res);
free(res);
} while(expression[0] != '-');
return 0;
}
I would start with the result. There are not that many different cases:
AAA
AAB, ABA, BAA
ABC
All other cases can be reduced to these by renaming the variables. ABC + CBA = GGG would become DBC + CBD = AAA.
Then you have
10 possible solutions for the one-variable case AAA
90 (10*9) for the two variable cases
720 (10*9*8) for the three variable case
assuming that zero is allowed anywhere. If not, you can filter out those that are not allowed.
This sets the variables for the right side of the equation. Each variable that appears only on the left, adds possible solutions. B adds a factor of 9, C a factor of 8, D 7 and so forth.
The most "efficient" solution would take all knowledge of the task and simple print the result. So the question is how much of the conditions can be coded and where and what flexibility is needed.
An alternative is to view the generation of test cases and evaluation of them separately.
A simple recursion function can generate the 10! (362880) test cases of unique digits.
unsigned long long count = 0;
unsigned long long sol = 0;
void evaluate(int object[]) {
count++;
int ABC = object[0] * 100 + object[1] * 10 + object[2];
int DEF = object[3] * 100 + object[4] * 10 + object[5];
int GHI = object[6] * 100 + object[7] * 10 + object[8];
if (ABC + DEF == GHI) {
printf("%4llu %03d + %03d = %03d\n", ++sol, ABC,DEF,GHI);
}
}
void form_combos(int pool[], size_t pool_count, int object[],
size_t object_count, size_t object_count_max) {
if (object_count >= object_count_max) {
evaluate(object);
return;
}
assert(pool_count > 0);
int *pool_end = pool + pool_count - 1;
for (size_t p = 0; p < pool_count; p++) {
int sample = pool[p]; // take one out
pool[p] = *pool_end; // replace it with the end
object[object_count] = sample;
form_combos(pool, pool_count - 1, object, object_count + 1,
object_count_max);
pool[p] = sample; // restore pool item
}
}
int main() {
int pool[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
size_t pool_size = sizeof pool / sizeof pool[0];
#define object_count 9
int object[object_count];
form_combos(pool, pool_size, object, 0, object_count);
printf("Evaluate() iterations %llu\n", count);
}
Output
1 091 + 762 = 853
2 091 + 763 = 854
3 091 + 735 = 826
...
1726 874 + 061 = 935
1727 875 + 046 = 921
1728 876 + 045 = 921
Evaluate() iterations 3628800
What is nice about this approach is that if the task was now find
ABC*ABC + DEF*DEF == GHI*GHI
Changing only 2 lines of code:
if (ABC*ABC + DEF*DEF == GHI*GHI) {
printf("%4llu sqr(%03d) + sqr(%03d) = sqr(%03d)\n", ++sol, ABC,DEF,GHI);
}
results in
1 sqr(534) + sqr(712) = sqr(890)
2 sqr(546) + sqr(728) = sqr(910)
3 sqr(712) + sqr(534) = sqr(890)
4 sqr(728) + sqr(546) = sqr(910)
Evaluate() iterations 3628800
Ok, so for a trivial solution (a base to build a generalization on, so far it only works on the format <3 digit number> + <3 digit number> = <3 digit number>) inspired from #chux and #alain's suggestions is the following code. It truly runs on O(10^N) where N is the distinct number of digits present, or variables if you'd like to call them that. I'll see if I can generalize this even further.
Note that this is for the initial problem of finding the largest rhs. Take that into account as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIGITS 10
#define MAX_VARIABLES 9
#define MAX_EXPRESSION_SIZE 11
int IsImpossible(char* expression) {
// if all letters are same or end letters are same, no solution
if(
(expression[0] == expression[4] && expression[0] == expression[8]) ||
(!strncmp(expression, expression + 4, 3) && !strncmp(expression, expression + 8, 3))
)
return 1;
return 0;
}
int ArePointersAssigned(int*** pointers) {
for(int i = 0; i < MAX_VARIABLES; ++i) {
if(**pointers[i] == -1)
return 0;
}
return 1;
}
int evaluate(int*** pointers) {
int ABC = *(*pointers[0]) * 100 + *(*pointers[1]) * 10 + *(*pointers[2]);
int DEF = *(*pointers[3]) * 100 + *(*pointers[4]) * 10 + *(*pointers[5]);
int GHI = *(*pointers[6]) * 100 + *(*pointers[7]) * 10 + *(*pointers[8]);
if (ABC + DEF == GHI) { // since we use dfs, if this is a solution simply return it
//printf("%d + %d = %d\n", ABC, DEF, GHI);
return 1;
}
return 0;
}
// use the solved pointer to escape recursion early
// check_end checks if we reached 6 for the 2nd time, if it's first time we ignore (because it's start state)
void form_combos(int pool[], int pool_count, int object_count, int*** pointers, int* solved) {
if(object_count == MAX_DIGITS - 1)
object_count = 0;
if(*solved) // if a branch solved this, escape recursion
return;
if (ArePointersAssigned(pointers)) { // that means we got a full equation set
*solved = evaluate(pointers);
if(*solved)
return;
}
int *pool_end = pool + pool_count - 1;
for (int p = pool_count - 1; p >= 0 && !*solved; p--) {
int sample = pool[p]; // take one out
pool[p] = *pool_end; // replace it with the end
int temp = **pointers[object_count];
if(**pointers[object_count] == -1)
**pointers[object_count] = sample;
form_combos(pool, pool_count - 1, object_count + 1, pointers, solved);
pool[p] = sample; // restore pool item
if(!*solved)
**pointers[object_count] = temp;
}
}
int main() {
char expression[MAX_EXPRESSION_SIZE] = { 0 };
printf("Enter the formula: ");
scanf("%s", expression);
while(expression[0] != '-') {
if(IsImpossible(expression))
printf("No solution!\n");
else {
int digits[MAX_DIGITS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int object[MAX_VARIABLES] = { -1, -1, -1, -1, -1, -1, -1, -1, -1 }; // stack for dfs
int *A = &object[0], *B = &object[1], *C = &object[2],
*D = &object[3], *E = &object[4], *F = &object[5],
*G = &object[6], *H = &object[7], *I = &object[8];
// set same pointers
int** pointers[MAX_VARIABLES] = { &A, &B, &C, &D, &E, &F, &G, &H, &I };
// analyze the equation
int var = 0;
for(int p = 0; p < MAX_EXPRESSION_SIZE; ++p) {
if(expression[p] >= 'A' && expression[p] <= 'I') {
*pointers[var++] = &object[expression[p] - 'A']; // link same pointers
}
}
int solved = 0, check_end = 0;
form_combos(digits, MAX_DIGITS, MAX_DIGITS - 4, pointers, &solved);
if(!solved) // it can be unsolvable still
printf("No solution!\n");
else
printf("%d%d%d + %d%d%d = %d%d%d\n", *A, *B, *C, *D, *E, *F, *G, *H, *I);
}
printf("Enter the formula: ");
scanf("%s", expression);
}
return 0;
}
I'm trying to implement an easy backpropagation algorithm for an exam (I'm a beginner programmer).
I've got a set of arrays and I generate random weights to start the algorithm.
I implemented the activation function following the math formula:
formula
(where x index are for inputs and y index is for hidden neuron input)
My problem is that I get some summation results with very high exponential values that are incompatible with what I'd expect to be.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define INPUT_NEURONS 4
#define HIDDEN_NEURONS 7
#define OUTPUT_NEURONS 3
#define MAX_SAMPLES 150
#define LEARNING_RATE 0.1
#define RAND_WEIGHT ((double)rand()/(RAND_MAX+1))
double IHweight[INPUT_NEURONS][HIDDEN_NEURONS]; /* in->hid weight */
double HOweight[HIDDEN_NEURONS][OUTPUT_NEURONS]; /* hid->out weight */
//activation
double inputs[MAX_SAMPLES][INPUT_NEURONS];
double hidden[HIDDEN_NEURONS];
double target[MAX_SAMPLES][OUTPUT_NEURONS];
double actual[OUTPUT_NEURONS];
//errors
double errO[OUTPUT_NEURONS];
double errH[HIDDEN_NEURONS];
double Error = 0.0;
int sample = 0;
typedef struct {
double sepal_lenght;
double sepal_width;
double petal_lenght;
double petal_width;
double output[OUTPUT_NEURONS];
} IRIS;
IRIS samples[MAX_SAMPLES] = {
{ 5.1, 3.5, 1.4, 0.2, 0.0, 0.0, 1.0 },
{ 4.9, 3.0, 1.4, 0.2, 0.0, 0.0, 1.0 },
{ 4.7, 3.2, 1.3, 0.2, 0.0, 0.0, 1.0 },
{...},
};
double sigmoid(double val) {
return (1.0 / (1.0 + exp(-val)));
}
double dsigmoid(double val) {
return (val * (1.0 - val));
}
void assignRandomWeights() {
int hid, inp, out;
printf("Initializing weights...\n\n");
for (inp = 0; inp < INPUT_NEURONS; inp++) {
for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
IHweight[inp][hid] = RAND_WEIGHT;
printf("Weights : input %d -> hidden %d: %f\n",
inp, hid, IHweight[inp][hid]);
}
}
for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
for (out = 0; out < OUTPUT_NEURONS; out++) {
HOweight[hid][out] = RAND_WEIGHT;
printf("hidden %d -> output %d: %f\n",
hid, out, HOweight[hid][out]);
}
}
system("pause");
}
void activation() {
int hid, inp, out;
double sumH[HIDDEN_NEURONS] ;
double sumO[OUTPUT_NEURONS];
for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
for (inp = 0; inp < INPUT_NEURONS; inp++) {
sumH[hid] += (inputs[sample][inp] * IHweight[inp][hid]);
printf("\n%d Input %d = %.1f Weight = %f sumH = %g",
sample, inp, inputs[sample][inp], IHweight[inp][hid], sumH[hid]);
}
hidden[hid] = sigmoid(sumH[hid]);
printf("\nHidden neuron %d activation = %f", hid, hidden[hid]);
}
for (out = 0; out < OUTPUT_NEURONS; out++) {
for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
sumO[out] += (hidden[hid] * HOweight[hid][out]);
printf("\n%d Hidden %d = %f Weight = %f sumO = %g",
sample, hid, hidden[hid], HOweight[hid][out], sumO[out]);
}
actual[out] = sigmoid(sumO[out]);
printf("\nOutput neuron %d activation = %f", out, actual[out]);
}
}
main () {
srand(time(NULL));
assignRandomWeights();
for (int epoch = 0; epoch < 1; epoch++) {
for (int i = 0; i < 1; i++) {
sample = rand() % MAX_SAMPLES;
inputs[sample][0] = samples[sample].sepal_lenght;
inputs[sample][1] = samples[sample].sepal_width;
inputs[sample][2] = samples[sample].petal_lenght;
inputs[sample][3] = samples[sample].petal_width;
target[sample][0] = samples[sample].output[0];
target[sample][1] = samples[sample].output[1];
target[sample][2] = samples[sample].output[2];
activation();
}
}
}
I'm using a lot of printf() to check my results and i get
...
41 Input 0 = 4.5 Weight = 0.321014 sumH = 1.31886e+267
41 Input 1 = 2.3 Weight = 0.772369 sumH = 1.31886e+267
41 Input 2 = 1.3 Weight = 0.526123 sumH = 1.31886e+267
41 Input 3 = 0.3 Weight = 0.271881 sumH = 1.31886e+267
Hidden neuron 6 activation = 1.000000
...
41 Hidden 0 = 0.974952 Weight = 0.343445 sumO = 1.24176e+267
41 Hidden 1 = 0.917789 Weight = 0.288361 sumO = 1.24176e+267
41 Hidden 2 = 0.999188 Weight = 0.972168 sumO = 1.24176e+267
41 Hidden 3 = 0.989726 Weight = 0.082642 sumO = 1.24176e+267
41 Hidden 4 = 0.979063 Weight = 0.531799 sumO = 1.24176e+267
41 Hidden 5 = 0.972474 Weight = 0.552521 sumO = 1.24176e+267
41 Hidden 6 = 1.000000 Weight = 0.707153 sumO = 1.24176e+267
Output neuron 1 activation = 1.000000
The assignRandomweights() and sigmoid() functions are ok as far as i can tell, the problem is in activation().
Please help me understand why this happens and how to solve it.
Your problem is in these lines
double sumH[HIDDEN_NEURONS];
double sumO[OUTPUT_NEURONS];
You don't initialise them before use. Technically the program behaviour is undefined. Your friendly compiler appears to be setting uninitialised variables to large values. (Other platforms such as Itanium will trap a "Not a Thing").
A simple remedy is to use double sumH[HIDDEN_NEURONS] = {0}; etc. which will set every element to zero.
check this line:
double sumH[HIDDEN_NEURONS] ;
and this line:
sumH[hid] += (inputs[sample][inp] * IHweight[inp][hid]);
you have declared the sumH[] without setting to zero every member of it, so, it starts from an arbitrary value as there is no definition of sumH[hid]
you can use:
for(unsigned int i=0; i<HIDDEN_NEURONS; i++) sumH[i] = 0;
before any use of it (if you don't like malloc() or ZeroMemory()), for example...