Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C++ program
- #include <iostream>
- using namespace std;
- // definition of printGrid method to print the grid of "size" rows and "size" columns
- void printGrid(int size)
- {
- // if the argument size is a negative number, print error message
- if(size<=0)
- cout << "The grid can only have a positive number of rows and columns." << endl;
- // if the argument size is a positive number, print the grid
- else
- {
- /* outer for loop is repeated size number of times to print the following pattern
- size times
- +--+--+--+
- | | | | */
- for(int i=1; i<= size; i++)
- {
- /* This inner for loop prints the pattern "+--" size number of times in one row */
- for(int i=1; i<=size; i++)
- cout << "+--" ;
- /* end every row by printing a final + symbol and moves cursor to next line */
- cout << "+" << endl;
- /* This inner for loop prints the pattern "| " size number of times in next row */
- for(int i=1; i<=size; i++)
- cout << "| " ;
- /* end that row by printing a final | symbol and moves cursor to next line */
- cout << "|" << endl;
- }
- /** this for loop is to print the last line of the pattern */
- for(int i=1; i<=size; i++)
- cout << "+--" ;
- cout << "+" << endl;
- }
- }
- int main()
- {
- // call the printGrid method with 3 as the argument
- cout << "printGrid(3)" << endl;
- cout << "************" << endl << endl;
- printGrid(3);
- cout << endl;
- // call the printGrid method with -4 as the argument
- cout << "printGrid(-4)" << endl;
- cout << "************" << endl << endl;
- printGrid(-4);
- }
- ====================================================================================
- https://media.cheggcdn.com/media/49e/49e5eb8c-0d12-4531-9f3a-5b0e8174a83b/phpkTImlW.png
- https://media.cheggcdn.com/media/66e/66e126c3-7b90-4b40-97ae-632a60004d36/phpW9WzBZ.png
- ======================================================================================
- Sample output
- https://media.cheggcdn.com/media/2c5/2c532078-533a-497c-aba7-e7bbc9e73d2e/phpE27qgT.png
Advertisement
Add Comment
Please, Sign In to add comment