Write a program that takes in 6 arguments (from the command line using argc and
ID: 639003 • 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> ":
$./lab2 10 10 2 2 4 4
XXXX
XXXX
XXXX
XXXX
I need a program that outputs X's in the above form. CODE MUST BE IN C++.
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<<" ";
for(int i=0; i<((window_y - off_y) < height ? (window_y - off_y) : height); i++)
{
for(int j=0; j<off_x; j++)
cout<<" ";
for(int j=0; j<((window_x - off_x) < width ? (window_x - off_x) : width); j++)
{
cout<<"X";
}
cout<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.