Home > Software engineering >  Why the three numbers for this code is "-666 3289 1617" and able to print the last line?
Why the three numbers for this code is "-666 3289 1617" and able to print the last line?

Time:06-05

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
static int64_t arr[] = { 20047, 75106, 19594, 88069, 22445, 82238 };
const static int N = sizeof(arr)/sizeof(*arr);

static void fail(){
        puts("Nope!");
        exit(EXIT_FAILURE);
     }

    static void func_one(int64_t r, int64_t i, int64_t g){
        if ( r - g / 7   3 * i / 11 )
              fail();
    }
static void func_two(int z, int64_t b){
        int o = b;
        for(; z<N;   z) {
           if ((z % 2) == 0)
              continue;
           o  = arr[z];
        }
        if (o != 188394)
           fail();
    }
static void func_three(int d, int64_t e){
        if (d<N) {
          if (d % 2)
            func_three(  d, e);
          else func_three(d   1, e * arr[d]);
      } else if (e != 190100988)
         fail();
    }

int main(){
    int64_t p, q, v;
    printf("Please enter the right three numbers: ");
    fflush(stdout);
    if (scanf("%" SCNd64 " %" SCNd64 " %" SCNd64,&p,&q,&v) != 3)
    fail();
    arr[0] = p;
    arr[5] = q;
    arr[4] = v;
    func_two(0, 21930);
    func_three(1, 6);
    func_one(p, q, v);
    puts("Exactly! Good job.");
    }

I pass three input values:"-666 3289 1617".What does these input doing to the code and able to print the last line "Exactly! Good job."? Is there any way other inputs values can be found? What exactly happening with here.

CodePudding user response:

Each of the func… functions calls fail if the function’s test is not passed, and the fail routine terminates the program with the “Nope!” message. So the goal of getting the “Exactly! Good job.” message requires passing the tests.

The program tries to read three numbers from input, called p, q, and v. It should be noted that if there are errors of various sorts, such as arithmetic overflows or use of variables that are uninitialized due to the scanf not receiving suitable input, then the behavior of the program is not defined by the C standard, so it might be possible the program prints “Exactly! Good job.” in such circumstances. However, the rest of this answer will exclude undefined behavior while analyzing the program.

After reading the numbers, p, q, and v, the program puts them into certain positions in arr, leaving arr with the values p, 75106, 19594, 88069, v, and q.

Then the program calls func_two(0, 21930). func_two uses that first argument, 0, as a loop counter, and it uses the second argument, 21930, to start an accumulating sum. In the loop, it does nothing for even values of the loop counter, z. For odd values of z, it adds arr[z] to the sum. Thus, to 21930, it adds arr[1], arr[3], and arr[5], forming the sum 21930 75106 88069 q. To pass, this sum must equal 188394. So we have 21930 75106 88069 q = 188394, to which the solution is q = 3289.

Then the program calls func_three(1, 6). func_three also uses its first argument as a loop counter, d, and it uses e as an accumulating product, e, by way of recursion. When d is odd, func_three merely calls itself with an incremented d and no change to e. When d is even, func_three calls itself with an incremented d and e multiplied by arr[d].

Since d starts at 1, the first time it is even is when it is 2. Thus, func_three starts with e set to 6 and multiplies it by arr[2] and then arr[4], producing 6 • 19594 • v. Then the recursion ends because d reaches the end of the array size, N, and the function applies its test: This product must equal 190100988. So we have 6 • 19594 • v = 190100988, to which the solution is v = 1617.

Then the function calls func_one(p, q, v), which we now know must be func_one(p, 3289, 1617). It calls these arguments r, i, and g and evaluates r - g / 7 3 * i / 11, which must be zero to pass the test. Substituting gives us p - 1617 / 7 3 * 3289 / 11, which reduces to p - 231 897 and further to p 666. Obviously for this to be zero, p must be −666.

Thus, the only input that produces the output “Exactly! Good job.” without undefined behavior is -666 3289 1617 or equivalents such as the same numbers with leading zeros.

  •  Tags:  
  • c
  • Related