Skip to content

Commit 5504028

Browse files
committed
variable multidimensional
1 parent 702f988 commit 5504028

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

prog.8.13A.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
3+
int main (void)
4+
{
5+
void scalarMultiply (int nRows, int nCols,
6+
int matrix[nRows][nCols], int scalar);
7+
void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols]);
8+
int sampleMatrix[3][5] =
9+
{
10+
{ 7, 16, 55, 13, 12 },
11+
{ 12, 10, 52, 0, 7 },
12+
{ -2, 1, 2, 4, 9 }
13+
};
14+
15+
printf ("Original matrix:\n");
16+
displayMatrix (3, 5, sampleMatrix);
17+
18+
scalarMultiply (3, 5, sampleMatrix, 2);
19+
20+
printf ("\nMultiplied by 2:\n");
21+
displayMatrix (3, 5, sampleMatrix);
22+
23+
scalarMultiply (3, 5, sampleMatrix, -1);
24+
25+
printf ("\nThen multiplied by -1:\n");
26+
displayMatrix (3, 5, sampleMatrix);
27+
28+
return 0;
29+
}
30+
31+
// Function to multiply a 3 x 5 array by a scalar
32+
33+
void scalarMultiply (int nRows, int nCols,
34+
int matrix[nRows][nCols], int scalar)
35+
{
36+
int row, column;
37+
38+
for ( row = 0; row < nRows; ++row )
39+
for ( column = 0; column < nCols; ++column )
40+
matrix[row][column] *= scalar;
41+
}
42+
43+
void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols])
44+
{
45+
int row, column;
46+
47+
for ( row = 0; row < nRows; ++row ) {
48+
for ( column = 0; column < nCols; ++column )
49+
printf ("%5i", matrix[row][column]);
50+
printf ("\n");
51+
}
52+
}

0 commit comments

Comments
 (0)