#include <stdio.h> #define ROWS 3 #define COLS 3 void matrixMultiply(int *mat1, int *mat2, int *result, int rows1, int cols1, int cols2) { int i, j, k; // Multiplying matrices for (i = 0; i < rows1; i++) { for (j = 0; j < cols2; j++) { *(result + i * cols2 + j) = 0; for (k = 0; k < cols1; k++) { *(result + i * cols2 + j) += *(mat1 + i * cols1 + k) * *(mat2 + k * cols2 + j); } } } } void displayMatrix(int *mat, int rows, int cols) { int i, j; // Displaying matrix for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("%d\t", *(mat + i * cols + j)); } printf("\n"); } } int main() { int mat1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int mat2[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {
Write a C program (use a switch statement for selection) to add or subtract 2 matrices having order 3 x 3, depending upon the choice made by the user
Write a C program (use a switch statement for selection) to add or subtract 2 matrices having order 3 x 3, depending upon the choice made by the user.
MCS-011 : PROBLEM SOLVING AND PROGRAMMING (June, 2017)
c program for matrix operations using switch case, c program for addition and subtraction of two matrices, algorithm for matrix addition in c program
#include <stdio.h>
#include<conio.h>
void main() {
int first_matrix[3][3], second_matrix[3][3], sub_of_two_matrix[3][3],sum_of_two_matrix[3][3], i, j;
int oprater;
clrscr();
//Enter First Matrix
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &first_matrix[i][j]);
}
//Enter Second Matrix
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
printf("Enter element %d%d: ", i + 1, j + 1);
scanf("%d", &second_matrix[i][j]);
}
printf("if u add matrix then enter 1 or if u subtract matrix 2 : ");
scanf("%d",&oprater);
switch(oprater)
{
case 1:
// adding two matrices
for (i = 0; i <3; i++){
for (j = 0; j < 3; j++) {
sum_of_two_matrix[i][j] = first_matrix[i][j] + second_matrix[i][j];
}
}
// printing the result of Sum Two Matrix
printf("\nSum of two matrices: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sum_of_two_matrix[i][j]);
}
printf("\n");
}
break;
//Second Switch Condition
case 2:
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++) {
sub_of_two_matrix[i][j] = first_matrix[i][j] - second_matrix[i][j];
}
}
// printing the result
printf("\nSubtraction of two matrices: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sub_of_two_matrix[i][j]);
}
printf("\n");
}
// break;
default :
printf("Enter Currect Value ");
}
getch();
}
What is Matrix ?
In mathematics, a matrix may be a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. for instance , the dimension of the matrix below is 2 × 3, because there are two rows and three columns: as long as they need an equivalent size, two matrices are often added or subtracted element by element.
The matrix dimension of representation of row and column the 2x3 means First is Column and second is Row
2 | 3 | 4 |
4 | 4 | 4 |
This is 2 Row and 3 Column, in this question 3x3 ka create a matrix.
Step 1. Header file is defined predefined function
#include <stdio.h>
#include<conio.h>
#include <stdio.h> standard input output function in this file contain function and include stdio file and use predefined function in contain in file.
printf(), scanf(); and more function in this file.
#include<conio.h> console input and output in this file defined function control output console screen getch() is defined function in this file.
what is console screen ?
Console screen is a runtime output screen. output black screen is called console screen.
step 2. void main() {}
it is void main function is control our instruction, how to flow our program is handle main function.
it is instruction body is write code in this main function body.
void main(){
//write any code.
}
step 3. int first_matrix[3][3], second_matrix[3][3], sub_of_two_matrix[3][3],sum_of_two_matrix[3][3], i, j;
it is defined variable to store temporary value, first_matrix[3][3] in this type create 2d array.
clrscr(); is clear perverse output on the screen.
step 4.
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &first_matrix[i][j]);
}
}
input a first Matrix using scanf function and for loop.
step 5.
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
printf("Enter element %d%d: ", i + 1, j + 1);
scanf("%d", &second_matrix[i][j]);
}
input second matrix
Step 6.for (i = 0; i <3; i++){
for (j = 0; j < 3; j++) {
sum_of_two_matrix[i][j] = first_matrix[i][j] + second_matrix[i][j];
}
}
// printing the result of Sum Two Matrix
printf("\nSum of two matrices: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sum_of_two_matrix[i][j]);
}
printf("\n");
}In this question add and subtract matrix by switch condition.input two matrix after input ask u user to add matrix to input 1 and subtract input 2.create a switch condition passing a argument and create case 1: here adding condition.case 2: is subtract matrix and print result according to your input 1 to add and second 2.
Kitna kama lete ho
ReplyDelete