0
#include <iostream>

using namespace std;

int main()
{
int a,b;
int arr[5][2];
for(int i = 0; i<3; i++)
{   
    cin >> a >> b;
    for(int j=0;j<2;j++)
    {
        arr[i][0]=a;
        arr[i][1]=b;
    }
}
cout << arr;
}

I just wanted to print the whole array for the 2-d array. e.g.- Input- 1 2 2 3 4 6 Output- {{1,2},{2,3},{4,6}}

  • arrays are just a contiguous block of memory, there's no actual "structure" to them. A reference to an array is a reference to a memory address where the array starts, with the spacing (in-memory) between values being equivalent to the size of the type in the array. In that sense, you still need to iterate over the array in some form to print its values (short of a library function) – Rogue Jun 11 '20 at 18:33
  • `for (auto &line : arr) { for (auto i : line) {cout << i << " ";} cout << endl;}}` – anastaciu Jun 11 '20 at 18:39
  • You correctly wrote a loop to read your array, why not also write a loop to output your array? – john Jun 11 '20 at 19:13
  • Incidentally there's no need to do your assignments twice `for(int j=0;j<2;j++) { arr[i][0]=a; arr[i][1]=b; }` can be replaced simply by `arr[i][0]=a; arr[i][1]=b;` – john Jun 11 '20 at 19:14

0 Answers0