Needidng help with Resistor Color exercise in C

I’ve been trying to solve Resistor Color Exercise, but i done a little search and seen thar all i had to do was define the colors at the enum:

typedef enum {
    BLACK,
    BROWN,
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    BLUE,
    VIOLET,
    GREY,
    WHITE
} resistor_band_t;

And call it inside the function on resistor_color.c:

#include "resistor_color.h"

int color_code(resistor_band_t color) {
    resistor_band_t resistor = color;
    return resistor;
}

I asked to Gemini what to do, but all it said is that i have to include this to resolve the testing:

const resistor_band_t *colors(void) {
    
    static const resistor_band_t all_colors[] = {
        BLACK, BROWN, RED, ORANGE, YELLOW,
        GREEN, BLUE, VIOLET, GREY, WHITE
    };

    return all_colors;
}

And when i asked why, it said that “my main code in the tests so all the code you should do is to complete it”…

Obviously, i wanted to understand what this piece of code do, rather than just accepting it

Edit: I tested this piece of code it recomended me, but it doesn’t work, i guess i was right to doubt

In the practice exercises the tests are the specification, you are supposed to read them. In this exercise the first test looks like this:

static void test_black(void)
{
   TEST_ASSERT_EQUAL_UINT16(0, color_code(BLACK));
}

It calls a function color_code with one argument (BLACK) and expects the result to be a uint16_t with the value 0.
You have to satisfy the requirements of the test and get it to pass.

You already filled in the enumerators of the enum. Great!
Did you declare the function color_code in the .h file, so that the tests can see it?


There are several more tests for color_code, and one last test at the end for a second function colors.

static void test_colors(void)
{
   TEST_IGNORE();
   const resistor_band_t expected[] = { BLACK, BROWN, RED,    ORANGE, YELLOW,
                                        GREEN, BLUE,  VIOLET, GREY,   WHITE };
   const resistor_band_t *actual = colors();
   TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, ARRAY_LENGTH(expected));
}

Gemini’s implementation of the function colors looks great.
Did you declare it in the .h file so that the tests can see it?


My general advice:

  1. Read the instructions. They give you a high-level description of the task.
  2. Read the tests. They define in detail what you need to pass the tests.
  3. Read the error messages from the compiler/linker. They will tell you if something else is not valid C, if some entity is unknown (i.e. missing), or if there’s a conflict.
  4. Read the error messages from failed tests. Try to understand the tests, why its expected result is correct, what your function did return, and how to fix it.

Thank you for your reply,

So, I read the error messages, i got confused when it sayed:

error: implicit declaration of function 'color_code' [-Wimplicit-function-declaration]

But now i get it, i should be declaring my functions on .h file.

Thank you again, for your support.

1 Like