Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<string>;
- #include<sstream>;
- using namespace std;
- int main()
- {
- int row, col;
- cin >> row >> col; cin.ignore();
- string input;
- stringstream lineIn;
- int elm;
- //Allocate memory and initilize
- int** array2D = new int*[row];
- for (int i = 0; i < row; i++)
- {
- array2D[i] = new int[col];
- getline(cin, input);
- lineIn << input;
- int j = 0;
- while (lineIn >> elm)
- {
- array2D[i][j] = elm;
- j++;
- if (j > col)
- {
- cout << "Error";
- return 0;
- }
- }
- //Clear buffer
- lineIn.str("");
- lineIn.clear();
- }
- int printRow, printCol;
- cin >> printRow >> printCol; cin.ignore();
- for (int i = 0; i < printRow; i++)
- {
- for (int j = 0; j < printCol; j++)
- {
- cout << array2D[i][j] << " ";
- }
- cout << endl;
- }
- //Deallocate the memory
- for (int i = 0; i < row; i++)
- {
- delete[] array2D[i];
- }
- delete[] array2D;
- }
- //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