Forming a matrix multiplication program

Hey guys, I just started Linear Algebra for my programming degree and to full understand everything I want to put it into code and make my own "matrix calculator." I have all the theory but I am having issues keeping the matrix variable and moving it around the class' functions to create it and output it. Once I know I have it saved I think I can get the addition/multiplication to work. Thanks! Here is the code I have so far:

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

using namespace std;

class matrix{
    
private:
  
    int q = 1, s = 1, a = 1, b = 1;
    int i=0, j=0;
    int *r, *c, *w, *x;
    int rows[2], columns[2];
    
    int matrix[2][2];

public:
    
    void getInfo1(int matrix[2][2]);
    void getInfo2();
    void output1();
    void output2();
    
};

int main()
{

    matrix m1, m2;


    return 0;
}



void matrix::getInfo1(int matrix[2][2])
{
    cout<<"How many rows is your first matrix?"<<endl;
    cin>>rows[0];
    cout<<"How many columns is your first matrix?"<<endl;
    cin>>columns[0];
    
    //assigns rows and columns to pointers
    
    r = &rows[0];
    c = &columns[0];
    
    //Creates an array of matrices
    
    int matrix1[*r][*c];
    
    //assigns value for the matrix1 definied by rxc
    
    for (i=0; i<rows[0]; i++) {
        
        for (j=0; j<columns[0]; j++) {
            
            cout<<"What is your "<<q<<" "<<s<<" term?"<<endl;
            cin>>matrix1[i][j];
            
            s++;
            
        }//end column (j) for loop
        
        s=1;
        q++;
        
    }//end row (i) for loop
}

/*
void matrix::getInfo2()
{
    cout<<"How many rows is your second matrix?"<<endl;
    cin>>rows[1];
    cout<<"How many columns is your second matrix?"<<endl;
    cin>>columns[1];
    
    w = &rows[1];
    x = &columns[1];
    
    int matrix2[*w][*x];
    
    //assings values for the matrix2 definied by w x
    for (i=0; i<rows[1]; i++) {
        
        for (j=0; j<columns[1]; j++) {
            
            cout<<"What is your "<<a<<" "<<b<<" term?"<<endl;
            cin>>matrix2[i][j];
            
            a++;
            
        }//end column (j) for loop
        
        a=1;
        b++;
        
    }//end row (i) for loop
  
}*/

void matrix::output1()
{
    //outputs the matrix1 of numbers
    for (i=0; i<rows[0]; i++) {
        
        for (j=0; j!=columns[0]; j++) {
            
            cout<<matrix1[i][j];
            cout<<"  ";
            
        }//end column (j) output for loop
        
        cout<<endl;
        
    }//end row (i) output for loop
}

/*
void matrix::output2()
{
    //outputs the matrix2 of numbers
    
    for (i=0; i<rows[1]; i++) {
        
        for (j=0; j!=columns[1]; j++) {
            
            cout<<matrix2[i][j];
            cout<<"  ";
            
        }//end column (j) output for loop
        
        cout<<endl;
        
    }//end row (i) output for loop
}
*/









You can create static array for matrix like:

1
2
3
4
5
6
7
#define MAX 20

class matrix
{
private:
values[MAX][MAX];
};


and when getInfo1 is executed you can show boundaries to user and check his input.

More professional would be creating dynamic array, ether of using pointer to pointer array ( **p) and new or vector class from std library (i wrote example in this topic http://www.cplusplus.com/forum/general/107678/ )
SWEET! But what do you mean by check boundaries? I can define MAX 20 and set my array to that and use it in the functions correct? But then how would I nullify all the other parts if its a 4X4 and not a 20X20? Or do I copy what I from that array into an array of a distinct size? I tried using pointers but I do not know how to call it then because I do not know what the actual size would be. Thank you so much for your help!
this is what i meant:
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
	unsigned row_size;
	unsigned col_size;

	unsigned row;
	unsigned col;

	int matrix[MAX_SIZE][MAX_SIZE];
	
	do
	{
	cout << "enter number of columns: ";
	cin >> col_size;
	} while (col_size > MAX_SIZE);

	do
	{
	cout << "enter number of rows: ";
	cin >> row_size;
	} while (row_size > MAX_SIZE); 
	
	//now that you know matrix is of size [col_size][row_size]
	cout << "enter values:" << endl;

	for (row=0; row < row_size; row++)
		for (col=0; col < col_size; col++)
		{
			cout << "[" << row << "][" << col << "] = ";
			cin >> matrix[row][col];
		}

	cout << endl << "values entered:" << endl;
	for (row=0; row < row_size; row++)
	{
		for (col=0; col < col_size; col++)
		{
			cout << matrix[row][col] << " ";
		}
		cout << endl;
	}

	cout << endl;


but as i said before, this is static memory solution (C-like) and user can't enter matrix size bigger than defined value. If you would like to do this dynamically (user can enter any matrix size he wants):
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
	unsigned row_size;
	unsigned col_size;

	unsigned row;
	unsigned col;

	int **matrix;
	
	do
	{
	cout << "enter number of columns: ";
	cin >> col_size;
	} while (col_size <= 0);

	do
	{
	cout << "enter number of rows: ";
	cin >> row_size;
	} while (row_size <= 0); 
	
	//now that you know matrix is of size [col_size][row_size]
	// create it:
	matrix = new int*[col_size]; // create columns

	for (col=0; col < col_size; col++) //create rows
		matrix[col] = new int[row_size];

	cout << "enter values:" << endl;

	for (row=0; row < row_size; row++)
		for (col=0; col < col_size; col++)
		{
			cout << "[" << row << "][" << col << "] = ";
			cin >> matrix[col][row];
		}

	cout << endl << "values entered:" << endl;
	
	for (row=0; row < row_size; row++)
	{
		for (col=0; col < col_size; col++)
		{
			cout << matrix[col][row] << " ";
		}
		cout << endl;
	}

	cout << endl;


	for (col=0; col < col_size; col++)		
	{
		delete [] matrix[col]; //delete rows
	}
	delete [] matrix; //delete cols 


or you can use http://www.cplusplus.com/forum/general/107678/ ;)
Last edited on
@tath: Normal delete does not match the array-form of new; use array delete.

You do use column-major matrix. C/C++ style is to use row-major. That is not an error, but has to be documented.

Furthermore, you could allocate a single block for data rather than col_size separate blocks.

PS. Prefer a const to macro.
Last edited on
@keskiverto thanks for your input - i always enjoy getting more info.

You do use column-major matrix
i wanted 2nd example to be similar to 1st one, to clearly see the difference.

use array delete
overlooked. Edited.
Topic archived. No new replies allowed.