I don't know how to multiply the matrix with the array!!

This is my program, it has a 1D array and a 2D array (matrix)

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
#include <iostream>
using namespace std;
class vector
{
private:
	int size;
	int *p;
public:
	vector(int a)
	{	
		size = a;
		p = new int[a];
	}
	void input()
	{
		for (int i = 0; i < size; i++)
		{
			int x;
			cout << "Enter values" << endl;
			cin >> x;
			p[i]= x;
		}
	}
		void output()
		{
			for (int i = 0; i < size; i++)
			{
				cout << p[i]  <<" ";
			}
			cout << endl;
		}
		int getelement(int i)
		{
			return p[i];
		}
		int getsize()
		{
			return size;
		}

};
class matrix
{
private:
	int row;
	int col;
	int **r;
public:
	matrix(int s, int a)
	{
		row = s;
		col = a;
		r = new int*[s];
		for (int i = 0; i < s; i++)
		{
			r[i] = new int[a];
		}
	}
	void input()
	{
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				int x;
				cout << "Enter values for matrix" << endl;
				cin >> x;
				r[i][j] = x;
			}
		}
	}
	void output()
	{
		
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				cout << r[i][j] <<" ";
			}
			cout << endl;
		}
	}
	int getelement(int i, int j)
	{
		return r[i][j];
	}

};
void main()
{
	vector c(10);
	c.input();
	c.output();
	cout << "Your element is:" << c.getelement(5) << endl;
	matrix b(2, 3);
	b.input();
	b.output();
	cout << "Your element is:" << b.getelement(1, 0) << endl;
	
}


I'm supposed to multiply the matrix with the array, but I have no idea how?!?!?!
Anyone please help??
Last edited on
Last edited on
But this didn't exactly help me finishing my program :/
Come on! Nobody is perfect...:D

EDIT:
Your first post is unreadable...Please read this:

http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
OK, now your program is readable.

Line 90: main is always of integer type so...int main()

Now here's the code bona fide:
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
#include <iostream>

using namespace std;

class vector
{
private:
	int size;
	int *p;
public:
	vector(int a)
	{	
		size = a;
		p = new int[a];
	}
	void input()
	{
		cout << "Enter vector elements\n";
		for (int i = 0; i < size; i++)
		{
			cout << i + 1 << "  ";
			cin >> p[i];
		}
	}
	void output()
		{
			for (int i = 0; i < size; i++)
			{
				cout << p[i]  <<" ";
			}
			cout << endl;
		}
		
	int e(int i) { return p[i]; }
		
	int getsize()
		{
			return size;
		}

};
class matrix
{
private:
	int row;
	int col;
	int **r;
public:
	matrix(int s, int a)
	{
		row = s;
		col = a;
		r = new int*[s];
		for (int i = 0; i < s; i++)
		{
			r[i] = new int[a];
		}
	}
	void input()
	{
		cout << "Enter matrix elements\n";
		for (int i = 0; i < row; i++)
		{
			cout << "Row " << i + 1 << '\n';
			for (int j = 0; j < col; j++)
			{
				if(j == 0)
					cout << "     Col " << j + 1 << "  ";
				else
					cout << "         " << j + 1 << "  ";
				cin >> r[i][j];
			}
		}
	}
	void output()
	{
		
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				cout << r[i][j] <<" ";
			}
			cout << endl;
		}
	}
	
	int e(int i, int j) { return r[i][j];}
};
int main()
{
	vector c(3);
	c.input();
//	c.output();
	matrix b(2, 3);
	b.input();
//	b.output();
	
	cout << "\nThe product of matrix b(2, 3) by vector c(3)\n\n";
	int d[2];
	for(int i = 0; i < 2; ++i) 
	{
		d[i] = 0;
		for(int j = 0; j < 3; ++j)
			d[i] += b.e(i, j) * c.e(j);
		cout << "          " << d[i] << '\n';
	}
}
Enter vector elements
1  1
2  2
3  3
Enter matrix elements
Row 1
     Col 1  1
         2  2
         3  3
Row 2
     Col 1  4
         2  5
         3  6

The product of matrix b(2, 3) by vector c(3)

          14
          32


Thanks, but I was trying to use a friend class

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
#include <iostream>
using namespace std;
class vector
{
private:
	int size;
	int *p;
	friend class multiply;
public:
	vector(int a)
	{
		size = a;
		p = new int[a];
	}
	int getsize()
	{
		return size;
	}
	void input()
	{
		for (int i = 0; i < size; i++)
		{
			int x;
			cout << "Enter values" << endl;
			cin >> x;
			p[i] = x;
		}
	}
	void output()
	{
		for (int i = 0; i < size; i++)
		{
			cout << p[i] << " ";
		}
		cout << endl;
	}
	int getelement(int i)
	{
		return p[i];
	}
};
class matrix
{
private:
	int row;
	int col;
	int **r;
	friend class multiply;
public:
	matrix(int s, int a)
	{
		row = s;
		col = a;
		r = new int*[s];
		for (int i = 0; i < s; i++)
		{
			r[i] = new int[a];
		}
	}
	int getrow()
	{
		return row;
	}
	int getcol()
	{
		return col;
	}
	void input()
	{
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				int x;
				cout << "Enter values for matrix" << endl;
				cin >> x;
				r[i][j] = x;
			}
		}
	}
	void output()
	{

		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				cout << r[i][j] << " ";
			}
			cout << endl;
		}
	}
	int getelement(int i, int j)
	{
		return r[i][j];
	}
};
class multiply
{
private:
	int r;
	int c;
	int **a;
public:
	multiply(int s, int a)
	{
		r = s;
		c = a;
		a = new int*[s];
		for (int i = 0; i < s; i++)
		{
			a[i] = new int[a];
		}
	}
	void input()
	{
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				a[i][j] = 0;
			}
		}
	}
	for (int i = 0; i < matrix.r; i++)
	{
		if (matrix.row != vector.p)
		{
			cout << "Can't be multiplied" << endl;
		}
		else
		{
			for (int j = 0; j < matrix.col; j++)
			{
				for (int k = 0; k < matrix.row; k++)
				{
					a[i][k] += matrix.r[i][j] * vector.p[k];
				}
			}
		}
	}
};
void main()
{
	int s, r;
	cout << "Enter size" << endl;
	cin >> s;
	vector c(s);
	c.input();
	c.output();
	cout << "Your element is:" << c.getelement(5) << endl;
	cout << "Enter number of rows" << endl << ;
	cin >> r;
	matrix b(r, 1);
	b.input();
	b.output();
	cout << "Your element is:" << b.getelement(1, 0) << endl;

}


Did I use friend class correctly?? If not, how do I write it using friend class?And I also didn't know how to call it in the main function :/
Please help me! Much appreciated :)
If you want to multiply outside the main() function you can do this directly in the class matrix. It's simpler. See the code in bold.

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

using namespace std;

class vector
{
private:
	int size;
	int *p;
public:
	vector(int a)
	{	
		size = a;
		p = new int[a];
	}
	void input()
	{
		cout << "Enter vector elements\n";
		for (int i = 0; i < size; i++)
		{
			cout << "Row " << i + 1 << "    ";
			cin >> p[i];
		}
	}
	void output()
		{
			for (int i = 0; i < size; i++)
				cout << '\t' << p[i]  << '\n';
			cout << endl;
		}
		
	int e(int i) { return p[i]; }
		
