Create a source and a header file for the functions and name them screen.cpp and
ID: 3587211 • Letter: C
Question
Create a source and a header file for the functions and name them screen.cpp and screen.h, respectively.
Define a function printScreen that accept the two-dimensional array for the screen by reference. Notice that you need to define global variables g_width and g_height in your header file for screen. I have used g_width = 40 and g_height = 20 in my example but different values could be used. Notice further that as we pass the fixed-sized array by reference we do not need to pass dimensions. The function printScreen is to print the array line-by-line to the console.
Define a function clearScreen that accept the two-dimensional array for the screen by reference and sets all the characters in the 2D array to the empty space ' '.
Define a function gridScreen that accept the two-dimensional array for the screen by reference and two parameters for the horizontal and vertical lines in the grid, respectively. The number of vertical lines is optional and if the number is not specified, your function should use the same number of vertical than horizontal lines. Your function is to degrade gracefully if the number of lines exceeds the number of rows or columns available. Also inputs of 0 lines have to work and negative numbers should be treated as 0.
Note: this is a C++ program
Explanation / Answer
Here is the code for Screen.h:
// you need to define global variables g_width and g_height in your header file for screen.
int g_width = 40;
int g_height = 20;
//A function printScreen that accept the two-dimensional array for the screen by reference.
void printScreen(char** screen);
//A function clearScreen that accept the two-dimensional array for the screen by reference.
void clearScreen(char** screen);
//Define a function gridScreen that accept the two-dimensional array for the screen by
//reference and two parameters for the horizontal and vertical lines in the grid, respectively.
//The number of vertical lines is optional and if the number is not specified,
//your function should use the same number of vertical than horizontal lines.
void gridScreen(char** screen, int numOfHorizontals, int numOfVerticals = g_width + 1);
And the code for Screen.cpp is:
#include <iostream>
#include "Screen.h"
using namespace std;
//The function printScreen is to print the array line-by-line to the console.
void printScreen(char** screen)
{
for(int i = 0; i < g_height; i++)
{
cout << *(screen + i) << endl;
}
}
//The function clearScreen sets all the characters in the 2D array to the empty space ' '.
void clearScreen(char** screen)
{
for(int i = 0; i < g_height; i++)
for(int j = 0; j < g_width; j++)
screen[i][j] = ' ';
}
void gridScreen(char** screen, int numOfHorizontals, int numOfVerticals)
{
if(numOfVerticals == g_width + 1)
numOfVerticals = numOfHorizontals;
// Your function is to degrade gracefully if the number of lines exceeds the number of rows or columns available.
if(numOfVerticals > g_width || numOfHorizontals > g_height)
cout << "The screen size is set to beyond its maximum capacity." << endl;
//Also inputs of 0 lines have to work and negative numbers should be treated as 0.
if(numOfVerticals < 0)
numOfVerticals = 0;
if(numOfHorizontals < 0)
numOfHorizontals = 0;
for(int i = 0; i < numOfHorizontals; i++)
for(int j = 0; j < numOfVerticals; j++)
screen[i][j] = '*';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.