It is very interesting problem I got as a screenshot I captured from somewhere I do not remember. The screenshot tells that the problem is from CoderByte. I can not give you the link to the problem because I do not know where it is on the website. I am presenting here the problem with some necessary modifications (The things that might be excluded from the screenshot) along with the solution.
- The program first inputs a string from the user at line 10 and converts it into a char array at line 12.
- At line 14, a loop starts and an if statement at line 17 tests each element of the char array to determine if it is a number. If this is a number, then the another loop starts at line 19 inside which another if statement at line 22 checks the next number in the string. When the position of two numbers is determined in the string, an if statement at line 24 determines whether these two numbers add to 10 or not. If they add to 10, another loop at line 28 will start inside which an if statement will be used to count the number of question marks between the two numbers. If three question marks are not present, the value of the variable dec will turn 0 due to an if statement at line 36. The loop ends and the same procedure starts again.
- At last, the value of dec variable is examined at line 47. If it is the default i.e 1 assigned in the line 13, then the program displays true and if it is changed due to the if statement at line 36, the program will display false on the screen. Please note that the if statement at line 36 will change the value of the dec variable if exactly three question marks do not exist between each pair of numbers that add to 10.
- Please take a look at the example 4 in the problem. The 9 at the first place and the 1 at the 13th place add to 10 but there are 9 question marks between them. Still the output should be true. It means we have to compare a number in the string with only one number which is next to it in the string and not the other ones. Due to this the 9 at first place should only be compared with 1 at 5th place and not with the 1 at 13th place. For this purpose, I have placed a break statement at line 41 which will terminate the loop after a number is compared with another number next to it in the string.
- Now take a look at the example 5 in the problem. In this case no pair of numbers add to 10 and the result is false. It means that we will have to check the string in the program and if no pair of numbers add to 10, the program should return false. For this purpose, I have defined a variable de2 at line 13 with initial value of zero. Its value is altered only at line 26 in the program. Please see the structure of the program. The control will only approach the line 26 if it passes the condition of the if statement at line 24 and the condition is that the two numbers add to 10. It means that if any two pair of numbers in the string add to 10, the value of de2 will be 1 due to line 26 and if no pair of numbers add to 10 in the string, its value will remain 0. Before displaying the result, the de2 is multiplied with the dec variable due to which the final result will now also depend upon the de2 variable. In short, the program will not be able to return true if no pair of numbers in the string add to 10.