Question: C++ Program Add additional code to my program The program needs to have validation on the A and B txt files like the program should

C++ Program

Add additional code to my program The program needs to have validation on the A and B txt files like the program should not count the value if there is a non-numeral value, the program should still process even if there is a non-numerical

Source code

#include #include #include #include #include

using namespace std;

/* matrix A and matrix B should have same dimension */ float** addition(float **A,float **B,int m,int n){ int i,j,r;

/* creating 2d array */ float **C = new float*[m]; for (r = 0; r < m; r++) { C[r] = new float[n]; }

/* 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 = new float*[m]; for (r = 0; r < m; r++) { C[r] = new float[n]; }

/* subtract corresponding matrix elements */ for(i=0;i

/* multiplication of two matrix */ /* dimension of matrix A is mXp dimension 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 = new float*[m]; for (r = 0; r < m; r++) { C[r] = new float[n]; }

/* 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; cout<<"--MENU "; cout<<"1. new values for A imported from a file "; cout<<"2. new values for B imported from a file "; cout<<"3. compute A+B "; cout<<"4. compute A-B "; cout<<"5. compute AB "; cout<<"6. compute BA "; cout<<"7. export the latest valid computation to a matrix file specified by the user "; cout<<"8. terminate the application "; cout<<" Choose user choice : ";

while(1){ cin>>choice; /* if choice is valid */ if(choice>=1 && choice<=8){ break; } else{ cout<<"Please choose between 1 to 8 : "; } }

return choice; }

/* read matrix from file */ float **readMatrix(char file[],int *m,int *n){ /* open file */ ifstream fp(file); char line[100]; char num[10]; float **C; int len,i,r=0,j=0,x=0,y=0; *m=0; *n=0;

string str; /* read number of row */ while(getline(fp,str)){ r++; } *m=r; fp.close();

/* create partially 2d array */ C = new float*[*m]; /* re open file again */ fp.open(file); /* read line by line */ while(getline(fp,str)){ len=str.length(); strcpy(line,str.c_str()); r=0; /* get number of column */ for(i=0;i

r=0; for(i=0;i

fp.close();

return C; }

int main(int argc,char *argv[] ){ ofstream 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);

cout<<" matrix A is "; print(A,am,an); cout<<" ";

cout<<" matrix B is "; print(B,bm,bn); cout<<" "; }

else{ cout<<"Enter filename to read A : "; cin>>filename; A=readMatrix(filename,&am,&an); cout<<" matrix A is "; print(A,am,an); cout<<" ";

cout<<"Enter filename to read B : "; cin>>filename; B=readMatrix(filename,&bm,&bn); cout<<" matrix B is "; print(B,bm,bn); cout<<" "; }

while(1){ option=menu();

if(option==1){ cout<<"Enter filename to read A : "; cin>>filename; A=readMatrix(filename,&am,&an); cout<<" matrix A is "; print(A,am,an); cout<<" "; } if(option==2){ cout<<"Enter filename to read B : "; cin>>filename; B=readMatrix(filename,&bm,&bn); cout<<" matrix B is "; print(B,bm,bn); cout<<" "; } if(option==3){ if(am==bm && an==bn){ C=addition(A,B,am,an); cm=am; cn=an; cout<<" --Output"; cout<<" matrix (A+B) is "; print(C,cm,cn); cout<<" "; } else{ cout<<"Dimensions does not match "; } } if(option==4){ if(am==bm && an==bn){ C=subtraction(A,B,am,an); cm=am; cn=an; cout<<" --Output"; cout<<" matrix (A-B) is "; print(C,cm,cn); cout<<" "; } else{ cout<<"Dimensions does not match "; } } if(option==5){ if(an==bm){ C=mult(A,B,am,bm,bn); cm=am; cn=bn; cout<<" --Output"; cout<<" matrix (AB) is "; print(C,cm,cn); cout<<" "; } else{ cout<<"Dimensions does not match "; } } if(option==6){ if(bn==am){ C=mult(B,A,bm,bn,an); cm=bm; cn=an; cout<<" --Output"; cout<<" matrix (BA) is "; print(C,cm,cn); cout<<" "; } else{ cout<<"Dimensions does not match "; } } if(option==7){ cout<<" Enter filename to write : "; cin>>filename; fp1.open(filename); for(i=0;i

fp1<<" "; } fp1.close(); cout<<" Saved!"; cout<<" "; } if(option==8){ break; } }

cout<<" Program End.";

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!