Question: CONVERT C TO C++ PROGRAM **Add comments on how the code works SOURCE CODE #include #include #include /* matrix A and matrix B should have
CONVERT C TO C++ PROGRAM
**Add comments on how the code works
SOURCE CODE
#include
/* matrix A and matrix B should have same dimention */ float** addition(float **A,float **B,int m,int n){ int i,j,r;
/* creating 2d array */ float **C = (float **)malloc(m * sizeof(float *)); for (r = 0; r < m; r++) { C[r] = (float *)malloc(n* sizeof(float)); }
/* add corresponding matrix elements */ for(i=0;i return C; } /* subtraction of two matrix */ float** subtraction(float **A,float **B,int m,int n){ int i,j,r; /* creating 2d array */ float **C = (float **)malloc(m * sizeof(float *)); for (r = 0; r < m; r++) { C[r] = (float *)malloc(n* sizeof(float)); } /* subtract corresponding matrix elements */ for(i=0;i /* multiplication of two matrix */ /* dimention of matrix A is mXp dimention of matrix B is pXn */ float** mult(float **A,float **B,int m,int p,int n){ int i,j,r; /* creating 2d array */ float **C = (float **)malloc(m * sizeof(float *)); for (r = 0; r < m; r++) { C[r] = (float *)malloc(n* sizeof(float)); } /* multiplication calculation */ for(i=0;i /* print matrix */ void print(float **c,int m,int n){ int i,j; for(i=0;i /* user menu choice */ int menu(){ int choice; printf("--MENU "); printf("1. new values for A imported from a file "); printf("2. new values for B imported from a file "); printf("3. compute A+B "); printf("4. compute A-B "); printf("5. compute AB "); printf("6. compute BA "); printf("7. export the latest valid computation to a matrix file specified by the user "); printf("8. terminate the application "); printf("Choose user choice : "); while(1){ scanf("%d",&choice); /* if choice is valid */ if(choice>=1 && choice<=8){ break; } else{ printf("Please choose between 1 to 8 : "); } } return choice; } /* read matrix from file */ float **readMatrix(char file[],int *m,int *n){ /* open file */ FILE *fp=fopen(file,"r"); char line[100]; char num[10]; float **C; int len,i,r=0,j=0,x=0,y=0; *m=0; *n=0; /* read number of row */ while(fgets(line,sizeof(line),fp)!=NULL){ r++; } *m=r; fclose(fp); /* create prtially 2d array */ C = (float **)malloc(*m * sizeof(float *)); /* re open file again */ fp=fopen(file,"r"); /* read line by line */ while(fgets(line,sizeof(line),fp)!=NULL){ len=strlen(line); r=0; /* get number of column */ for(i=0;i /* create complete 2d array */ C[j] = (float *)malloc(*n* sizeof(float)); r=0; for(i=0;i x=0; r++; } else{ /* add non space character */ if(line[i]!=' '){ num[x]=line[i]; x++; } } } num[x]=line[i]; num[x+1]='\0'; /* convert string to float */ C[j][r]=atof(num); x=0; /* increment number of row */ j++; } fclose(fp); return C; } int main(int argc,char *argv[] ){ FILE *fp1; int m=3,p,n=4,r,i,j; int cm,cn; int am=0,an=0; int bm=0,bn=0; float **A,**B,**C; char line[100]; int option; char filename[20]; if(argc==3){ A=readMatrix(argv[1],&am,&an); B=readMatrix(argv[2],&bm,&bn); printf(" matrix A is "); print(A,am,an); printf(" "); printf(" matrix B is "); print(B,bm,bn); printf(" "); } else{ printf("Enter filename to read A : "); scanf("%s",filename); A=readMatrix(filename,&am,&an); printf(" matrix A is "); print(A,am,an); printf(" "); printf("Enter filename to read B : "); scanf("%s",filename); B=readMatrix(filename,&bm,&bn); printf(" matrix B is "); print(B,bm,bn); printf(" "); } while(1){ option=menu(); if(option==1){ printf("Enter filename to read A : "); scanf("%s",filename); A=readMatrix(filename,&am,&an); printf(" matrix A is "); print(A,am,an); printf(" "); } if(option==2){ printf("Enter filename to read B : "); scanf("%s",filename); B=readMatrix(filename,&bm,&bn); printf(" matrix B is "); print(B,bm,bn); printf(" "); } if(option==3){ if(am==bm && an==bn){ C=addition(A,B,am,an); cm=am; cn=an; printf(" --Output"); printf(" matrix (A+B) is "); print(C,cm,cn); printf(" "); } else{ printf("Dimentions does not match "); } } if(option==4){ if(am==bm && an==bn){ C=subtraction(A,B,am,an); cm=am; cn=an; printf(" --Output"); printf(" matrix (A-B) is "); print(C,cm,cn); printf(" "); } else{ printf("Dimentions does not match "); } } if(option==5){ if(an==bm){ C=mult(A,B,am,bm,bn); cm=am; cn=bn; printf(" --Output"); printf(" matrix (AB) is "); print(C,cm,cn); printf(" "); } else{ printf("Dimentions does not match "); } } if(option==6){ if(bn==am){ C=mult(B,A,bm,bn,an); cm=bm; cn=an; printf(" --Output"); printf(" matrix (BA) is "); print(C,cm,cn); printf(" "); } else{ printf("Dimentions does not match "); } } if(option==7){ printf(" Enter filename to write : "); scanf("%s",filename); fp1=fopen(filename,"w"); for(i=0;i printf(" Program End."); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
