Sorting string array

Dear all,
I have been working on a project, I am still at the beginning, one of the tasks is to take a number of equations from the user line by line
example :
3x1+4x3+3x2=12
4x2+4x1+2x1=13
and so on, I should rearrange these equations as follow: x1, x2, x3...
the rest of the project is to apply operations on these equations and finally solve them.
I have managed to take the equations and add them to an array now I am trying to rearrange them inside the array, also I need some help in generalizing my work.
Thanks in advance

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include #include #include #include using namespace std; void fill_array(string arr[12][12],string s,int row) { int pos1=0; int pos2=0; int col=0; while(s.find("+",pos1)!=-1) {pos2=s.find("+",pos1); arr[row][col++]=s.substr(pos1,pos2-pos1); pos1=pos2+1; } arr[row][col]=s.substr(pos1,3); pos2=s.find("=",pos1); pos1=pos2+1; col++; arr[row][col]=s.substr(pos1,3);} int main(){ int n,i=0; cin>>n; int row =-1; string arr[12][12]; for(i=0;i>s; row=row+1; fill_array(arr,s,row); } for(int z=0;z

Last edited on
You should edit your post and repaste your code in code tags to preserve indentation: http://www.cplusplus.com/forum/articles/16853/

And I assume your equations were supposed to look like this:
 ``` 3x1+4x3+3x2=12 4x2+4x1+2x1=13 ```

Sorry for the inconvenience as this is my first post.
and actually it is intended to be( x1,x2) it was asked like this in the project I know it is confusing but that what they want.
I think it's better to read the data into an int (or float) vector and forget about the strings. Then the index of the element represents the subscript and the value is the coefficient. (see code below)

How are you expecting to solve the equations?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include #include using namespace std; struct Equation { vector coef; int result; }; void read_eq(string &line, vector &eq) { eq.push_back(Equation()); // Create a position for a new equation istringstream sin(line); char ch, plus; int n, m; sin >> n >> ch >> m; eq.back().coef.resize(m+1); // ensure index position exists eq.back().coef[m] += n; while (sin >> plus >> n >> ch >> m) { eq.back().coef.resize(m+1); // ensure index position exists if (plus == '+') eq.back().coef[m] += n; else eq.back().coef[m] -= n; } eq.back().result = n; } int main() { vector eq; string line; while (getline(cin, line)) read_eq(line, eq); for (size_t i = 0; i < eq.size(); i++) { for (size_t j = 0; j <= eq[i].coef.size(); j++) if (eq[i].coef[j] != 0) cout << j << ": " << eq[i].coef[j] << '\n'; cout << "result: " << eq[i].result << '\n'; } }``````

First of all, thank you very much for your code as it saves a lot of work.
could please help me understand it more, I don't know how to access these equations to apply operations on them like,
adding or subtracting equations when user enter ( add 1 3)
print coefficients of x1 when ( column x1)
finally solving these equations using Cramer rule
How can I end your code to see the result ? I am very sorry for my ignorant.

There is something I forgot to add in the main post I am able to take the number of equations from the user at the beginning

Thanks in advance
Last edited on
I'm not sure how much I can explain to you. The subscripts have become the coef array's indices. So to add one to the first equation's subscript 2 coefficient you would say:
 `` `` ``++eq[0].coef[2];``

Maybe you can understand an array-based version better. This version has a maximum number of equations and coefficients-per-equation, though (both set to 20 below).
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include using namespace std; const int MAX_EQS = 20; const int MAX_COEFS = 20; struct Equation { int coef[MAX_COEFS]; int result; }; void read_eq(string &line, Equation *eq, int n) { for (int i = 0; i < MAX_COEFS; i++) eq[n].coef[i] = 0; istringstream sin(line); char ch, plus; int c, s; sin >> c >> ch >> s; eq[n].coef[s] += c; while (sin >> plus >> c >> ch >> s) { if (plus == '+') eq[n].coef[s] += c; else eq[n].coef[s] -= c; } eq[n].result = c; } void print_eqs(Equation *eq, int n) { for (int i = 0; i < n; i++) { bool doplus = false; for (int j = 0; j < MAX_COEFS; j++) if (eq[i].coef[j] != 0) { if (doplus) cout << '+'; cout << eq[i].coef[j] << 'x' << j; doplus = true; } cout << '=' << eq[i].result << '\n'; } } int main() { Equation eq[MAX_EQS]; int n = 0; string line; while (getline(cin, line)) read_eq(line, eq, n++); print_eqs(eq, n); }``````

I am very Thankful to you, that was very helpful.
tomorrow I will try to add the operations and solve the equations.
If I need help can I replay here in this post ?
 If I need help can I reply here in this post ?

Yes, that's the best thing to do.
Dear tpb,
I dont know how to ask the user for my operations(see comments)
somthing I couldnt do is to make 1x1 == +x1 and -1x1== -x1 (make the one optional)

Thanks in advance
Last edited on
could you help me with optional one problem and there should be no multiples if user entered 2 of x1 they should be added up and if he entered 2 results they should be added up
ex: 2x1+3x2-x1-12=10 should be 1x1+3x2=22
Thanks in advance

Last edited on
Topic archived. No new replies allowed.