1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <iomanip>
#include <valarray>
using namespace std;
const int rows = 3;
const int cols = 3;
void printMatrix( const char *prompt, const valarray<int> &matrix )
{
cout << prompt << '\n';
for ( int r = 0; r < rows; r++ )
{
for ( int c = 0; c < cols; c++ ) cout << setw( 3 ) << matrix[r*cols+c];
cout << '\n';
}
cout << '\n';
}
int main()
{
valarray<int> first { 15, 13, 11, 11, 9, 7, 7, 5, 4 };
valarray<int> second{ 12, 10, 8, 8, 6, 4, 4, 2, 1 };
valarray<int> diff = first - second; // <=========
printMatrix( "The first matrix is" , first );
printMatrix( "The second matrix is", second );
printMatrix( "The difference is" , diff );
}
|
The first matrix is
15 13 11
11 9 7
7 5 4
The second matrix is
12 10 8
8 6 4
4 2 1
The difference is
3 3 3
3 3 3
3 3 3 |