Classes and objects with variables problems

So I'm new to classes and trying to figure it out. I can't seem to get it to work with regards to the objects in main. Can somebody explain or show how it should be?

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 #include <iostream>
#include <math.h>
#include <fstream>
#include <algorithm>
#define Nmax 10

using namespace std;
class MatrixSamling{
	double A[Nmax+1][Nmax],AT[Nmax][Nmax+1],b[Nmax],M[Nmax][Nmax],W[Nmax],TM[Nmax][Nmax+1],x[Nmax],FM[Nmax][Nmax+1];
	int n,m;
public:

	void Indtast(double A[Nmax+1][Nmax],double AT[Nmax][Nmax+1],double b[Nmax], int &n,int &m);
	void MatrixProd(double A[Nmax+1][Nmax],double M[Nmax][Nmax],double AT[Nmax][Nmax+1],int n,int m);
	void MatVekProd(double AT[Nmax][Nmax+1],double b[Nmax],double W[Nmax],int n,int m);
	void DanTotalMatrix(double TM[Nmax][Nmax+1],double FM[Nmax][Nmax+1],double W[Nmax],double M[Nmax][Nmax],int m);
};
class Loesning{
	double A[Nmax+1][Nmax],AT[Nmax][Nmax+1],b[Nmax],M[Nmax][Nmax],W[Nmax],TM[Nmax][Nmax+1],x[Nmax],FM[Nmax][Nmax+1];
	int n,m;
public:
	void Gauss(double TM[Nmax][Nmax+1],int m);
	void Backwardsubstitution(double TM[Nmax][Nmax+1],double x[Nmax],int m);
	void UdskrivVektor(double A[Nmax+1][Nmax],double v[Nmax],int m,int n);

};


int main(){

	double A[Nmax+1][Nmax],AT[Nmax][Nmax+1],b[Nmax],M[Nmax][Nmax],W[Nmax],TM[Nmax][Nmax+1],x[Nmax],FM[Nmax][Nmax+1];
	int n,m;

	MatrixSamling one,two,three,four;
	one.Indtast(A,AT,b,n,m);
	two.MatrixProd(A,M,AT,n,m);
	three.MatVekProd(AT,b,W,n,m);
	four.DanTotalMatrix(TM,FM,W,M,m);
	Loesning five,six,seven;
	five.Gauss(TM,m);
	six.Backwardsubstitution(TM,x,m);
	cout<<"Løsning: ";
	seven.UdskrivVektor(A,x,m,n);


	return 0;
}

void Indtast(double A[Nmax+1][Nmax],double AT[Nmax][Nmax+1],double b[Nmax], int &n,int &m){
	char svar;
	int p=0;

	cout<<"Vil du indtaste en matrice manuelt eller hente den fra filen? Tryk m for manuelt eller f for filen: "<<endl;
	cin>>svar;

	if(svar=='m'){
    cout<<"Indtast antallet rækker og søjler, hvor n>m"<<endl;
    cin>>n;
    cin>>m;

	for(int i=0;i<n;++i){

		for(int k=0;k<m;++k){
			cout<<"Indtast koordinaten i række "<<i+1<<" søjle "<<k+1<<endl;
			cin>>A[i][k];
		}

		cout<<"Indtast en vektor b: ";

		for(int k=0;k<n;++k){
		cout<<"Indtast koordinat "<<k+1<<endl;
		cin>>b[k];
				}
	}

	}
	else if(svar=='f'){
		ifstream Fil;
		Fil.open("Uge15.txt");
		Fil>>n;
		Fil>>m;
		for(int i=0;i<n;++i){
			for(int k=0;k<m+1;++k)
			if(k<m)
			Fil>>A[i][k];
			else if(k==m){
			Fil>>b[p];
			++p;
			}
		}
		Fil.close();
	}

	for(int i=0;i<n;++i){

		for(int k=0;k<m;++k)
		AT[k][i]=A[i][k];

	}

	}

void MatrixProd(double A[Nmax+1][Nmax],double M[Nmax][Nmax],double AT[Nmax][Nmax+1],int n,int m){

	int i,j,k;

	for(j=0;j<m;++j){
	for(i=0;i<m;++i){
	M[i][j]=0;
    for(k=0;k<n;++k)
    M[i][j]+=AT[i][k]*A[k][j];
	}
	}

	}

void MatVekProd(double AT[Nmax][Nmax+1],double b[Nmax],double W[Nmax],int n,int m){
    for(int j=0;j<m;++j)
    W[j]=0;
	for(int i=0;i<m;++i){
		for(int k=0;k<n;++k){
			W[i]+=AT[i][k]*b[k];
		}
	}
	}
void DanTotalMatrix(double TM[Nmax][Nmax+1],double FM[Nmax][Nmax+1],double W[Nmax],double M[Nmax][Nmax],int m){

	for(int i=0;i<m;i++){

			for(int j=0;j<m+1;j++){
				TM[i][j]=M[i][j];
			    TM[i][m]=W[i];
			    FM[i][j]=M[i][j];
			    FM[i][m]=W[i];
			}
		}
}

void Gauss(double TM[Nmax][Nmax+1],int m){
int i,j,k;
double factor;



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

for(int i=j+1; i<m;i++){
             if(TM[j][j]>TM[i][j]){
            	              }
             else{
            	 for(int k=0;k<m+1;k++){

            	 swap(TM[j][k],TM[i][k]);

            	}

             }
			}
	}
}

for(j=0;j<=m-2;j++)
{
	for(i=j+1;i<=m-1;i++)
{
factor = -TM[i][j]/TM[j][j];
TM[i][j]=0;
for(k=j+1;k<=m;k++) TM[i][k]=TM[i][k]+factor*TM[j][k];
	}
}

}

void Backwardsubstitution(double TM[Nmax][Nmax+1],double x[Nmax],int m){
	double sum;
	x[m-1]=TM[m-1][m]/TM[m-1][m-1];
	for (int i=m-2;i>=0;i--){
		sum=0;
		for (int j=i+1;j<m;j++) sum+=TM[i][j]*x[j];
		x[i]=(TM[i][m]-sum)/TM[i][i];
	}
}

void UdskrivVektor(double A[Nmax+1][Nmax],double v[Nmax],int m,int n){

	double sum;

	cout<<"[";
	for(int i =0;i<m;++i)cout<<v[i]<<" ";
	cout<<"]"<<endl;

	cout<<"Kontrol Ax=b=[ ";

	for(int j=0;j<n;j++){
		sum=0;
	for(int i=0;i<m;i++){
		sum+=A[j][i]*v[i];
	}
	cout<<sum<<" ";
	}
cout<<"]";
}
Last edited on
Hi,


You need to qualify your function definition names with the class name and scope operator:

void MatrixSamling::Indtast(double A[Nmax+1][Nmax],double AT[Nmax][Nmax+1],double b[Nmax], int &n,int &m){

The variables on line 31 and 32 are not initialised. This is a golden rule in coding - always initialise. These variables have the same names as the class member variables, so you shouldn't have to send them as arguments to the functions. That is, given the intention was to make the functions member functions.

Are variable n and m to do with the size of the arrays? You already have Nmax.

Your classes do not have constructors, so you are relying on the default implicit one.

Instead of a #define for Nmax, make it a const unsigned or std::size_t type variable.

Why are the arrays 1 larger than Nmax?

When using files, check that opening the file actually worked. Include the filestream as part of a condition when writing to it.


From a style point of view:

Don't declare more than one variable per line. This is difficult to read:

double A[Nmax+1][Nmax],AT[Nmax][Nmax+1],b[Nmax],M[Nmax][Nmax],W[Nmax],TM[Nmax][Nmax+1],x[Nmax],FM[Nmax][Nmax+1];

If you put one variable per line, you can comment each one. There can be other problems to do with types when more than one variable is declared in a statement.

Use const wherever you can. In member functions where the state of the class isn't changed; in function parameters where the value isn't changed in the function.

Don't have using namespace std; Just put std:: instead. Lots written online as to why that is.

Put some white space around your operators - Itsbetterthanrunningeverythingtogetherinonelongexpression.

If a function has lots of arguments, I like to format it with one argument per line:

1
2
3
4
5
void UdskrivVektor(const double A[Nmax+1][Nmax],
                   const double v[Nmax],
                   const int m,
                   const int n ) 
                   {

As implied earlier, you may not need all these parameters.

Always use braces, even when there is one line and when not actually required to, it will save you one day - when someone adds in extra code:

82
83
84
85
86
87
88
89
90
91
92
	for(int i=0;i<n;++i){
			for(int k=0;k<m+1;++k) {
			   if(k<m) {
			      Fil>>A[i][k];
                           }
			   else if(k==m){
			     Fil>>b[p];
			     ++p;
			   }
                        }
		}


Good Luck !!

Edit:

If you format your editor to use 4 spaces instead of tabs, it will display better when you post it here. This site expands tabs to 8 spaces, resulting in too much indenting.
Last edited on
Topic archived. No new replies allowed.