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

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    2

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,314
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    2
    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;
        addOperators(num, target,returnSize);
    }
    I made this but i cant print it whats wrong

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,314
    The very first line of the body of the main function is wrong. That's pretty indicative that you wrote this at one go and hoped that it works, and when it didn't, you're doomed because now you're overwhelmed by the many problems.

    There's a reason why I said I would likely attempt this in three stages: it may sound slower, but in each stage I'll be solving a manageable problem.

    So, start over. Write some code, compile and (if feasible) test. Write more code, compile and test.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with math equation.
    By syahidfz in forum C Programming
    Replies: 6
    Last Post: 02-04-2015, 05:25 AM
  2. Math equation help!!
    By alex33zebras in forum C Programming
    Replies: 2
    Last Post: 09-27-2011, 03:31 PM
  3. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  4. Insert Math Symbols
    By MarkTheSpark in forum C Programming
    Replies: 7
    Last Post: 06-09-2005, 10:33 AM
  5. reading math symbols
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-08-2003, 10:13 AM

Tags for this Thread