C++ 1) Create a source and a header file for the functions and name them screen.
ID: 3588848 • Letter: C
Question
C++
1) Create a source and a header file for the functions and name them screen.cpp and screen.h, respectively. You can find a file main.cpp with this post. Make sure to use the names exactly as typed, otherwise the TAs may return the files to you.
a) 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.
b) 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 ' ‘.
c) 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.
The main.cpp file:
#include <iostream>
#include "screen.h"
using std::cout;
using std::cin;
using std::endl;
int main() {
char screen[g_height][g_width]{};
clearScreen( screen );
int hLines, vLines;
cout << "No. of Horizontal and Vertical Lines: "; cin >> hLines; cout << endl;
gridScreen( screen, hLines );
printScreen( screen );
// No clear screen
cout << "Drawing new grid over old grid:" << endl;
cout << "No. of Horizontal Lines: "; cin >> hLines;
cout << "No. of Vertical Lines: "; cin >> vLines; cout << endl;
gridScreen( screen, hLines, vLines );
printScreen( screen );
clearScreen( screen );
return 0;
}
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.