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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include<sstream>
#include <math.h>
#include <stdio.h>

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<n;i++){
	string s;
	cin>>s;
	row=row+1;
fill_array(arr,s,row);
	}
	for(int z=0;z<n;z++){
	for(int x=0; x<12; x++)
{
for(int y=0; y<12; y++)
{
if(arr[z][y].compare(arr[y+1])<0)
{
string hold=arr[z][y];
arr[z][y]=arr[z][y+1];
arr[z][y+1]=hold; 
}
  }

return(0);
}}}

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Equation {
    vector<int> coef;
    int result;
};

void read_eq(string &line, vector<Equation> &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<Equation> 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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <sstream>
#include <string>
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
Registered users can post here. Sign in or register to post.