Saturday, 5 May 2012

Dynamic 2D Arrays in C


  int rows = 0;
  int cols = 0;


  int** two_d_int_array;


  cout<<"Please enter number of rows and columns: ";
  cin>>rows>>cols;


  two_d_int_array = new int*[rows];

  for(int i=0; i<rows; i++)
      two_d_int_array[i] = new int[cols];


  for(int i=0; i<rows; i++)
      for(int j=0; j<cols; j++)
        two_d_int_array[i][j] = j+1;


  for(int i=0; i<rows; i++){
      for(int j=0; j<cols; j++){
        cout<<two_d_int_array[i][j];
     }
     cout<<endl;
  }
  for(int i = 0; i<rows; i++)
     delete[] two_d_int_array[i];


  delete[] two_d_int_array; 

No comments:

Post a Comment