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