Solving Simutaneous Equations with Matrices

Pages: 12
Hey everyone, I'm having a bit of a problem with the addition code for my program. Sorry to say this but I ONLY need help with the ADDITION code, as I posted this question around on other forums and they complained and complained about my code, when i know it works perfectly fine.

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
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;
int askselectioninput();
int main()

{
	cout<<"============================="<<endl;
	cout<<"|| Calculation of Matrices ||"<<endl;
	cout<<"============================="<<endl;
int numbers[2][2];
int row, col;
int option = 0;


for (row=1; !(row>2); row++)

{ cout<<"Data for row: "<<row<<endl;

for (col=1; !(col>2); col++)

{ cout<<"Type a number: ";

cin>>numbers[row][col];
}

cout<<"Matrices Inserted.."<<endl;
}
while (true){

		option = askselectioninput();

		if (option ==1)

		{
			
			cout<<"==================================="<<endl;
			cout<<"||Addition of Current Matrices Data||"<<endl;
			cout<<"==================================="<<endl;
		for (row=1; !(row>2); row++)
		{

	for (col=1; !(col>2); col++)

		{ cout<<numbers[row][col]<<" ";

		 }

		cout<<" "<<endl;
		 }

	cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;

	}

		else if (option ==2)

		{

			cout<<"======================================"<<endl;
			cout<<"||Subtraction of Current Matrices Data||"<<endl;
			cout<<"======================================"<<endl;

		}

		else if (option ==3)

		{
			cout<<"==================================="<<endl;
			cout<<"||Displaying Current Matrices Data||"<<endl;
			cout<<"==================================="<<endl;
			cout<<"The current results are: "<<endl;
				
	for (row=1; !(row>2); row++)

		{

	for (col=1; !(col>2); col++)

		{ cout<<numbers[row][col]<<" ";

		 }

		cout<<" "<<endl;
		 }

	cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;
	}


		else if (option ==4)

		{

			cout<<"==================================="<<endl;
			cout<<"||Multiplying Current Matrices Data||"<<endl;
			cout<<"==================================="<<endl;
		}

		else if (option ==5){

			cout<<"--Program...TERMINATED--"<<endl;
			cout<<"--I'll BE BACK--"<<endl;
			exit (1);

		}

	}
}

int askselectioninput()
{
	int option= 0;
	cout<<"==============YOUR TRANSACTION CHOICES==================="<<endl;
	cout<<"Press 1 for Addition              Press 2 for Subtraction"<<endl;
	cout<<"press 3 for Display Balance    Press 4 for Mutliplication"<<endl;
	cout<<"===============Press 5 for Quit=========================="<<endl;
	cin>>option;

	if (option >0 && option <6){
		return option;
	}

	else {

		cout<<"--Incorrect Value--"<<endl<<endl;
		return askselectioninput();
	}

}
Last edited on
They were probably complaining because your formatting is horrendous.

Compilers do not care, but when the code is hard-to-read for people, they tend to be less prone to help.
It had nothing to do with formatting but thank you for your input, i suppose.
Too lazy to go through yours but I did one a year or so 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
#include <iostream>
using namespace std;
void displaymatrix (double a[4][5],int M,int N){
     N--;
     int n,m;
     for (m=0;m<=M;m++)
     {
     for (n=0;n<=N;n++)
     cout<<a[m][n]<<"\t";
     cout<<"|\t";
     cout<<a[m][n];
     cout<<endl<<endl<<endl;}
     cout<<endl<<endl<<endl<<endl;
     }

int main()
{
    double a[4][5];
    int M=4-1;
    int N=5-1;
    a[0][0]=3;
    a[0][1]=5;
    a[0][2]=7;
    a[0][3]=9;
    a[0][4]=3;
    a[1][0]=2;
    a[1][1]=3;
    a[1][2]=4;
    a[1][3]=4;
    a[1][4]=1;
    a[2][0]=15;
    a[2][1]=7;
    a[2][2]=3;
    a[2][3]=7;
    a[2][4]=6;
    a[3][0]=2;
    a[3][1]=11;
    a[3][2]=3;
    a[3][3]=12;
    a[3][4]=3;
    displaymatrix (a,M,N);

/*Ordering Matrix:*/

int temporary[N];

for(int n=0;n<=N;n++)
for(int m=0;m<=M;m++)
if (a[m][n]==0)
temporary[n]=a[m][n];
a[m][n]=a[m+1][n];



/*Ordering Matrix*/

    double scalar;/*Scalar "scales" each row by a certain ammount*/
    for (int column=0;column<=2;column++)/*For All Columns*/
    for (int row=column+1;row<=3;row++)/*For All Rows*/
    {
    scalar=a[column][column]/a[row][column];
    for (int n=0;n<=N;n++)/*Multiplying All Columns of Row*/
    a[row][n]*=scalar;
    for (int n=0;n<=N;n++)/*Subtracting All Columns of Row*/
    a[row][n]=a[row][n]-a[column][n];
    }
    displaymatrix (a,M,N);
double z=a[3][3];
cin.get();
}


The crux is the last couple of lines. Note how I included a triple for loop with each for having a comment on what it does. Note that this analytically solves the matrix. Its also fun to do the numerical approach of "Gauss-Seidel" iteration.
Last edited on
 
result = row [1] += col[2]; row[1] += col[3];


I imagine this would give you errors. You haven't declared result for starters (I imagine it should be an int) and both 'row' and 'col' are ints not pointer (or pointers to arrays for that matter) so you can't use [ ] on them.

Last edited on
they complained and complained about my code, when i know it works perfectly fine.


Works fine? It doesn't even compile. Which compiler are you using? It should have pointed out both the problems for you. My compiler agrees exactly with LBEaston and points out both the problems.

I'm using Visual Studios 2010 it compiles, runs and even shows the matrices. I just need to find the addition code.
It does now that you've gone back and edited your post to fix the errors.

1
2
3
4
5
6
7
8
9
// adding two matrices

for (int i=0; i < numberOfRows ; i++)
{
  for (int j=0; j< numberOfColumns; j++)
  {
     outputMatrix[i][j] = inputMatrixOne[i][j] + inputMatrixTwo[i][j];
   }
}



Last edited on
What exactly do you mean by that (the addition code)? what should get added to what?

ie. If I enetered

1,2
3,4

as the matrix what should happen when I select add?

also, did you just edit your code?
Last edited on
I've changed from a 3,4 to a 2,2 as i emailed the tutor she said 2x2 is fine. I also changed from row 1>2 for the foor loop.

If you where to enter:
1,2
3,4

it should add the 1,2
then add the 3,4 to give
e.g. 3x, 7y
In that case, your two matrices are

1
2
1
3


and

1
2
2
4


Not one matrix. Two matrices. Is that what you meant? That you have these two matrices and you want to add them together?
also you're writing (and reading) past the end of the array numbers[2][2] doesn't (shouldn't) exist. You're messing with random memory addresses with no nowing what they might contain.
Good spot, LB.

Yes, when you have a 2D array of size [2][2] the four elements are in location [0][0], [0][1]. [1][0] and [1][1].

If you start your row and column counter at 1 and finish at 2, you'll miss the start and you'll go off the end.
Look it's all getting confusing. I'm just so confused with this program right now I dont know where i start or where i end with this.

I've got to calculate a 2x2 matrix with values such as
r c
1 2
3 4

that are held within row and coloum of the single 2D array.

I'm just confused in how i would access the same array to calculate the values.

e.g. x 1,2 y 3,4.

I need to calculate the whole process to get the whole calculatiob
Last edited on
I'm just confused in how i would access the same array to calculate the values.


Create the 2D array like this

int someArray[2][2];

There are then four element in the array. Put things in them like this:

1
2
3
4
someArray[0][0] = 1;
someArray[0][1] = 2;
someArray[1][0] = 3;
someArray[1][1] = 4;


You read them into another variable like this

1
2
3
4
5
int x;
x = someArray[0][0];
x = someArray[0][1];
x = someArray[1][0];
x = someArray[1][1];


someArray[2][2] does not exist.
someArray[2][1] does not exist.
someArray[1][2] does not exist.
Last edited on
the part where you said to input data into the arrays:

someArray[0][0] = 1;
someArray[0][1] = 2;
someArray[1][0] = 3;
someArray[1][1] = 4;

this has to be user input, does that matter?

1
2
3
4
5
6
7
8
9
cin >>a;
cin >>b;
cin >>c;
cin >>d;

someArray[0][0] = a;
someArray[0][1] = b;
someArray[1][0] = c;
someArray[1][1] = d;

ok that works
So now it's simple as a+b etc?
Last edited on

If you want to start with

1 2
3 4

and finish with

3
7

then suitable code is

1
2
3
int outputArray [2][1];
outputArray [0][0] = someArray[0][0] + someArray[1][0];
outputArray [1][0] = someArray[1][0] + someArray[1][1];

Last edited on
Just a question but when you say this:

someArray[0][0] = a;
someArray[0][1] = b;
someArray[1][0] = c;
someArray[1][1] = d;

that's 8 fields in an array?

could it be possible to do this in a loop as well?
Last edited on
Pages: 12