Advertisement
PsichiX

some 2D array matrix operations

Nov 10th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. #define SAFE_DELETE_OBJECT(ptr)     if( ptr ){ delete ptr; ptr = 0; }
  7. #define SAFE_DELETE_ARRAY(ptr)      if( ptr ){ delete[] ptr; ptr = 0; }
  8.  
  9. int zeros( int** matrix, int rows, int cols )
  10. {
  11.     int count = 0;
  12.     for( int row = 0; row < rows; row++ )
  13.         for( int col = 0; col < cols; col++ )
  14.             if( matrix[ row ][ col ] == 0 )
  15.                 count++;
  16.     return count;
  17. }
  18.  
  19. int evens( int** matrix, int rows, int cols )
  20. {
  21.     int count = 0;
  22.     for( int row = 0; row < rows; row++ )
  23.         for( int col = 0; col < cols; col++ )
  24.             if( matrix[ row ][ col ] % 2 == 0 )
  25.                 count++;
  26.     return count;
  27. }
  28.  
  29. int rowSum( int** matrix, int rowIndex, int cols )
  30. {
  31.     int sum = 0;
  32.     for( int col = 0; col < cols; col++ )
  33.         sum += matrix[ rowIndex ][ col ];
  34.     return sum;
  35. }
  36.  
  37. int colMul( int** matrix, int colIndex, int rows )
  38. {
  39.     int mul = 1;
  40.     for( int row = 0; row < rows; row++ )
  41.         mul *= matrix[ row ][ colIndex ];
  42.     return mul;
  43. }
  44.  
  45. int edgeSum( int** matrix, int rows, int cols )
  46. {
  47.     int sum = 0;
  48.     for( int row = 0; row < rows; row++ )
  49.         for( int col = 0; col < cols; col++ )
  50.             // inline if-statement. with this line you can gain attention of your instructor :)
  51.             sum += row == 0 || row == rows - 1 ? matrix[ row ][ col ] : ( col == 0 || col == cols - 1 ? matrix[ row ][ col ] : 0 );
  52.     return sum;
  53. }
  54.  
  55. int main()
  56. {
  57.     int cols;
  58.     cout << "Matrix columns count: ";
  59.     cin >> cols;
  60.     cols = max( cols, 1 ); // columns cannot be less than one.
  61.    
  62.     int rows;
  63.     cout << "Matrix rows count: ";
  64.     cin >> rows;
  65.     rows = max( rows, 1 ); // rows cannot be less than one.
  66.    
  67.     // create 2D array with non constant size (hint: dynamic arrays).
  68.     int** matrix = new int*[ rows ];
  69.     for( int row = 0; row < rows; row++ )
  70.         matrix[ row ] = new int[ cols ];
  71.    
  72.     // iterate through matrix items and set their values.
  73.     for( int row = 0; row < rows; row++ )
  74.     {
  75.         for( int col = 0; col < cols; col++ )
  76.         {
  77.             cout << "Value at row " << row << ", column " << col << ": ";
  78.             cin >> matrix[ row ][ col ];
  79.         }
  80.     }
  81.    
  82.     cout << "Zero-values count: " << zeros( matrix, rows, cols ) << endl;
  83.    
  84.     cout << "Even-values count: " << evens( matrix, rows, cols ) << endl;
  85.    
  86.     int rowIndex;
  87.     cout << "Type row index: ";
  88.     cin >> rowIndex;
  89.     if( rowIndex >= 0 && rowIndex < rows )
  90.         cout << "Row-values sum: " << rowSum( matrix, rowIndex, cols ) << endl;
  91.     else
  92.         cout << "Row index is out of matrix bounds!" << endl;
  93.    
  94.     int colIndex;
  95.     cout << "Type column index: ";
  96.     cin >> colIndex;
  97.     if( colIndex >= 0 && colIndex < cols )
  98.         cout << "Column-values multiplication: " << colMul( matrix, colIndex, rows ) << endl;
  99.     else
  100.         cout << "Column index is out of matrix bounds!" << endl;
  101.    
  102.     cout << "Edge-values sum: " << edgeSum( matrix, rows, cols );
  103.    
  104.     // always delete dynamicaly created objects.
  105.     // if we use N>1 dimensions of arrays, always we need to delete it's content, because deleting root of arrays do not remove their childs.
  106.     for( int row = 0; row < rows; row++ )
  107.         SAFE_DELETE_ARRAY( matrix[ row ] );
  108.     SAFE_DELETE_ARRAY( matrix );
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement