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

#include <stdlib.h> #include <cab202_graphics.h> #include <cab202_timers.h> // (

ID: 3731429 • Letter: #

Question

#include <stdlib.h>
#include <cab202_graphics.h>
#include <cab202_timers.h>

// (a) Declare four global integer variables, as follows:
// l - the horizontal location of the left edge of the rectangle
// t - the vertical location of the top edge of the rectangle
// cols - the width of the rectangle.
// rows - the height of the rectangle.

// (b) Declare a global variable of type char called symbol.
// 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.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '@';
draw_rect();
show_screen();

// draw a box.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '&';
draw_rect();
show_screen();

// draw a box with zero width.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 0;
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '*';
draw_rect();
show_screen();

// draw a box.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '#';
draw_rect();
show_screen();

// draw a box with negative width.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = -rand() % screen_width();
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '!';
draw_rect();
show_screen();

// draw a box.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '+';
draw_rect();
show_screen();

// draw a box with zero height.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 0;
symbol = 'a';
draw_rect();
show_screen();

// draw a box.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = 'b';
draw_rect();
show_screen();

// draw a box with negative width.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = -rand() % screen_width();
rows = 1 + rand() % (screen_height() - t - 1);
symbol = 'c';
draw_rect();
show_screen();

// draw a box.
l = rand() % screen_width() / 2;
t = rand() % screen_height() / 2;
cols = 1 + rand() % (screen_width() - l - 1);
rows = 1 + rand() % (screen_height() - t - 1);
symbol = '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: #ca6202 and #cab292DrawRect1. is 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 Notes The supplied test driver produces s output similar to that shown below. . Use this test driver to implement and test your function prior to submission. rows = 1 + rand() % (screen-height() - t- 1); symbol '; draw rect); show_screen) // draw a box with negative width I = rand() % screen-width() / 2; t rand() % screen-height() / 2; cols -rand() % screen-width(); rows = 1 + rand() % (screen-height() symbol = ' ! ' ; draw_rect); show_screen); - t - 1); // draw a box 1 rand() % screen-width() / 2; t rand() % screen-height() / 2; cols = 1 + rand() % (screen-width() - 1-1); rows 1 + rand() % (screen-height()-t- 1); hings to avoid Don't rely on anything you do in your main function. AMS will compile your submission as part of a larger program and call setup) and process( as necessary to test functionality. To allow this to work, any main function in your submission will be automatically removed. Don't directly use ncurses or standard L O functions such as printf). AMS is looking for a ZDK screen.

Explanation / Answer

// Declaration of global variables
int l, t, cols, rows;
char symbol;

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.
  
// We will use the function void draw_char(int x, int y, char value) from the cab202_graphics.h header file to draw each character to a specific pair of coordinates

int i = l, j = t;
if (rows > 0 && cols > 0)
{
for(i = l; i < l+cols; i++)
{
for(j = t; j < t+rows; j++)
{
draw_char(i, j, symbol);
}
}
}
}// End of method