HRusev

03.Print in Parts

May 29th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include<string>;
  3. #include<sstream>;
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int row, col;
  10.     cin >> row >> col; cin.ignore();
  11.  
  12.     string input;
  13.     stringstream lineIn;
  14.     int elm;
  15.     //Allocate memory and initilize
  16.     int** array2D = new int*[row];
  17.     for (int i = 0; i < row; i++)
  18.     {
  19.         array2D[i] = new int[col];
  20.         getline(cin, input);
  21.         lineIn << input;
  22.         int j = 0;
  23.         while (lineIn >> elm)
  24.         {
  25.             array2D[i][j] = elm;
  26.             j++;
  27.             if (j > col)
  28.             {
  29.                 cout << "Error";
  30.                 return 0;
  31.             }
  32.         }
  33.  
  34.         //Clear buffer
  35.         lineIn.str("");
  36.         lineIn.clear();
  37.  
  38.     }
  39.  
  40.  
  41.     int printRow, printCol;
  42.     cin >> printRow >> printCol; cin.ignore();
  43.  
  44.  
  45.     for (int i = 0; i < printRow; i++)
  46.     {
  47.         for (int j = 0; j < printCol; j++)
  48.         {
  49.             cout << array2D[i][j] << " ";
  50.         }
  51.         cout << endl;
  52.     }
  53.  
  54.     //Deallocate the memory
  55.     for (int i = 0; i < row; i++)
  56.     {
  57.         delete[] array2D[i];
  58.     }
  59.     delete[] array2D;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. //Warning   C6386   Buffer overrun while writing to 'array2D[i]':  the writable size is 'col*4' bytes, but '8' bytes might be written.
Advertisement
Add Comment
Please, Sign In to add comment