gauss vector

I gotta implement gauss's elimination method using vectors starting off with this example code I've already started working on. My main question is where would i put the actually loop to do the calculations?

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
 
#include <iostream>
#include<iomanip>
#include<cmath>
#include <vector>
using namespace std;

class gauss
{
private:
    std::vector < std::vector <double> > M;

public:

    void loadit (void);
    void printit (void);

};

void loadit(std::vector <std::vector <int> > &b)
{
	int ncols, nrows;

	std::vector <std::vector<int> > M;
	std::vector <std::vector<int> > MT;
int temp;
	std::cout << "What are the number of columns?" << std::endl;
	std::cin >> ncols;


	std::cout << "What are the number of rows?" << std::endl;
	std::cin >> nrows;


	std::cout << "Type in all of the elements.  Use a space between elements. "
		<< "Use enter between rows." << std::endl;
    b.resize (nrows);

	for (int i = 0; i <= ncols - 1; i++)
	{
	    b[i].resize (ncols);
		for (int j = 0; j <= nrows - 1; j++)
		{

			std::cin >> b[i][j];
			std::cout << i << j << b[i][j];
		}
	}
}


void printit(std::vector < std::vector<int> > &b)
{	int ncols, nrows;
	for (int i = 0; i <= nrows - 1; i++)
	{
		for (int j = 0; j <= ncols - 1; j++)
		{

			std::cout << b[i][j] << " ";
		}
		std::cout << std::endl;
	}
}

int main()
{


    std::vector <std::vector<int> > M;
	std::vector <std::vector<int> > MT;
	loadit(M);

	std::cout << "\n\nHere is your matrix: " << std::endl;
	printit(M);




	return 0;
}
paul314 wrote:
where would i put the actually [sic] loop to do the calculations?

In a separate member function
std::vector<double> gauss::solveit( const std::vector<double> &RHS );

AFTER you have checked that the rest of the code does anything sensible. Like not confusing ints and doubles, and deciding what precisely you are going to do with MT.


If you have the line
using namespace std;
at the top of your code you don't need to prefix standard library items with
std::
as well.

Alternatively, you could put lines like
using std::vector;
at the top of your code. Otherwise it is going to get very hard on the eyes doing any sort of mathematical programming with multi-dimensional arrays.
Last edited on
thanks ill give it a try:)
It does not matter here, if this is the whole scope of the problem, but your design is not stable for going forward very far.

I suggest you instead make a matrix class, think about that that might be, and have elimination as a method in that. That is, gauss should not be the class, its a method. If you decide to do anything more besides elimination, this will start to get weird.

How would i call loadit::gauss in main?





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
#include <iostream>
#include<iomanip>
#include<cmath>
#include <vector>
using namespace std;

class gauss
{
private:
    std::vector < std::vector <double> > M;
    std::vector <double> S;
    int N;

public:
    int n,i,j,k;
    gauss(int);
    void loadit (void);
    void printit (void);
    void eliminate (void);
    void substitute (void);
};

gauss::gauss(int n)
{
    N=n;
    S.resize(N);
    M.resize(N);
    for (int i(0);i<N;i++)
    {
        M[i].resize(N+1);
    }
}


void gauss::loadit(void)
{
	int ncols, nrows;
	ncols=N+1;
	nrows=N;
    int temp;


	std::cout << "Type in all of the elements.  Use a space between elements. "
		<< "Use enter between rows." << std::endl;

	for (int i = 0; i <= ncols - 1; i++)
	{
		for (int j = 0; j <= nrows - 1; j++)
		{

			std::cin >> M[i][j];
			std::cout << i << j << M[i][j];
		}
	}
}


//back substitution
//elimination



void gauss::printit(void)
{	int ncols, nrows;
	for (int i = 0; i <= nrows - 1; i++)
	{
		for (int j = 0; j <= ncols - 1; j++)
		{

			std::cout << M[i][j] << " ";
		}
		std::cout << std::endl;
	}
}

int main()
{
void gauss::loadit(void)

    std::vector <std::vector<double> > M;
	std::vector <std::vector<double> > S;


	std::cout << "\n\nHere is your matrix: " << std::endl;





	return 0;
}
Topic archived. No new replies allowed.