Need help with a code that solves linear equation using elimination method

what help? Doing it for you is not 'help'. Help is where you do something and ask a question, not post a homework assignment with no effort shown.
This situation, where @OP on their first post comes up with rubbish like this, is now becoming commonplace.

All that needs to be done instead of replying to it:

Just press the report button and answer 'spam' or something and that's the end of it, gone forever, and nobody else's time is wasted by replies trying to reason with dickheads.

Actually for my first post (I am newish to C++), I had a try at this.
It is more or less high school stuff, but my high school was 60 years ago.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128


#include<iostream>  
#include<vector> 
#include<cmath> 
#include <iomanip>

using namespace std;  

inline void pivot(vector<vector<double>> &b,vector<double>&r,int num,int n)
{
	int p1,p2,g;
	for (p1 = num;p1<=n-1;p1++) 
		{
				for (p2 = p1+1;p2<=n;p2++)
			{
				if (  abs(b[p1][num]) < abs(b[p2][num])  )
			    {
			swap(r[p1],r[p2]);
			for(g=0;g<=n;g++)
			{
				swap (b[p1][g],b[p2][g]);
			}
	    	}	
	    }
	}	
}//pivot

void gauss(vector<vector<double>> b,vector<double> r, vector<double> &ans)
{
	int n=b.size(),p1,p2,num,k,row,g,z,j;
	double f;
	ans.resize(n);
	n=n-1;
for(k=0;k<=n-1;k++)
{
	pivot(b,r,k,n);
	for(row=k;row<=n-1;row++)
	{
		 if (b[row+1][k]==0) {break;};
		  f=b[k][k]/b[row+1][k];
		  r[row+1]=r[row+1]*f-r[k];
		 for(g=0;g<=n;g++)
		 {
		 	 b[row+1][g]=b[row+1][g]*f-b[k][g];
	     }//g
	}//row	
}//k

//back substitute
for(z=n;z>=0;z--)
{
	 ans[z]=r[z]/b[z][z];
	 for(j=n;j>=z+1;j--)
	 {
	 	 ans[z]=ans[z]-(b[z][j]*ans[j]/b[z][z]);
	 }//j
}//z
}//gauss


void matmultvect(vector<vector<double>> m1,vector<double> m2, vector<double> &ans)
{
	int rows=m1.size(),r,k;
	double rxc;
	ans.resize(rows);
	rows=rows-1;
	for(r=0;r<=rows;r++)
	{
		rxc=0;
		for(k=0;k<=rows;k++)
		{
			rxc=rxc+m1[r][k]*m2[k];
		}//k
	ans[r]=rxc;	
	}//r	
}//matmult


void print(vector<double> v)
{
	int n=v.size(),i;
	string sp;
	for(i=0;i<n;i++)
	{
		if(v[i]>=0) sp="+"; else sp="";
		cout<<setprecision(15)<<sp<<v[i]<<endl;
	}	
}//print


int main()
{
	
	int n;
vector<vector<double>> d
{
        {1, 2, -3,9.8},
        {-4, 5, 6,2},
        {7, 8, 9,8},
        {2, 0, -3,8}
    };
    
 vector<double> r
 {
 	{4,5,6,7}
};

vector<double> ret;

gauss(d,r,ret);

cout<<"solution:"<<endl;
print(ret);

cout <<"  "<<endl;
// multiply back to check
vector<double>ret2;
matmultvect(d,ret,ret2);
cout<<"check, should be 4,5,6,7:"<<endl;
print(ret2);

cout <<"  "<<endl;
cout <<"Press return to end . . ."<<endl; 
cin.get();	  
	
}
 



(dev c++)
Last edited on
What can you not get to print correctly?
cout << will accept a double just fine.
e.g.
1
2
3
4
5
vector<double> numbers {4.2, 5.11, 89.5};
cout << numbers[0] << '\n';

double blah  = 42;
cout << blah << '\n'


What is the specific problem you're having?

If I had to guess, I'd say the issue is your conversions between 1d vectors and vectors of vectors (2d vectors). In your gauss function, you resize ans to be n, but n is b.size().
b.size() is only the size of the outer vector, not the total number of elements.
To get the total number of elements (assuming a rectangular layout, and not a jagged layout), you'd have to do something like:
int n = 2d_vector.size() * 2d_vector[0].size();
Last edited on

I'll check these sizes, but I am used to 1 based arrays, these better represent a matrix.
I sized to n, but then n becomes n-1, so that should be OK.
Using zero based arrays for me is a nuisance, but I am getting used to it.
Is there a way to check out of bounds?
My question was to get cout to print a double in it's entirity
viz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>  
#include<cmath> 

double pi=4*atan(1);

int main()
{
	std::cout<<"cout    "<<pi<<std::endl;
	printf("printf  %.15f\n",pi);
	std::cout <<"Press return to end . . ."<<std::endl;

std::cin.get();	 
}
 

(using gcc via dev C++)
Oh, you mean show more digits (precision) with cout. Unfortunately C++ is quite verbose when it comes to formatting with standard streams. You could just stick with printf, or do:
1
2
3
4
5
6
7
#include <iostream>
#include <iomanip>
int main()
{
     double pi = 3.1415926535897932;
     std::cout << std::setprecision(15) << pi << '\n';
}


Is there a way to check out of bounds?
You could use my_vector.at(index) instead of my_vector[index].
The at function does bounds checking and throws an exception when out of bounds access happens, instead of undefined behavior.


Eventually, I think you'll find it's a lot more clear to do:
1
2
3
4
5
int rows = m1.size();
for (int r = 0; r < rows; r++)
{
    // executes 'rows' times
}


instead of:
1
2
3
4
5
6
int rows = m1.size();
rows = rows-1;
for (int r = 0; r <= rows; r++)
{
    // executes 'rows + 1' times (but rows was decremented)
}
Last edited on
if it really bothers you, you can allocate one more and ignore the 0th location.
eg
int x[11]; //1-10 is valid ... ok?
.at is much slower than [] because of the extra work. Its crippling in linear algebra for larger problems. You can use it to debug and swap it out, but that is a bit of work.

one-d makes a number of things easier.
even a simple 2 matrix addition becomes 1 for loop instead of 2, and the more complex algorithms can do some funky stuff, like a transpose in place instead of having to copy.
Last edited on

Thanks for the tips Ganado/jonnin.
I have implemented some of them.
I put an extra dimension on also.
I'll stick with the zero based array just now, I am getting used to it.
I cannot see the original posters name ("Need help with a code that solves linear equation using elimination method")
But I hope it helps him/her.





Ha, I assumed you were OP.
Topic archived. No new replies allowed.