Need Help with matrices

Hello im an engineering student who is a beginner at c++. for homework we were assigned a basic matrices worksheet. my question is how do i save the letters as variables and not as addresses.

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
  #include <iostream>
#include "T:\StewartM\ENR 110\Matrix.cpp"
#include <cmath>
using namespace std;



int main(){ 
	Matrix A(3,3);
	A.a[0][0] = 1;	A.a[0][1] = 1;	A.a[0][2] = 2;
	A.a[1][0] = 1;	A.a[1][1] = -1;	A.a[1][2] = -3;
	A.a[2][0] = 1;	A.a[2][1] = 2;	A.a[2][2] = -12;
	A.print(); 



	char one = 'x';
	char two = 'y';
	char three = 'z';
	


	Matrix B(3,1);
	B.a[0][0] = one;
	B.a[1][0] = two;
	B.a[2][0] = three;
	B.print();

	Matrix C = mult(A,B);
	C.print();

	
}
Not sure what you mean. You have a bunch of letters... also "address" is a special word in C/C++, did you actually mean another word?

Guessing your custom "Matrix" can only be made with integers and when you're trying to save char they're getting auto-converted to int (expected behavior)? Maybe change or improve Matrix itself to allow you to create matrices of strings, or simply change how print() method works by explicitly casting elements you're outputting by adding (char) in front of the printed integer.

(btw, it's usually bad practice to include .cpp files if there's a standalone header file with an 'include guard' that can be included instead)
I think this works:
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
#include <iostream>
//#include "T:\StewartM\ENR 110\Matrix.cpp"
#include <cmath>
#include <string>
using namespace std;



int main() {
	int m[3][3];
	m[0][0] = 1;	m[0][1] = 1;	m[0][2] = 2; //1x 1y 1z 
	m[1][0] = 1;	m[1][1] = -1;	m[1][2] = -3; //1x -1y -3z
	m[2][0] = 1;	m[2][1] = 2;	m[2][2] = -12; //1x 2y -12z
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << m[i][j] << " ";
		}
		cout << endl;
	}



	char one = 'x';
	char two = 'y';
	char three = 'z';



	char m2[3];
	m2[0] = one;
	m2[1] = two;
	m2[2] = three;
	for (int i = 0; i < 3; i++)
	{
		cout << m2[i] << " ";
	}
	cout << endl;
	string m3[3];
	for (int i = 0; i < 3; i++)
	{
			m3[i] = "";
			for (int j = 0; j < 3; j++)
			{
				m3[i] += " + " + to_string(m[i][j]) + m2[j];
			}
			cout << m3[i] << endl;
	}
}
Topic archived. No new replies allowed.