Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C-Code 1) Let color be the following structure: struct color { int red; int gree

ID: 3713490 • Letter: C

Question

C-Code

1)     Let color be the following structure:

struct color {

   int red;

   int green;

   int blue;

}

write function from a-i

a) struct color make_color (int red, int green, int blue);

Return a color structure containing the specified red, green, and blue values. If any argument is less than zero, the corresponding member of the structure will contain zero instead. If any argument is greater than 255, the corresponding member of the structure will contain 255.

b) int getRed(struct color c)

Returns the value of c’s red member

c) bool equal_color(struct color color1, struct color color2);

Returns true if the corresponding members of color1 and color2 are equal.

(Note: please typedef int as bool, and define true and false as Macros)

d) struct color brighter(struct color c);

Returns a color structure that represents a brighter version of the color c. The structure is identical to c, except that each member has been divided by 0.7 (with the result truncated to an integer). However, there are three special cases: (1) If all members of c are zero, the function returns a color whose members all have the value 3. (2) If any member of c is greater than 0 but less than 3, it is replaced by 3 before the division by 0.7. (3) If dividing by 0.7 causes a member to exceed 255, it is reduced to 255.

e) struct color darker(struct color c)

Returns a color structure that represents a darker version of the color c. The structure is identical to c, except that each member has been multiplied by 0.7 (with the result truncated to an integer).

f) void reset(struct color *p)

Reset the red, green and blue member to 0 of the color by using a pointer pointing to it.

g)     Write a declaration for a const variable named MAGENTA of type struct color and initialize its members as 255,0, and 255, respectively.

h)     Write statements to declare an array of colors using pointer and allocate memory space to it using malloc. The size of the array is 5.

i)       Continue the question above and write statements to initialize the colors in the array. Each member in the color should be initialized as 0.

Explanation / Answer

If you post more than 4 parts in a question, as per chegg guidelines I have to solve only first 4 parts.

(a)

struct color make_color (int red, int green, int blue)

{

    struct color ans;

   

    if( red < 0 )

        ans.red = 0;

    else if( red > 255 )

        ans.red = 255;

    else

        ans.red = red;

   

    if( green < 0 )

        ans.green = 0;

    else if( green > 255 )

        ans.green = 255;

    else

        ans.green = green;

   

    if( blue < 0 )

        ans.blue = 0;

    else if( blue > 255 )

        ans.blue = 255;

    else

        ans.blue = blue;

}

(B)

int getRed(struct color c)

{

    return c.red;

}

(c)

bool equal_color(struct color color1, struct color color2)

{

    return color1.red == color2.red && color1.green == color2.green && color1.blue == color2.blue;

}

(d)

struct color brighter(struct color c)

{

    struct color ans;

   

    // If all members of c are zero

    if( c.red == 0 && c.green == 0 && c.blue == 0 )

    {

        ans.red = 3;

        ans.green = 3;

        ans.blue = 3;

       

        return ans;

    }

   

    // If any member of c is greater than 0 but less than 3

    if( c.red > 0 && c.red < 3 )

        c.red = 3;

   

    if(c.green > 0 && c.green < 3 )

        c.green = 3;

   

    if(c.blue > 0 && c.blue < 3 )

        c.blue = 3;

       

    ans.red = c.red / 0.7;

    ans.green = c.green / 0.7;

    ans.blue = c.blue / 0.7;

   

    // If dividing by 0.7 causes a member to exceed 255, it is reduced to 255.

    if( ans.red > 255 )

        ans.red = 255;

   

    if( ans.green > 255 )

        ans.green = 255;

   

    if( c.blue > 255 )

        ans.blue = 255;

   

    return ans;

}

(e)

struct color darker(struct color c)

{

    struct color ans;

   

    ans.red = c.red * 0.7;

    ans.green = c.green * 0.7;

    ans.blue = c.blue * 0.7;

   

    return ans;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote