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

A C++ program code is given designed to draw several pictures of rectangles. The

ID: 3683944 • Letter: A

Question

A C++ program code is given designed to draw several pictures of rectangles. The main function will read in a series of data sets (from a file via Linux redirection) until the end of file is reached. Each data set will consist of 2 integers and a character. The integers represent, respectively, the vertical length and horizontal width of a rectangle. The character is to be used when drawing the rectangle. For each data set, the draw_rectangle function is called to generate the picture or an appropriate error message.
The fstream header file will not be used, as data will be gathered from a file using cin.
The draw_rectangle function is incomplete. The heading has been supplied, but the statements for the body of the function are needed. The function should do the following:

If the length and/or width are less than 2, the function should display an error message that includes the requested length and width and states that the rectangle will not be drawn.

If the length and width are both greater than or equal to 2, then the function should display a label that includes the requested length and width and then draw the rectangle as a hollow shape using the specified character to form the outline (see examples below).

Sample terminal session:

[key]$ more data4eight
4 6 #
2 -5 $
10 3 &
[key]$ g++ ex8.cpp
[key]$ ./a.out < data4eight

4 by 6 Rectangle
######
#    #
#    #
######

Length of 2 and/or width of -5 is/are invalid.
The rectangle will not be drawn.

10 by 3 Rectangle
&&&
& &
& &
& &
& &
& &
& &
& &
& &
&&&

Here is the incomplete code:

//The purpose of this program is to draw a series of rectangles.   
//Expected input: a file (via Linux redirection) that contains several sets
//of data. Each data set consists of 2 integers and a character. The first   
//integer is the vertical length and the second integer is the horizontal
//width of a rectangle. The character is used for the drawing.   
//Expected output: for each data set, a picture of a rectangle. Each picture   
//will be labeled and there will be at least one blank line between each drawing.
//For example, if a data set is: 4 7 $ the output should be
// 4 by 7 Rectangle   
// $$$$$$$
// $ $
// $ $
// $$$$$$$

#include <iostream>
using namespace std;

void draw_rectangle(int,int,char);

int main()
{
int vlength,hwidth; //the vertical length and horizontal width of a rectangle
char symbol; //character used for the border of the rectangle   
cin >> vlength;
while (cin)
{
cin >> hwidth >> symbol;
draw_rectangle(vlength,hwidth,symbol);
cout << endl;
cin >> vlength;
}
}
void draw_rectangle(int length, int width, char draw)
//Given the vertical length and horizontal width of a rectangle along with   
//a character to be used for drawing it, if both length and width are >= 2,
//display the outline of the rectangle with a label that includes the length   

//and width. If the length and/or the width are less than 2, display a message   
//stating that the rectangle will not be drawn. Include the requested dimensions   
//in the message.
{

}

Explanation / Answer

#include using namespace std; void draw_rectangle(int, int, char); int main() { int vlength, hwidth; char symbol; cin >> vlength; while(cin) { cin>>hwidth>>symbol; draw_rectangle(vlength,hwidth,symbol); coutvlength; } } void draw_rectangle(int length, int height, char draw) { if((length
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote