Sunday, January 29, 2012

C/C++ Exercise: Initializing two-dimension array

#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: Initializing two-dimension array" << endl << endl;

 const int size_i = 3;
 const int size_j = 4;
 int a[size_i][size_j] = 
 {
  {0, 1, 2, 3},
  {10}
 };

 cout << endl;
 cout << "Accessed as two dimension array" << endl;
 for(int i = 0; i < size_i; i++){
  for(int j = 0; j < size_j; j++){
   cout << a[i][j] << " ";
  }
  cout << endl;
 }

 cout << endl;

 int *p = *a;
 cout << "Accessed by pointer" << endl;
 for(int i = 0; i < size_i * size_j; i++){
  cout << i << ":" << *(p + i) << " ";
 }
 cout << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

C/C++ Exercise: Initializing two-dimension array

No comments:

Post a Comment