3
\$\begingroup\$

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 <stdio.h>

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");
    }
}
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$
  1. 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}
    }
    
  2. 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}
    }
    
  3. 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.

  4. 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.

  5. 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);
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.