HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Applying scalar multiplication to matrix

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applyingscalarmatrixmultiplication

Problem

This is one of my first programs in C. Please review and help me improve it. I create a matrix and apply scalar multiplication on it. Then I print the result.

#include 

int main(void)
{
    void scalarMultiply(int matrix[3][5], int scalar);
    void displayMatrix (int matrix[3][5]);

    int sampleMatrix[3][5] = {
        7,16,55,13,12,12,10,52,0,7,-2,1,2,4,9
    };

    printf("Original matrix:\n");
    displayMatrix(sampleMatrix);

    scalarMultiply (sampleMatrix, 2);
    printf ("\nMultiplied by 2:\n");
    displayMatrix (sampleMatrix);
    scalarMultiply (sampleMatrix, -1);
    printf ("\nThen multiplied by -1:\n");
    displayMatrix (sampleMatrix);
    return 0;

}

// Function to multiply a 3 x 5 array by a scalar
void scalarMultiply (int matrix[3][5], int scalar)
{
    int row, column;
    for ( row = 0; row < 3; ++row )
        for ( column = 0; column < 5; ++column )
            matrix[row][column] *= scalar;
}
void displayMatrix (int matrix[][5])
{
    int row, column;
    for ( row = 0; row < 3; ++row) {
        for ( column = 0; column < 5; ++column )
            printf ("  %i", matrix[row][column]);
        printf ("\n");
    }
}

Solution

-
Braces aren't required as initialization will proceed by row.
However it is much easier to read if you initialized your
two-dimensional array like this:

int sampleMatrix[3][5] = {
    {7,16,55,13,12},
    {12,10,52,0,7},
    {-2,1,2,4,9}
}


-
Also note, it is not required that the entire array be initialized.
If you use inner pairs of braces you can force initialization to
whatever you want. Unspecified values are set to zero by default.

int sampleMatrix[3][5] = {
    {7,16,55,13},  //Last element in each row is zero ; test it yourself
    {12,10,52,0},
    {-2,1,2,4}
}


-
Use variables to represent the number of rows and columns for the
matrices. Magic numbers are bad. This will also allow you to pass
matrices of different dimensions.

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


It is best practice to keep your function prototypes outside of
main(). Keeping it in main() makes it harder to find.

-
The outout layout looks a bit messy. Change your printf statement
to include a field width specifier.

printf ("%5i", matrix[row][column]);


%5i tells printf to display integers and their sign that take up 5 columns.

-
Use proper spacing:

scalarMultiply (sampleMatrix, 2);

    printf ("\nMultiplied by 2:\n");
    displayMatrix (sampleMatrix);

    scalarMultiply (sampleMatrix, -1);

    printf ("\nThen multiplied by -1:\n");  //Keep related functions together      
    displayMatrix (sampleMatrix);

Code Snippets

int sampleMatrix[3][5] = {
    {7,16,55,13,12},
    {12,10,52,0,7},
    {-2,1,2,4,9}
}
int sampleMatrix[3][5] = {
    {7,16,55,13},  //Last element in each row is zero ; test it yourself
    {12,10,52,0},
    {-2,1,2,4}
}
void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);
void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols]);
printf ("%5i", matrix[row][column]);
scalarMultiply (sampleMatrix, 2);

    printf ("\nMultiplied by 2:\n");
    displayMatrix (sampleMatrix);

    scalarMultiply (sampleMatrix, -1);

    printf ("\nThen multiplied by -1:\n");  //Keep related functions together      
    displayMatrix (sampleMatrix);

Context

StackExchange Code Review Q#67166, answer score: 2

Revisions (0)

No revisions yet.