#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;
}
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.