November 03, 2012
C Program To Add, Subtract or Multiply two Matrices
- Add, Subtract or Multiply two Matrices- Command line based
- Menu driven
- Uses Structs, functions, dynamic allocations etc.
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- struct matrix{
- int **elements;
- int rows;
- int cols;
- };
- matrix read(){
- int rows = 0, cols = 0, i, j;
- matrix m = {NULL, 0, 0};
- printf("\nEnter the number of rows: ");
- scanf("%d", &rows);
- printf("\nEnter the number of columns: ");
- scanf("%d", &cols);
- if( rows <= 0 || cols <= 0){
- printf("%dx%d matrix does not exist!", rows, cols);
- }else {
- m.rows = rows;
- m.cols = cols;
- m.elements = (int **)malloc(m.rows * sizeof(int *));
- for( i = 0; i < rows; i++){
- m.elements[i] = (int *)malloc(m.cols * sizeof(int));
- for( j = 0; j < cols; j++){
- printf("\nEnter element at %d x %d: ", i + 1, j + 1);
- scanf("%d", &m.elements[i][j]);
- }
- }
- }
- return m;
- }
- void display(char msg[], matrix m){
- int i, j;
- printf("\n========== %s ==========\n", msg);
- for(i = 0; i < m.rows; i++){
- for(j = 0; j < m.cols; j++){
- printf("%d\t", m.elements[i][j]);
- }
- printf("\n");
- }
- }
- matrix process(matrix m1, matrix m2, char opr){
- int i, j, k;
- matrix m = {NULL, 0, 0};
- if((opr == '1' || opr == '2') && (m1.rows != m2.rows || m1.cols != m2.cols)){
- printf("\nMatrix addition or subtraction is not possible since number of rows and columns of the two matrices are not equal.");
- }else if(opr == '3' && m1.cols != m2.rows){
- printf("\nMatrix multiplication is not possible since number of columns of first matrix is not equal to number of rows of second matrix.");
- }else{
- if(opr == '1' || opr == '2'){
- m.rows = m1.rows;
- m.cols = m1.cols;
- m.elements = (int **)malloc(m.rows * sizeof(int *));
- for(i = 0; i < m1.rows; i++){
- m.elements[i] = (int *)malloc(m.cols * sizeof(int));
- for( j = 0; j < m1.cols; j++){
- if(opr == '1'){
- m.elements[i][j] = m1.elements[i][j] + m2.elements[i][j];
- }else if(opr == '2'){
- m.elements[i][j] = m1.elements[i][j] - m2.elements[i][j];
- }
- }
- }
- }else if(opr == '3'){
- m.rows = m1.rows;
- m.cols = m2.cols;
- m.elements = (int **)malloc(m.rows * sizeof(int *));
- for(i = 0; i < m1.rows; i++){
- m.elements[i] = (int *)malloc(m.cols * sizeof(int));
- for( j = 0; j < m2.cols; j++){
- m.elements[i][j] = 0;
- for(k = 0; k < m2.rows; k++){
- m.elements[i][j] += m1.elements[i][k] * m2.elements[k][j];
- }
- }
- }
- }
- }
- return m;
- }
- int main(){
- while(1){
- printf("\nPress 1 to add two matrices\nPress 2 to subtract\nPress 3 to multiply two matrices\nPress q to quit");
- char opt = getch();
- if(opt == 'q' || opt == 'Q'){
- break;
- }
- matrix m1 = read();
- matrix m2 = read();
- if(m1.elements != NULL && m2.elements != NULL && (opt == '1' || opt == '2' || opt == '3')){
- matrix m3 = process(m1, m2, opt);
- if(m3.elements != NULL){
- display("Matrix 1", m1);
- display("Matrix 2", m2);
- display("Result", m3);
- }
- }
- }
- return 0;
- }
Related Posts
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment