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

Write a program that takes in 6 arguments (from the command line using argc and

ID: 639023 • Letter: W

Question

Write a program that takes in 6 arguments (from the command line using argc and argv) and draws the outline of a rectangle with Xs. The first two arguments are the number of columns and number of rows of the drawing plane. The next two are the column and row offset to begin drawing the rectangle. The last two arguments are the width and height of the rectangle. You may assume that all numbers will be positive. If you specify dimensions and offsets that are outside the bounds of your drawing plane, then your rectangle will be cut off and you should only output the portions that aren't cutoff. You should also make sure that the user specifies the correct number of command line arguments. Here are some example outputs (assuming that the arguments are in the order of "<# of columns> <# of rows> <column offset> <row offset> <width of rectangle> <height of rectange>":

$./lab2 10 10 2 2 4 4
        
        
XXXX  
X    X  
X    X  
XXXX

Explanation / Answer

#include <iostream>
using namespace std;

int atoi(char * str) {
int n = 0;
int i=0;
  
while(str[i] != '')
{
n = n*10 + (str[i++] - '0');
}
  
return n;
}

int main(int argc, char** argv) {

if(argc != 7)
{
cout<<"wrong number of inputs !!! ";
return 0;
}

int window_x = atoi(argv[1]);
int window_y = atoi(argv[2]);
int off_x = atoi(argv[3]);
int off_y = atoi(argv[4]);
int width=atoi(argv[5]);
int height = atoi(argv[6]);
  
  
for(int i=0; i<off_y; i++)
cout<<" ";

int maxy = ((window_y - off_y) < height ? (window_y - off_y) : height);
int maxx= ((window_x - off_x) < width ? (window_x - off_x) : width);

for(int i=0; i<maxy; i++)
{
for(int j=0; j<off_x; j++)
cout<<" ";
for(int j=0; j<maxx; j++)
{
if(i == 0 || i == maxy-1)
{
cout<<"X";
}
else
{
if(j==0 || j==maxx-1)
{
cout<<"X";
}
else
{
cout<<" ";
}
}
}
cout<<" ";
}
}

----------------------

OUTPUT

sh-4.2# ./main 10 10 2 2 4 4                                               

                                                                           

                                                                           

  XXXX                                                                     

  X X                                                                     

  X X                                                                     

  XXXX

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