'What is the correct input to have the program print the sentence: Exactly! Good Job

Read and understand the following code.

Provide the correct input to have the program print the sentence: Exactly! Good Job.

Before submitting your answer, please check its correctness by executing the code.

#include <stdio.h>

#include <inttypes.h>

#include <stdlib.h>

static int64_t green_day[] = {68053, 56286, 93562, 32510, 80652, 62435};

const static int N = sizeof(green_day)/sizeof(*green_day);

static void fail(){

    puts("Nope!");

    exit(EXIT_FAILURE);

}

static void pink_floyd(int64_t a, int64_t f, int64_t r){

    if ( a - r / 7 + 3 * f / 11 )

    fail();

}

static void ac_dc(int k, int64_t d){

    int64_t i = d;

    for(; k<N; ++k) {

        if ((k % 2) == 0)

        continue;

        i += green_day[k];

    }

    if (i != 152916)

    fail();

}

static void the_rolling_stones(int t, int64_t n){

    if (t<N) {

    if (t % 2)

    the_rolling_stones(++t, n);

    else the_rolling_stones(t + 1, n * green_day[t]);

    } else if (n != 769547450)

    fail();

}

int main(){

    int64_t h, x, v;

    printf("Please enter the right three numbers: ");

    fflush(stdout);

    if (scanf("%" SCNd64 " %" SCNd64 " %" SCNd64, &h, &x, &v) != 3)

    fail();

    green_day[0] = h;
    green_day[5] = x;
    green_day[4] = v;

    ac_dc(0, 61106);

    the_rolling_stones(1, 5);

    pink_floyd(h, x, v);

    puts("Exactly! Good job.");

}


Solution 1:[1]

First the answer:

Please enter the right three numbers: -587 3014 1645
Exactly! Good job.

If you can trace the call, each function gives out value of an unknown.

  • ac_dc() gives you x = 3014
  • the_rolling_stones() gives you v = 1645
  • pink_floyd() gives you the last h = -587

Tracing calls is beyond the scope of this site. If the person who asked you this question doesn't follow it up with Show me how you got it? then the whole exercise was a moot point.

Solution 2:[2]

-Please enter the right three numbers: -311 2112 1855 Exactly! Good job.

    #include <stdio.h>
    #include <inttypes.h>
    #include <stdlib.h>
    static int64_t aerosmith[] = { 37262, 52682, 47166, 68777, 97309, 53919 };
    const static int N = sizeof(aerosmith) /sizeof(*aerosmith);
    static void fail(){
            puts("Nope!");
            exit(EXIT_FAILURE);
    }
    static void guns_n_roses(int64_t e, int64_t p, int64_t n) {
            if ( e - n / 7 + 3 * p / 11)
                    fail();
    }
    static void metallica(int j, int64_t k) {
            int64_t g = k;
            for(; j<N; ++j) {
                    if ((j % 2) == 0)
                            continue;
                    g += aerosmith[j];
                }
                if (g != 181228)
                      fail();
    }
    static void the_beatles(int u, int64_t c ) {
            if (u<N) {
                    if (u % 2)
                            the_beatles(++u, c);
                        else the_beatles(u + 1, c * aerosmith[u]);
                    } else if (c != 262478790)
                            fail();
    }
    int main(){
           int64_t a, y, w;
           printf("Please enter the right three numbers: ");
           fflush(stdout);
           if (scanf("%" SCNd64 "%" SCNd64 "%" SCNd64, &a, &y, &w) != 3)
                fail();
           aerosmith[0] = a;
           aerosmith[5] = y;
           aerosmith[4] = w;
           metallica(0, 57657);
           the_beatles(1, 3);
           guns_n_roses(a, y, w);
           puts("Exactly! Good job. ");
    }

-I recommend that if you are faced with such a type of problem, try to trace each function separately,in the above example you can start tracing from a metallica function, display the values in each iteration.

Solution 3:[3]

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
static int64_t ac_dc[] = { 14479, 12574, 54786, 51996, 77903, 16499 };
const static int N = sizeof(ac_dc)/sizeof(*ac_dc);
static void fail(){
puts("Nope!");
exit(EXIT_FAILURE);
}
static void linkin_park(int64_t i, int64_t z, int64_t o){
if ( i - o / 7 + 3 * z / 11 )
fail();
}
static void the_beatles(int n, int64_t u){
int64_t s = u;
for(; n<N; ++n) {
if ((n % 2) == 0)
continue;
s += ac_dc[n];
}
if (s != 159276)
fail();
}
static void metallica(int y, int64_t d){
if (y<N) {
if (y % 2)
metallica(++y, d);
else metallica(y + 1, d * ac_dc[y]);
} else if (d != 197887032)
fail();
}
int main(){
int64_t r, l, h;
printf("Please enter the right three numbers: ");
fflush(stdout);
if (scanf("%" SCNd64 " %" SCNd64 " %" SCNd64, &r, &l, &h) != 3)
fail();
ac_dc[0] = r;
ac_dc[5] = l;
ac_dc[4] = h;
the_beatles(0, 92627);
metallica(1, 3);
linkin_park(r, l, h);
puts("Exactly! Good job.");
}

These functions give value of S & D, but what is the third value it will give while tracing the call of each function

Solution 4:[4]

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
static int64_t ac_dc[] = { 14479, 12574, 54786, 51996, 77903, 16499 };
const static int N = sizeof(ac_dc)/sizeof(*ac_dc);
static void fail(){
 puts("Nope!");
 exit(EXIT_FAILURE);
}
static void linkin_park(int64_t i, int64_t z, int64_t o){
 if ( i - o / 7 + 3 * z / 11 )
 fail();
}
static void the_beatles(int n, int64_t u){
 int64_t s = u;
 for(; n<N; ++n) {
 if ((n % 2) == 0)
 continue;
 s += ac_dc[n];
 }
 if (s != 159276)
 fail();
}
static void metallica(int y, int64_t d){
 if (y<N) {
 if (y % 2)
 metallica(++y, d);
 else metallica(y + 1, d * ac_dc[y]);
 } else if (d != 197887032)
 fail();
}
int main(){
 int64_t r, l, h;
 printf("Please enter the right three numbers: ");
 fflush(stdout);
 if (scanf("%" SCNd64 " %" SCNd64 " %" SCNd64, &r, &l, &h) != 3)
 fail();
 ac_dc[0] = r;
 ac_dc[5] = l;
 ac_dc[4] = h;
 the_beatles(0, 92627);
 metallica(1, 3);
 linkin_park(r, l, h);
 puts("Exactly! Good job.");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 जलजनक
Solution 2
Solution 3 waqasak98
Solution 4 Robel Debebe