Introduction
Scalar multiplication of a matrix involves multiplying each element of the matrix by a scalar value. This guide will show you how to write a C program that performs scalar multiplication of a matrix provided by the user.
Problem Statement
Create a C program that:
- Takes a matrix and a scalar value as input from the user.
- Multiplies each element of the matrix by the scalar value.
- Displays the resulting matrix.
Example:
- Input:
- Matrix:
1 2 3 4 5 6 7 8 9 - Scalar:
2
- Matrix:
- Output: Resulting Matrix:
2 4 6 8 10 12 14 16 18
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the matrix, its dimensions, the scalar value, and loop counters.
- Input the Dimensions and Elements of the Matrix: Use loops to take input from the user for the matrix.
- Input the Scalar Value: Use
scanfto take input for the scalar value. - Perform Scalar Multiplication: Use nested loops to multiply each element of the matrix by the scalar value.
- Display the Resulting Matrix: Use loops to display the resulting matrix.
C Program to Perform Scalar Multiplication of a Matrix
#include <stdio.h>
int main() {
// Step 1: Declare variables to hold the matrix, dimensions, and scalar
int rows, cols;
int matrix[100][100];
int scalar;
int i, j;
// Step 2: Prompt the user to enter the dimensions of the matrix
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
// Step 3: Input the elements of the matrix
printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Step 4: Input the scalar value
printf("Enter the scalar value: ");
scanf("%d", &scalar);
// Step 5: Perform scalar multiplication
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
matrix[i][j] *= scalar;
}
}
// Step 6: Display the resulting matrix
printf("Resulting matrix after scalar multiplication:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0; // Step 7: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variables
rowsandcolsstore the dimensions of the matrix. Thematrixarray stores the elements of the matrix. The variablescalarstores the scalar value by which the matrix will be multiplied. The variablesiandjare used as loop counters.
Step 2: Input the Dimensions of the Matrix
- The program prompts the user to enter the number of rows and columns for the matrix using
scanf.
Step 3: Input the Elements of the Matrix
- The program uses nested
forloops to take input for each element of the matrix from the user.
Step 4: Input the Scalar Value
- The program prompts the user to enter the scalar value using
scanf.
Step 5: Perform Scalar Multiplication
- The program uses nested
forloops to iterate through each element of the matrix:- Each element is multiplied by the scalar value, and the result is stored back in the matrix.
Step 6: Display the Resulting Matrix
- The program uses nested
forloops to display the elements of the resulting matrix after scalar multiplication.
Step 7: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the number of rows and columns of the matrix: 3 3
Enter elements of the matrix:
1 2 3
4 5 6
7 8 9
Enter the scalar value: 2
Resulting matrix after scalar multiplication:
2 4 6
8 10 12
14 16 18
Example 2:
Enter the number of rows and columns of the matrix: 2 2
Enter elements of the matrix:
10 20
30 40
Enter the scalar value: 3
Resulting matrix after scalar multiplication:
30 60
90 120
Conclusion
This C program demonstrates how to perform scalar multiplication of a matrix by multiplying each element by a scalar value. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.