C++/Data Structure/Array Two Dimension

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Create a two-dimensional array of objects.

#include <iostream>
using namespace std;
class MyClass {
  int a;
public:
  MyClass(int n) { 
     a = n; 
  }
  int getA() { 
     return a; 
  }
};
int main()
{
  MyClass ob[4][2] = {
    1, 2,
    3, 4,
    5, 6,
    7, 8
  };
  int i;
  
  for(i = 0; i <4; i++) {
    cout << ob[ i ][0].getA() << " ";
    cout << ob[ i ][1].getA() << endl;
  }
  cout << endl;
  return 0;
}


Define and initiate a two-dimensional array

#include <iostream>
#include <iomanip>
using namespace std;
int matrix[3][5] = { { 215, 314, 216, 112,  8 },
                     { 119, 217, 214, 111,  4 },
                     {  61, 115, 315, 316, 118 } };
int rowsum[3];    
int colsum[5];    
                  
int matrixsum( int arr2D[][5], int vlen, int rsum[], int csum[]);
int main()
{
   cout << "Testing the function matrixsum()." << endl;
   int totalsum = matrixsum( matrix, 3, rowsum, colsum);
   cout << "The matrix with the sums of rows and columns:" << endl;
   int i,j;
   for( i = 0 ; i < 3 ; ++i)    
   {                            // matrix with row sums.
     for( j = 0 ; j < 5 ; ++j)
       cout << setw(8) << matrix[i][j];
     cout << " | " << setw(8) << rowsum[i] << endl;
   }
   cout << endl;
   for( j = 0 ;  j < 5  ;  ++j )
     cout << setw(8) << colsum[j];
   cout << " | " << setw(8) << totalsum << endl;
   return 0;
}
int matrixsum( int v[][5], int len, int rsum[], int csum[])
{  
   int ro, co;                      // Row and column index
   
   for( ro = 0 ; ro < len ; ++ro)    // To compute row sums
   {
      rsum[ro] = 0;
      for( co = 0 ; co < 5 ; ++co)
        rsum[ro] += v[ro][co];
   }
   for(co = 0 ; co < 5 ; ++co)       // Compute column sums
   {
      csum[co] = 0;
      for( ro = 0 ; ro < len ; ++ro)
        csum[co] += v[ro][co];
   }
   return (rsum[0] + rsum[1] + rsum[2]);  // Total sum =
}                                       // sum of row sums.


Init two dimension class array

#include <iostream>
using namespace std;
class MyClass {
  int a, b;
public:
  MyClass(int n, int m) { 
     a = n; 
     b = m; 
  }
  int getA() { 
     return a; 
  }
  int getB() { 
  
     return b; 
  }
};
int main()
{
  MyClass ob[4][2] = {
    MyClass(1, 2), MyClass(3, 4), 
    MyClass(5, 6), MyClass(7, 8),
    MyClass(9, 10), MyClass(11, 12),
    MyClass(13, 14), MyClass(15, 16)
  };
  int i;
  for(i = 0; i <4; i++) {
    cout << ob[ i ][0].getA() << " ";
    cout << ob[ i ][0].getB() << endl;
    cout << ob[ i ][1].getA() << " ";
    cout << ob[ i ][1].getB() << endl;
  }
  cout << endl;
  return 0;
}


Multidimensional Arrays

#include <iostream>
#include <iomanip>
using namespace std;
char representative[2][20] = {"AAAAAAAAAAAA",
                              "BBBBBBBBBBBB"};
int articleCount[2][5] = { { 120, 151, 130, 117, 144},
                            {150, 120, 190, 110, 188}
                          };
int main(){
   for( int i=0; i < 2; i++ ){
      cout <<"\nRepresentative:  " << representative[i];
      cout << "\nNumber of items sold: ";
      for( int j = 0; j < 5; j++ )
         cout << setw(6) << articleCount[i][j];
      cout << endl;
   }
    return 0;
}