Home > database >  Task 1 was to fix the code, I did it. Task 2 is to find the input values for which it prints "E
Task 1 was to fix the code, I did it. Task 2 is to find the input values for which it prints "E

Time:02-27

Task 1 was to fix the code, I did it. Task 2 is to find the input values for which it prints "Exactly! Good job.", I need some help figuring out for which inputs, I can get it to say "Exactly! Good job"

#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.");
}

CodePudding user response:

Currently, if your code can read the inputs, it declares a success. If the code cannot read the inputs, it bails out through the exit (in your fail function).

I believe your code is missing an else portion after read failure. The code is currently making an unconditional assignment into the 3 char array.

Based on your statements, I believe you need to check if the values stored in ac_dc are r,l,h.

To fix it, add an else after the scanf failure check. Inside the else, check if the inputs read into ac_dc are equal to r,l,h.

BTW - thanks for the metallica and ac_dc reference. If you would have included zepplin or sabbath, I would have re-written it for you.

CodePudding user response:

the_beatles() adds up four numbers and fails unless you have one specific value of l. Which value is that?

Go through the code step by step, and find which values avoid a call to fail(). Not hard.

  • Related