jaredec18

Untitled

Sep 21st, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. C++ program
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // definition of printGrid method to print the grid of "size" rows and "size" columns
  7. void printGrid(int size)
  8. {
  9. // if the argument size is a negative number, print error message
  10. if(size<=0)
  11. cout << "The grid can only have a positive number of rows and columns." << endl;
  12. // if the argument size is a positive number, print the grid
  13. else
  14. {
  15. /* outer for loop is repeated size number of times to print the following pattern
  16. size times
  17. +--+--+--+
  18. | | | | */
  19. for(int i=1; i<= size; i++)
  20. {
  21. /* This inner for loop prints the pattern "+--" size number of times in one row */
  22. for(int i=1; i<=size; i++)
  23. cout << "+--" ;
  24. /* end every row by printing a final + symbol and moves cursor to next line */
  25. cout << "+" << endl;
  26. /* This inner for loop prints the pattern "| " size number of times in next row */
  27. for(int i=1; i<=size; i++)
  28. cout << "| " ;
  29. /* end that row by printing a final | symbol and moves cursor to next line */
  30. cout << "|" << endl;
  31. }
  32. /** this for loop is to print the last line of the pattern */
  33. for(int i=1; i<=size; i++)
  34. cout << "+--" ;
  35. cout << "+" << endl;
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. // call the printGrid method with 3 as the argument
  42. cout << "printGrid(3)" << endl;
  43. cout << "************" << endl << endl;
  44. printGrid(3);
  45. cout << endl;
  46.  
  47. // call the printGrid method with -4 as the argument
  48. cout << "printGrid(-4)" << endl;
  49. cout << "************" << endl << endl;
  50. printGrid(-4);
  51. }
  52.  
  53. ====================================================================================
  54. https://media.cheggcdn.com/media/49e/49e5eb8c-0d12-4531-9f3a-5b0e8174a83b/phpkTImlW.png
  55.  
  56. https://media.cheggcdn.com/media/66e/66e126c3-7b90-4b40-97ae-632a60004d36/phpW9WzBZ.png
  57. ======================================================================================
  58.  
  59. Sample output
  60. https://media.cheggcdn.com/media/2c5/2c532078-533a-497c-aba7-e7bbc9e73d2e/phpE27qgT.png
Advertisement
Add Comment
Please, Sign In to add comment