fork download
  1. // Attached: HW_2b
  2. // ===========================================================
  3. // File: HW_2b
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. using namespace std;
  12.  
  13. // Function prototypes
  14. void getData(int array[][4], int rows);
  15. void displayArray(int array[][4], int rows);
  16. void clearScreen();
  17.  
  18. int main()
  19. {
  20. const int ROWS = 3;
  21. const int COLS = 4;
  22.  
  23. int numArray[ROWS][COLS];
  24.  
  25. getData(numArray, ROWS);
  26. displayArray(numArray, ROWS);
  27.  
  28. return 0;
  29. }
  30.  
  31. // ===========================================================
  32.  
  33. void getData(int array[][4], int rows)
  34. {
  35. cout << "Enter integers into the 2-Dimensional array:\n";
  36.  
  37. for (int row = 0; row < rows; row++)
  38. {
  39. for (int col = 0; col < 4; col++)
  40. {
  41. cout << "Enter a number: ";
  42. cin >> array[row][col];
  43. }
  44. }
  45. }
  46.  
  47. // ===========================================================
  48.  
  49. void displayArray(int array[][4], int rows)
  50. {
  51. clearScreen();
  52.  
  53. cout << "Here is the data in the 2-Dimensional array:\n";
  54.  
  55. for (int row = 0; row < rows; row++)
  56. {
  57. for (int col = 0; col < 4; col++)
  58. {
  59. cout << setw(6) << array[row][col];
  60. }
  61. cout << endl;
  62. }
  63.  
  64. cout << "\nPress any key to continue . . .";
  65. cin.get();
  66. cin.get();
  67. }
  68.  
  69. // ===========================================================
  70.  
  71. void clearScreen()
  72. {
  73. system("cls"); // Windows
  74. // system("clear"); // Mac/Linux
  75. }
  76.  
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
1
45
765
6
32
45
789
343
22
64
12
555
stdout
Enter integers into the 2-Dimensional array:
Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Here is the data in the 2-Dimensional array:
     1    45   765     6
    32    45   789   343
    22    64    12   555

Press any key to continue . . .
stderr
sh: 1: cls: not found