Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define SAFE_DELETE_OBJECT(ptr) if( ptr ){ delete ptr; ptr = 0; }
- #define SAFE_DELETE_ARRAY(ptr) if( ptr ){ delete[] ptr; ptr = 0; }
- int zeros( int** matrix, int rows, int cols )
- {
- int count = 0;
- for( int row = 0; row < rows; row++ )
- for( int col = 0; col < cols; col++ )
- if( matrix[ row ][ col ] == 0 )
- count++;
- return count;
- }
- int evens( int** matrix, int rows, int cols )
- {
- int count = 0;
- for( int row = 0; row < rows; row++ )
- for( int col = 0; col < cols; col++ )
- if( matrix[ row ][ col ] % 2 == 0 )
- count++;
- return count;
- }
- int rowSum( int** matrix, int rowIndex, int cols )
- {
- int sum = 0;
- for( int col = 0; col < cols; col++ )
- sum += matrix[ rowIndex ][ col ];
- return sum;
- }
- int colMul( int** matrix, int colIndex, int rows )
- {
- int mul = 1;
- for( int row = 0; row < rows; row++ )
- mul *= matrix[ row ][ colIndex ];
- return mul;
- }
- int edgeSum( int** matrix, int rows, int cols )
- {
- int sum = 0;
- for( int row = 0; row < rows; row++ )
- for( int col = 0; col < cols; col++ )
- // inline if-statement. with this line you can gain attention of your instructor :)
- sum += row == 0 || row == rows - 1 ? matrix[ row ][ col ] : ( col == 0 || col == cols - 1 ? matrix[ row ][ col ] : 0 );
- return sum;
- }
- int main()
- {
- int cols;
- cout << "Matrix columns count: ";
- cin >> cols;
- cols = max( cols, 1 ); // columns cannot be less than one.
- int rows;
- cout << "Matrix rows count: ";
- cin >> rows;
- rows = max( rows, 1 ); // rows cannot be less than one.
- // create 2D array with non constant size (hint: dynamic arrays).
- int** matrix = new int*[ rows ];
- for( int row = 0; row < rows; row++ )
- matrix[ row ] = new int[ cols ];
- // iterate through matrix items and set their values.
- for( int row = 0; row < rows; row++ )
- {
- for( int col = 0; col < cols; col++ )
- {
- cout << "Value at row " << row << ", column " << col << ": ";
- cin >> matrix[ row ][ col ];
- }
- }
- cout << "Zero-values count: " << zeros( matrix, rows, cols ) << endl;
- cout << "Even-values count: " << evens( matrix, rows, cols ) << endl;
- int rowIndex;
- cout << "Type row index: ";
- cin >> rowIndex;
- if( rowIndex >= 0 && rowIndex < rows )
- cout << "Row-values sum: " << rowSum( matrix, rowIndex, cols ) << endl;
- else
- cout << "Row index is out of matrix bounds!" << endl;
- int colIndex;
- cout << "Type column index: ";
- cin >> colIndex;
- if( colIndex >= 0 && colIndex < cols )
- cout << "Column-values multiplication: " << colMul( matrix, colIndex, rows ) << endl;
- else
- cout << "Column index is out of matrix bounds!" << endl;
- cout << "Edge-values sum: " << edgeSum( matrix, rows, cols );
- // always delete dynamicaly created objects.
- // 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.
- for( int row = 0; row < rows; row++ )
- SAFE_DELETE_ARRAY( matrix[ row ] );
- SAFE_DELETE_ARRAY( matrix );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement