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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <iomanip>
void display(int** arr, int rows, int cols)
{
for (int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
std::cout << std::setw(5) << std::right << arr[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
// The array will be forever changed
void multiply(int** arr, int rows, int cols)
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
arr[i][j] *= 2;
}
}
}
int main()
{
int rows=3;
int cols=6;
int myArray[rows][cols] =
{
{1,2,3,4,5,6},
{10,20,30,40,50,60},
{100,200,300,400,500,600}
};
// Creating pointer access for compatibility between int[][] and int**
int* a[rows];
for (int i=0; i<rows; ++i)
a[i]=myArray[i];
std::cout << "Displayed:\n";
display(a, rows, cols);
std::cout << "Multiplied + Displayed:\n";
multiply(a, rows, cols);
display(a, rows, cols);
std::cout << "Multiplied + Displayed:\n";
multiply(a, rows, cols);
display(a, rows, cols);
return 0;
}
|