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

#include #include #include // (a) Declare four global integer variables, as foll

ID: 3868418 • Letter: #

Question

#include
#include
#include

// (a) Declare four global integer variables, as follows:
//      x - the horizontal location of the left edge of the rectangle
//      y - the vertical location of the top edge of the rectangle
//      w - the width of the rectangle.
//      h - the height of the rectangle.

// (b) Declare a global variable of type char called c.
//      This is the character that is to be used to render the rectangle.

void draw_rect(void) {
    // (c) Insert code to draw the outline of the rectangle defined by the global variables.
    //      If either of the width or height is less than or equal to zero,
    //      the function must not draw anything.
}

int main(void) {
    setup_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '@';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '&';
    draw_rect();
    show_screen();

    // draw a box with zero width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 0;
    h = 1 + rand() % (screen_height() - y - 1);
    c = '*';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '#';
    draw_rect();
    show_screen();

    // draw a box with negative width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = -rand() % screen_width();
    h = 1 + rand() % (screen_height() - y - 1);
    c = '!';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '+';
    draw_rect();
    show_screen();

    // draw a box with zero height.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 0;
    c = 'a';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'b';
    draw_rect();
    show_screen();

    // draw a box with negative width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = -rand() % screen_width();
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'c';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'd';
    draw_rect();
    show_screen();

    timer_pause(5000);
    cleanup_screen();
    return 0;
}

omplete the implementation of the draw_rect function. This function augments the ZDK by adding the ability to draw rectangular boxes. The hash-tags for this exercise are: cab202 and #cab202DrawRect1. This exercise will give you practice with the drawing functions in the ZDK and the use of global variables to transfer data between functions. Given the definition of a rectangle, you will complete a function that draws the outline of the rectangle using pre-existing functions defined in the ZDK. This function does not return a result. To complete the program, follow the instructions detailed in the in-line comments in the draw_rect () function

Explanation / Answer

#include
#include
#include

// (a) Declare four global integer variables, as follows:
//      x - the horizontal location of the left edge of the rectangle
//      y - the vertical location of the top edge of the rectangle
//      w - the width of the rectangle.
//      h - the height of the rectangle.

int x,y,w,h;

// (b) Declare a global variable of type char called c.
//      This is the character that is to be used to render the rectangle.

char c;

void draw_rect(void) {
    // (c) Insert code to draw the outline of the rectangle defined by the global variables.
    //      If either of the width or height is less than or equal to zero,
    //      the function must not draw anything.

if (w > 0 && h > 0)

{

for(int i =0;i<y;i++)

{ printf(" ");

}

for(int i =0 ;i < w;i++)

{

for(int k =0;k<y;k++)

{

printf(" ");

}

for(int j = 0; j< h; j++)

{

print("%c",c);

}

printf(" ");

}

}
}

int main(void) {
    setup_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '@';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '&';
    draw_rect();
    show_screen();

    // draw a box with zero width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 0;
    h = 1 + rand() % (screen_height() - y - 1);
    c = '*';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '#';
    draw_rect();
    show_screen();

    // draw a box with negative width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = -rand() % screen_width();
    h = 1 + rand() % (screen_height() - y - 1);
    c = '!';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = '+';
    draw_rect();
    show_screen();

    // draw a box with zero height.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 0;
    c = 'a';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'b';
    draw_rect();
    show_screen();

    // draw a box with negative width.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = -rand() % screen_width();
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'c';
    draw_rect();
    show_screen();

    // draw a box.
    x = rand() % screen_width() / 2;
    y = rand() % screen_height() / 2;
    w = 1 + rand() % (screen_width() - x - 1);
    h = 1 + rand() % (screen_height() - y - 1);
    c = 'd';
    draw_rect();
    show_screen();

    timer_pause(5000);
    cleanup_screen();
    return 0;
}