# Thread: C project Find the math symbols so that the equation is valid

1. ## C project Find the math symbols so that the equation is valid

we have the 8 ? 7 ? 6 ? 5 ? 4 ? 3 ? 2 ? 1 = 36 and it asks to replace the ? with a math symol(*,+,-,/) so the equation is valid! We have to create multiple solutions for the equation.As an example we have that
8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 36
or with numbers that we connect them like that
87 - 65 - 4 - 3 + 21 = 36
How do we even start?
I guess it can be with strings or arrays i dont know sth like that
Thank you very much

2. I might do this in three stages:

Stage 1: write a program that stores the numbers on the left hand side of the equation as an array of ints, then figure out how you can implement the various permutations of operations to arrive at a result that can match with the number on the right hand side of the equation.

Stage 2: write a program that represents the numbers on the left hand side of the equation as an array of numeric strings. Figure out how you can implement an exhaustive cycling through the various combinations of adjacent numeric strings, resulting in a new array of numeric strings each time.

Stage 3: combine stages 1 and 2, with the arrays of numeric strings in stage 2 converted to arrays of ints.

3. insert
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ao( char* num, int cur, double left, double sum, char* sol, char*** res, int* resEnd, int* resCap, int target ){
if( cur == strlen(num) && left * sum == target ){
if( *resEnd == *resCap ){
*resCap *= 2;
*res = realloc( *res, (*resCap) * sizeof( **res ) );
}
( *res )[ *resEnd ] = sol;
( *resEnd )++;
return;
}

if( cur == strlen(num) ){
return;
}

int oldLen = strlen( sol );
if( sum != 0 ){
char* newSol = calloc( oldLen, sizeof( *newSol ) );
strcpy( newSol, sol );
newSol[ oldLen ] = num[cur];
ao( num, cur+1, left, sum * 10 + ( num[ cur ] - '0' ), newSol, res, resEnd, resCap, target );
}
{
char* newSol = calloc( oldLen, sizeof( *newSol ) );
strcpy( newSol, sol );
newSol[ oldLen ] = '*';
newSol[ oldLen + 1 ] = num[cur];
ao( num, cur+1, left * sum, num[ cur ] - '0', newSol, res, resEnd, resCap, target );
}
{
char* newSol = calloc( oldLen, sizeof( *newSol ) );
strcpy( newSol, sol );
newSol[ oldLen ] = '+';
newSol[ oldLen+1 ] = num[cur];
ao( num, cur+1, 1, num[cur] - '0', newSol, res, resEnd, resCap, target - left * sum );
}
{
char* newSol = calloc( oldLen, sizeof( *newSol ) );
strcpy( newSol, sol );
newSol[ oldLen ] = '-';
newSol[ oldLen+1 ] = num[cur];
ao( num, cur+1, -1, num[cur] - '0', newSol, res, resEnd, resCap, target - left * sum );
}

free( sol );
}

char** addOperators(char* num, int target, int* returnSize) {
int resCap = 16;
char** res = calloc( resCap, sizeof( *res ) );
int resEnd = 0; // 0 .. resEnd-1 is the current result we have

int n = strlen( num );

if( n== 0 ){
return res;
}

char* sol = calloc( 2 * n - 1, sizeof( *sol ) );
memset( sol, '\0', 2 * n - 1);
sol[0] = num[0];

ao( num, 1, 1, num[0] - '0', sol, &res, &resEnd, &resCap, target );

*returnSize = resEnd;
printf("%c",res);
}
int main(){
char* num=123;
int target=1;
int* returnSize=1;