	int getsize()
		{
			return size;
		}

};
class matrix
{
private:
	int row;
	int col;
	int **r;
public:
	matrix(int s, int a)
	{
		row = s;
		col = a;
		r = new int*[s];
		for (int i = 0; i < s; i++)
			r[i] = new int[a];
	}
	void input()
	{
		cout << "Enter matrix elements\n";
		for (int i = 0; i < row; i++)
		{
			cout << "Row " << i + 1 << '\n';
			for (int j = 0; j < col; j++)
			{
				if(j == 0)
					cout << "     Col " << j + 1 << "  ";
				else
					cout << "         " << j + 1 << "  ";
				cin >> r[i][j];
			}
		}
	}
	void output()
	{
		cout << '\n';
		for (int i = 0; i < row; i++)
		{
			cout << '\t';
			for (int j = 0; j < col; j++)
				cout << r[i][j] <<" ";
			cout << endl;
		}
		cout << '\n';
	}
	void multiply(int rows, int cols, vector v)
	{
		cout << "The product of matrix m(" << rows << ", " << 
		cols << ") by vector v(" << cols << ")\n\n";
		int *d = new int[cols];
		for(int i = 0; i < rows; ++i) 
		{
			d[i] = 0;
			for(int j = 0; j < cols; ++j)
				d[i] += r[i][j] * v.e(j);
			cout << '\t' << d[i] << '\n';
		}
	}
};

int main()
{
	int rows, cols;
	cout << "How  many  rows  has matrix: ";
	cin >> rows;
	cout << "How many columns has matrix: ";
	cin >> cols;
	
	matrix m(rows, cols);
	m.input();
	
	vector v(cols);
	v.input();
	
	m.output();
	v.output();
	
	m.multiply(rows, cols, v);
}
How  many  rows  has matrix: 3
How many columns has matrix: 3
Enter matrix elements
Row 1
     Col 1  3
         2  5
         3  7
Row 2
     Col 1  1
         2  5
         3  9
Row 3
     Col 1  1
         2  2
         3  3
Enter vector elements
Row 1    9
Row 2    7
Row 3    6

	3 5 7 
	1 5 9 
	1 2 3 

	9
	7
	6

The product of matrix m(3, 3) by vector v(3)

	104
	98
	41

Last edited on
But you didn't use any friend class in your code, I am asked to use it and I'm not exactly sure how :/
Now is the funny part: try to include the actual void multiply() function(in bold) into a friend class adding the necessary stuff!
Last edited on
Hoooowwww?? Hahaha I really don't know how :/
Can you pleeeaasseee rewrite it using friend class? Please!
The class "multiply" does not make sense, really; classes should usually be things, not actions. So you could have a result class. But there the result of mulitplying a matrix by a vector is another vector.

You could add a method or operator to your matrix class.

1
2
//matrix::operator*
vector operator* (const vector& v);


In this case, the vector class would need to be a friend of the matrix class (though it would be better to work through accessors...)

Or you could use a global operator* or function

vector operator* (const matrix& m, const vector& v);

This would need to be made a friend of matrix and vector, unless you used e() to access the elements (std::vector defines the method at() for this purpose)

Rather than passing the rows and cols to the multiplication operator or function, it should check the sizes of the inputs to make sure the calculation is valid. You already have getsize() to vector, and you could add getrows() and getcols() to matrix. Or just check the data members directly, if the function is a friend.

To signal an error you could raise an exception, or set a flag to indicate whether a matrix or vector is valid (in this case, the output code would need to be tweaked.)

The maths in condor's "multiply" function is fine, but the function need to check the dimensions and then either populate a new vector with the results, or returns a "bad vector".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	vector multiply(vector v)
	{
		// check rows of vector v are (its size is) the same as the cols
		// of this matrix...
		if(cols != v.size)
		{
			// TODO if not correct, return "bad" vector (or similar)
		}

		vector v2(rows);
		for(int i = 0; i < rows; ++i) 
		{
			v2.p[i] = 0;
			for(int j = 0; j < cols; ++j)
				v2.p[i] += r[i][j] * v.p[j]; // assume vector is a friend
		}

		return v2;
	}


Andy
Last edited on
Topic archived. No new replies allowed.