i was 2asked to develop a project to make mathematical opertaion on matrix but i was forced in the input method that would be [1 2;3 4] for 2x2 matrix and [1 2 3;4 5 6;7 8 9] for 3x3 matrix and i want to know how to take the float numbers from string to array without taking semicolon and spaces [ and]
i was 2asked to develop a project to make mathematical opertaion on matrix but i was
Collapse
X
-
If you need input like [1 2;3 4] then why do you think you need a string?
I mean, what the user sees is [1 2;3 4] and that doesn't mean using a string.
So display a [
scanf two floats
Display a ;
scanf two more floats
Display a ]
If this program is about matrix mathematics, then get the mathematics working first and then go back and work on the user input format. You can spend a lot of time formatting user input so I would take an easy way out until you have more experience. -
Two things here:esslam200 - the project description is attached in the link it is a project asked from me in faculty of engineering while i have recently studied c++
1) It is preferable to have a clear and complete description as the post, not as an attachment, especially an attachment stored at a third part site. One little opps, or deletion and the thread becomes worthless.
Along that same line, many of our experts help from within secured network settings (ie work) and the IT has restricted access to offsite storage. This is to protect the company from all sorts of nasties. :)
2) We don't do homework assignments. It's not fair to you nor other students to have the experts do the work for you.
If you want help on a homework assignment, then post your work first - many of us may provide direction in such cases depending on the amount of effort made by the student.Comment
-
hmmmz, and my post was just stating point 2 in the last answer.....
But it got 'moderated'.... ;)
But I more helpful answer from /me would be:
I know of a function called 'split'. I exists in PHP and in C#
The must be some equivalent for it in C++
But did your teacher not tell you a thing? Some stuff on how to solve this assignment MUST have been explained in the lessons you've got.... ;)Comment
-
Code:#include <iostream> #include <string> #include <cstdlib> #include <sstream> #include<string> #include<math.h> using namespace std; int main () { string a,b,op; float x[100][100],y[100][100],c,d; int nspace1=0,nsemi1=1,nsemi2=1,nspace2=0,n1,n2; getline(cin,a); getline(cin,op); getline(cin,b); n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==';') nsemi1++; } n2=b.length(); for (int i=0;i<n2;i++) {if (b[i]==';') nsemi2++; } n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==' ') {nspace1++;} } nspace1=(nspace1/nsemi1)+1; for (int i=0;i<n2;i++) {if (b[i]==' ') {nspace2++;} } nspace2=(nspace2/nsemi2)+1; for (int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++){x[i][j]=atof(a.c_str());}} for (int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++){cout<<x[i][j]<<" ";}} system ("PAUSE"); }Last edited by zmbd; Apr 25 '15, 09:21 PM. Reason: [z{please format your code using the [CODE/] button in the toolbar}{You also need to explain your post.}]Comment
-
now i delivered here but i don,t have idea how to make Inverse or power is not there any idea?
Code:#include <iostream> #include <string> #include <cstdlib> #include <sstream> #include<string> #include<math.h> using namespace std; int main () { string a,b,op,c,d,e,f=" ",r,p; float x[100][100],y[100][100],z[100][100]; float nspace1=0,nsemi1=1,nsemi2=1,nspace2=0,n1,n2,k,v; getline(cin,a); if(v==(int)v) {getline(cin,op); } else {cout<<"ERROR"; } if(op=="+"||op=="-"||op=="*"||op=="^"||op=="T"||op=="D"||op=="I"||op=="/") { getline(cin,b);} else {cout<<"ERROR"; } n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==';') nsemi1++; } n2=b.length(); for (int i=0;i<n2;i++) {if (b[i]==';') nsemi2++; } n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==' ') {nspace1++;} } nspace1=(nspace1/nsemi1)+1; v=nspace1; for (int i=0;i<n2;i++) {if (b[i]==' ') {nspace2++;} } nspace2=(nspace2/nsemi2)+1; c=a.substr(1,n1-2); d=b.substr(1,n2-2); c=c+";"; d=d+";"; /*Removing rubbish from string1*/ for(int i=0;i<nsemi1;i++) { e=c.insert(c.find(';')," "); e=c.erase(c.find(';'),1); } /*Removing rubbish in string 2*/ for(int i=0;i<nsemi2;i++) { f=d.insert(d.find(';')," "); f=d.erase(d.find(';'),1); } /*filling array1 from str1*/ for(int i=0;i<nsemi1;i++) {for (int j=0;j<nspace1;j++) {r=e.substr(0,e.find(' ')); x[i][j]=atof(r.c_str()); e=e.erase(0,e.find(' ')+1); } } /*filling array2 from str2*/ for(int i=0;i<nsemi2;i++) {for (int j=0;j<nspace2;j++) {p=f.substr(0,f.find(' ')); y[i][j]=atof(p.c_str()); f=f.erase(0,f.find(' ')+1); } } /*addition*/ if(op=="+") {if(nsemi1==nsemi2&&nspace1==nspace2) { cout<<"["; for(int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++) {z[i][j]=x[i][j]+y[i][j]; cout<<z[i][j]<<" "; }cout<<";" ; } cout<<"]";} else {cout<<"ERROR";} } /*substraction*/ if(op=="-") { if(nsemi1==nsemi2&&nspace1==nspace2) {cout<<"["; for(int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++) {z[i][j]=x[i][j]-y[i][j]; cout<<z[i][j]<<" "; } cout<<";" ; } cout<<"]"; } else {cout<<"ERROR";} } /* multiplying */ if(op=="*") {if(nspace1==nsemi2) { cout<<"["; for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace2;j++) { z[i][j]=0; for(int q=0;q<nsemi2;q++) { z[i][j]=z[i][j]+x[i][q]*y[q][j]; } } } for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace2;j++) { cout<<z[i][j]<<" "; } cout<<";" ; } cout<<"]"; } else{cout<<"ERROR"; } } if(op=="T") {/* Transpose*/ for(int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++) {z[i][j]=x[i][j]; cout<<z[j][i]<<" "; } cout<<";" ; } cout<<"]"; } }Comment
-
You have already worked out multiplication. But it is not a function you can call.
Make a function out of it:
So I pass in the addresses of two matrices (a and b) and get the product matrix (c).Code:int Multiply(float* a, float* b, float*c);
Then call this function in your to-be-written power function:
Inside this function you run a loop from 0 to <exp and inside the loop you call the Multiply function. The product matrix (c) replaces one of the other matrices before the loop cycles and the Multiply function is called again.Code:int Power(float* a, float* b, float*c, int exp);
Inverse power is just 1/matrix where matrix is the power result from the above function. You have already worked out division so you might make a function out of it also so you can use it here.Comment
-
I can't do that because if I did you still would not know how to do this. The basic blocks in C++ are functions and unless you learn how to write functions, you won't be able to write programs.
Read this: http://bytes.com/topic/c/insights/77...rrays-revealed
This article shows how to use arrays friom inside a function.Comment
-
I make the inverse and power but there are logical error that i can,t find please can u find it
Code:#include <iostream> #include <string> #include <cstdlib> #include <sstream> #include<string> #include<math.h> using namespace std; int main () { string a,b,op,c,d,e,f=" ",r,p; float x[100][100],y[100][100],z[100][100]; float nspace1=0,nsemi1=1,nsemi2=1,nspace2=0,n1,n2,v; int pow,nn,k; getline(cin,a); if(v==(int)v) {getline(cin,op); } else {cout<<"ERROR"; } if(op=="+"||op=="-"||op=="*"||op=="/") { getline(cin,b); n2=b.length(); for (int i=0;i<n2;i++) {if (b[i]==';') nsemi2++; } for (int i=0;i<n2;i++) {if (b[i]==' ') {nspace2++;} } nspace2=(nspace2/nsemi2)+1; d=b.substr(1,n2-2); d=d+";"; /*Removing rubbish in string 2*/ for(int i=0;i<nsemi2;i++) { f=d.insert(d.find(';')," "); f=d.erase(d.find(';'),1); } /*filling array2 from str2*/ for(int i=0;i<nsemi2;i++) {for (int j=0;j<nspace2;j++) {p=f.substr(0,f.find(' ')); y[i][j]=atof(p.c_str()); f=f.erase(0,f.find(' ')+1); } } } n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==';') nsemi1++; } n1=a.length(); for (int i=0;i<n1;i++) {if (a[i]==' ') {nspace1++;} } nspace1=(nspace1/nsemi1)+1; v=nspace1; c=a.substr(1,n1-2); c=c+";"; /*Removing rubbish from string1*/ for(int i=0;i<nsemi1;i++) { e=c.insert(c.find(';')," "); e=c.erase(c.find(';'),1); } /*filling array1 from str1*/ for(int i=0;i<nsemi1;i++) {for (int j=0;j<nspace1;j++) {r=e.substr(0,e.find(' ')); x[i][j]=atof(r.c_str()); e=e.erase(0,e.find(' ')+1); } } /*addition*/ if(op=="+") {if(nsemi1==nsemi2&&nspace1==nspace2) { cout<<"["; for(int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++) {z[i][j]=x[i][j]+y[i][j]; cout<<z[i][j]<<" "; }cout<<";" ; } cout<<"]";} else {cout<<"ERROR";} } /*substraction*/ if(op=="-") { if(nsemi1==nsemi2&&nspace1==nspace2) {cout<<"["; for(int i=0;i<nsemi1;i++) {for(int j=0;j<nspace1;j++) {z[i][j]=x[i][j]-y[i][j]; cout<<z[i][j]<<" "; } cout<<";" ; } cout<<"]"; } else {cout<<"ERROR";} } /* multiplying */ if(op=="*") {if(nspace1==nsemi2) { cout<<"["; for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace2;j++) { z[i][j]=0; for(int q=0;q<nsemi2;q++) { z[i][j]=z[i][j]+x[i][q]*y[q][j]; } } } for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace2;j++) { cout<<z[i][j]<<" "; } cout<<";" ; } cout<<"]"; } else{cout<<"ERROR"; } } /* Transpose*/ if(op=="T") { if(v==(int)v) { cout<<"["; for(int i=0;i<nspace1;i++) {for(int j=0;j<nsemi1;j++) { cout<<x[j][i]<<" "; } cout<<";" ; } cout<<"]"; } else{cout<<"ERROR"; } } /*Determinent*/ if(op=="D") { if(nsemi1==nspace1) {if(nsemi1==2) {cout<<(x[0][0]*x[1][1]-x[0][1]*x[1][0]);} else if(nsemi1==3) {cout<<(((x[0][0])*((x[2][2]*x[1][1])-(x[2][1]*x[1][2]))) -((x[1][0])*((x[0][1]*x[2][2])-(x[0][2]*x[2][1])))+ ((x[2][0])*((x[0][1]*x[1][2])-(x[0][2]*x[1][1])))); } else { cout<< x[0][0]*((x[1][1]*(x[3][3]*x[2][2]-x[2][3]*x[3][2])) -(x[2][1]*(x[3][3]*x[1][2]-x[1][3]*x[3][2]))+ (x[3][1]*(x[2][3]*x[1][2]-x[1][3]*x[2][2]))) -(x[1][0]*((x[0][1]*(x[2][2]*x[3][3]-x[2][3]*x[3][2])) -(x[2][1]*(x[0][2]*x[3][3]-x[0][3]*x[3][2]))+ (x[3][1]*(x[0][2]*x[2][3]-x[0][3]*x[2][2])))) +(x[2][0]*((x[0][1]*(x[1][2]*x[3][3]-x[1][3]*x[3][2])) -(x[1][1]*(x[0][2]*x[3][3]-x[0][3]*x[3][2]))+ (x[3][1]*(x[0][2]*x[1][3]-x[0][3]*x[1][2])))) -(x[3][0]*((x[0][1]*(x[1][2]*x[2][3]-x[1][3]*x[2][2])) -(x[1][1]*(x[0][2]*x[2][3]-x[0][3]*x[2][2]))+ (x[2][1]*(x[0][2]*x[1][3]-x[0][3]*x[1][2])))); } } else{cout<<"ERROR";} } /*power*/ if(op=="^") {if(nsemi1==nspace1) { cin>>pow; cout<<"["; float mat[100][100]; for(int k=0;k<pow-1;k++) { for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace1;j++) { for(int m=0;m<nsemi1;m++) {mat[i][j]=x[i][j]; z[i][j]=mat[i][j]+mat[i][j]*x[i][j]; } } } for(int l=0;l<nsemi1;l++) { for(int ab=0;ab<nsemi1;ab++) { mat[l][ab]=z[l][ab]; } } } for(int i=0;i<nsemi1;i++) { for(int j=0;j<nspace1;j++) { cout<<z[i][j]<<" "; } cout<<";" ; } cout<<"]"; } else{ cout<<"ERROR"; } } return 0; }Last edited by zmbd; Apr 30 '15, 12:43 PM. Reason: [z{Please remove the extra blank lines from your code before posting}]Comment
-
esslam200
WFC is a very gracious and generous person and is normally a very helpful expert/moderator - please follow the advice the WFC has already given you:
Please run your compiler and troubleshoot the code. WFC and I have both stated that we cannot do your homework - this is a site policy:
Posting Homework or Coursework Questions and Answers
Any and all Homework/Coursework questions will be deleted. If you see a question that is a homework or coursework assignment then please remember DO NOT attempt to post a reply. Replies to this thread will be deleted along with the original post. Please 'report' the thread.Last edited by zmbd; Apr 30 '15, 12:40 PM.Comment
-
thank u all very much and i must mention WFC who let me think how to solve prgramming problemsCode:#include <iostream> #include <sstream> #include<string> #include<math.h> #include <string> #include <cstdlib> using namespace std; float matrix[100][100],size,d; int i,j; float determinant(float matrix[100][100],float size) { float s=1,det=0,m1[100][100]; int i,j,m,n,c; if (size==1) { return (matrix[0][0]); } else { det=0; for (c=0;c<size;c++) { m=0; n=0; for (i=0;i<size;i++) { for (j=0;j<size;j++) { m1[i][j]=0; if (i != 0 && j != c) { m1[m][n]=matrix[i][j]; if (n<(size-2)) n++; else { n=0; m++; } } } } det=det + s * (matrix[0][c] * determinant(m1,size-1)); s=-1 * s; } } return (det); } int main () { string a1,c1,c2,e1,r1,t,b1,v1,v2,e2,r2,l,ope; float x1[100][100],y1[100][100],z1[100][100],k1[100][100],u=0; int i,j,n1,m1,n2,m2,na,nb,sum1=1,sum2=1,a2,a3,a4,a5,b2,b3,b4,b5,q,sum3=1,sum4=1,power; getline(cin,a1); a2=a1.length(); for (i=0;i<a2;i++) {if (a1[i]==';') sum1 = sum1 +1 ; } n1=sum1 ; for (int i=0;i<a2;i++) {if (a1[i]==' ') sum2=sum2+1; if(a1[i]==';') break; } m1=sum2; c2=a1.substr(1,a2-2); c2=c2+";"; t= " "; for ( i =0; i<n1;i++) { e1 = c2.substr(0,(c2.find(';'))); t= t +" "+e1; c2=c2.erase(0,c2.find(';')+1); } a3= t.length(); t=t.substr(1); t=t.substr(1); for( i=0;i<n1;i++) {for ( j=0;j<m1;j++) {a4=t.find(' '); a5=t.find(' '); r1=t.substr(0,a4); x1[i][j]=atof(r1.c_str()); t=t.erase(0,a5+1); } } getline(cin,ope); if(ope=="-"||ope=="+"||ope=="/"||ope=="*") { getline(cin,b1); b2=b1.length(); for (i=0;i<b2;i++) {if (b1[i]==';') sum3 = sum3 +1 ; } n2=sum3 ; for ( i=0;i<b2;i++) {if (b1[i]==' ') sum4=sum4+1; if(b1[i]==';') break; } m2=sum4; v2=b1.substr(1,b2-2); v2=v2+";"; l= " "; for ( i =0; i<n2;i++) { e2 = v2.substr(0,(v2.find(';'))); l= l +" "+e2; v2=v2.erase(0,v2.find(';')+1); } b3= l.length(); l=l.substr(1); l=l.substr(1); for( i=0;i<n2;i++) {for ( j=0;j<m2;j++) {b4=l.find(' '); b5=l.find(' '); r2=l.substr(0,b4); y1[i][j]=atof(r2.c_str()); l=l.erase(0,b5+1); } } if(ope=="+"){ if(n1==n2&&m1==m2) { cout<<"["; for(i=0;i<n1;i++) {for( j=0;j<m1;j++) {z1[i][j]=x1[i][j]+y1[i][j]; if(j!=(m1-1)) cout<<z1[i][j]<<" "; else cout<<z1[i][j]; } if(i!=(n1-1)) cout<<";"; else continue; } cout<<"]"; } else {cout<<"ERROR";} } if(ope=="-") {if(n1==n2&&m1==m2) { cout<<"["; for(i=0;i<n1;i++) {for( j=0;j<m1;j++) {z1[i][j]=x1[i][j]-y1[i][j]; if(j!=(m1-1)) cout<<z1[i][j]<<" "; else cout<<z1[i][j]; } if(i!=(n1-1)) cout<<";"; else continue; } cout<<"]"; } else {cout<<"ERROR";} } if(ope=="*") {if(m1==n2) { cout<<"["; for(int i=0;i<n1;i++) { for(int j=0;j<m2;j++) { z1[i][j]=0; for(int q=0;q<n2;q++) { z1[i][j]=z1[i][j]+x1[i][q]*y1[q][j]; } } } for(int i=0;i<n1;i++) { for(int j=0;j<m2;j++) { if(j!=(m2-1)) cout<<z1[i][j]<<" "; else cout<<z1[i][j]; } if(i!=(n1-1)) cout<<";"; else continue; } cout<<"]"; } else {cout<<"ERROR";} } if(ope=="/"){ if(m1==n2){ float raa[100][100],rbb[100][100]; int p,q,m,n,i,j; for (q=0;q<n1;q++) { for (p=0;p<n2;p++) { m=0; n=0; for (i=0;i<n2;i++) { for (j=0;j<n2;j++) { if (i != q && j != p) { raa[m][n]=y1[i][j]; if (n<(n2-2)) n++; else { n=0; m++; } } } } rbb[q][p]=pow(-1,q + p) * determinant(raa,n2-1); } } float kaa[100][100],d1; for (i=0;i<n2;i++) { for (j=0;j<n2;j++) { kaa[i][j]=rbb[j][i]; } } d=determinant(y1,n2); for (i=0;i<n2;i++) { for (j=0;j<n2;j++) { y1[i][j]=kaa[i][j] / d; } } for(int i=0;i<n1;i++) { for(int j=0;j<m2;j++) { z1[i][j]=0; for(int q=0;q<n2;q++) { z1[i][j]=z1[i][j]+x1[i][q]*y1[q][j]; } } } cout<<"["; for(int i=0;i<n1;i++) { for(int j=0;j<m2;j++) { if(j!=(m2-1)) cout<<z1[i][j]<<" "; else cout<<z1[i][j]; } if(i!=(n1-1)) cout<<";"; else continue; } cout<<"]"; } else cout<<"ERORR"; } } else if(ope=="^"||ope=="T"||ope=="D"||ope=="I") { if(ope=="T"){ for (int i=0;i<a2;i++) {if (a1[i]==' ') {u++;} } u=(u/n1)+1; int s = u; if(u!= s){ cout<<"ERORR";} else { cout<<"["; for( int i=0;i<m1;i++) {for( j=0;j<n1;j++) { cout<<x1[j][i]<<" "; } cout<<";" ; } cout<<"]";}} if(ope=="D"){ if(n1!=m1) cout<<"ERORR"; cout<<determinant(x1,n1); } if(ope=="^"){ if(n1==m1) { cin>>power; cout<<"["; for(i=0;i<n1;i++) { for(j=0;j<m1;j++) { z1[i][j]=x1[i][j]; } } float t =0; for(int m=0;m<power-1;m++) { for( i=0;i<n1;i++) { for( j=0;j<m1;j++) { for(q=0;q<m1;q++) { t=t+z1[i][q]*x1[q][j]; } k1[i][j]=t; t=0; } } for( i=0;i<n1;i++) { for( j=0;j<m1;j++) { z1[i][j]=k1[i][j]; } } } for( i=0;i<n1;i++) { for( j=0;j<m1;j++) { if(j!=(m1-1)) cout<<z1[i][j]<<" "; else cout<<z1[i][j]; } if(i!=(n1-1)) cout<<";"; else continue; } cout<<"]"; } else{cout<<"ERROR"; } } } if(ope=="I"){ if(n1==m1) { cout<<"["; float r11[100][100],r22[100][100]; int p,q,m,n,i,j; for (q=0;q<n1;q++) { for (p=0;p<n1;p++) { m=0; n=0; for (i=0;i<n1;i++) { for (j=0;j<n1;j++) { if (i != q && j != p) { r11[m][n]=x1[i][j]; if (n<(n1-2)) n++; else { n=0; m++; } } } } r22[q][p]=pow(-1,q + p) * determinant(r11,n1-1); } } float k11[100][100],d; for (i=0;i<n1;i++) { for (j=0;j<n1;j++) { k11[i][j]=r22[j][i]; } } d=determinant(x1,n1); for (i=0;i<n1;i++) { for (j=0;j<n1;j++) { z1[i][j]=k11[i][j] / d; } } for (i=0;i<n1;i++) { for (j=0;j<n1;j++) { cout<<z1[i][j]<<" "; } cout<<";"; } cout <<"]"; } else cout<< "ERROR"; } return 0; }Comment
Comment