-4

I've been given a task to write program in C using recursion. The program is given an equation as a string, such as "123+123=246".

My task is to determine, whether the equation is right. The catch is, that numbers in the equation are sometimes replaced with a '?', for example "1??+??3=??6". Using recursion, I need to sum all possibilities, when the '?' is replaced with a digit and the equation is right.

Obviously, the solution will be based on trying out all possibilities and only selecting those, that make up the right equation. But I have no idea, how to implement it.

Could anyone give me a hint or reply with a piece of code I could base my solution on ?

Thanks very much !

George
  • 3
  • 1
  • and those possibilities where do you want to store them in an array of char or what ? – Holy semicolon Dec 11 '18 at 12:25
  • In `"1??+??3=??6"` what happens if `'?'` is `0`? A pure replacement would leave `003` (not valid). How are you supposed to substitute for `'?'` -- that part is unclear. – David C. Rankin Dec 11 '18 at 12:33
  • 1
    Yes, in arr of chars. And no, 003 is a valid number, 003 = 3. I'm sorry for not specifying it earlier. – George Dec 11 '18 at 12:39

1 Answers1

1

Here's a general idea:

Basically we want to first of all be able to determine if the equation is right or not Implement this function

int eval(char * string ) 

This will return 1 for true 0 for false and -1 when there are still '?'

Now we want write our recursion it will return a string and take a string

char * recursion (char * string)

First we need to see if the string holds a full equation.

Int res = eval(string);
if(res == 1)return string;
else if(res == 0)return "";

If it didn't stop yet that means that it can not determine because of the '?' , we need to find a way to kill them. etch '?' can be 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 Let's do a for loop But first implement the method to replace the first '?' with a number,

char * Replace(char* string,int num);

After you've implemented this we create our for loop

for (int i = 0; i< 10 ; i++){
 char * result =recursion(Replace(string , I));
 if(Eval(result)==1) ;//we found a right answer add it to our return
}
return ""+ [all right answers we found if we even found ];

Good luck learning !

tomer zeitune
  • 1,080
  • 1
  • 12
  • 14