Programming in C

(Barry) #1
Functions and Arrays 151

{
int row, column;


for ( row = 0; row < nRows; ++row )
for ( column = 0; column < nCols; ++column )
matrix[row][column] *= scalar;
}


void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols])
{
int row, column;


for ( row = 0; row < nRows; ++row) {
for ( column = 0; column < nCols; ++column )
printf ("%5i", matrix[row][column]);

printf ("\n");
}
}


Program 8.13A Output


Original matrix:
7 16 55 13 12
12 10 52 0 7
-2 1 2 4 9


Multiplied by 2:
14 32 110 26 24
24 20 104 0 14
-4 2 4 8 18


Then multiplied by -1:
-14 -32 -110 -26 -24
-24 -20 -104 0 -14
4 -2 -4 -8 -18


The function declaration for scalarMultiplylooks like this:


void scalarMultiply (int nRows, int nCols, int matrix[nRows][nCols], int scalar)


The rows and columns in the matrix,nRows,and nCols,must be listed as arguments
beforethe matrix itself so that the compiler knows about these parameters before it
encounters the declaration of matrix in the argument list. If you try it this way instead:


void scalarMultiply (int matrix[nRows][nCols], int nRows, int nCols, int scalar)


Program 8.13A Continued

Free download pdf