Matrix Addition Using Operators + and <<

closed account (iEv9vCM9)
Hello! I got an assigment this friday
I have to write a program using Header File with the following data fields and functions.

I'm having trouble with initializing the 2D vector with those dimensions.

The default constructor has to initializes num_of_rows and num_of_columns with 0. We dont need to worry about the initial value of "data" 2D vector
But the following constructor has to load num_of_rows and num_of_columns with the values passed in tp initialize data using those dimensions.
How can I do that?


How can I implement my void get_index()const; void set_val(int, unsigned int, unsigned int); functions.
The get index should take 2 unsigned integers (i: row index, j: column index) as arguments. The function returns the value in the matrix at row index i and column index j.

And the set_val function takes 2 unsigned integers (i: row index, j: column index) and another integer (val: value to store) as arguments. The function stores the value passed in to the matrix at row index i and column index j. The function should not return a value.

And also if you help me with so guidance with the Matrix Addtion operator and ostream I// will appreciate it! I think I'm close to the idea of the assigment.




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

//Matrix.h
#ifndef MATRIX_H
#define MATRIX_H

#include <vector>
#include <ostream>
#include <iostream>

using namespace std;

class Matrix {

public:

	//Default Constructor
	Matrix();

	//Constructor
	Matrix(unsigned int = 0, unsigned int = 0);

	//Getters
	unsigned int get_num_of_rows()const;
	unsigned int get_num_of_columns()const;

	//Setters
	void set_num_of_rows(unsigned int);
	void set_num_of_columns(unsigned int);

	//Class member functions


	void get_index()const;

	void set_val(int, unsigned int, unsigned int);

	//Overloaded Operators

	Matrix operator+ (const Matrix&)const;

	friend ostream& operator << (ostream&, Matrix&);


private:
	//Data fields
	unsigned int num_of_rows, num_of_columns;
	vector<vector<int>> data;




};
#endif



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

//Matrix.cpp File 

#include "Matrix.h"
#include <vector>
#include <ostream>
#include <iostream>

using namespace std;

//Default Constructor
Matrix::Matrix() : num_of_rows(0), num_of_columns(0) {}

//Constructor
Matrix::Matrix(unsigned int numRows, unsigned int numColumns) :
	num_of_rows(numRows), num_of_columns(numColumns) {

	data[num_of_rows][num_of_columns];
}

//Getters
unsigned int Matrix::get_num_of_rows()const { return num_of_rows; }
unsigned int Matrix::get_num_of_columns()const { return num_of_columns; }

//Setters
void Matrix::set_num_of_rows(unsigned int numRows) { num_of_rows = numRows; }

void Matrix::set_num_of_columns(unsigned int numColumns) { num_of_columns = numColumns; }

//Class member functions

//I'm not sure if this is correct
unsigned int Matrix::get_index(unsigned int i, unsigned int j)const {

	for (int i = 0; i < data.size(); i++) {

		for (int j = 0; j < num_of_columns; j++) {
			
			data[i][j];
		}
	}
	return data[i][j];  
}



//This is how I'm implementing this function according to the instructions
void Matrix::set_val(int val, unsigned int i, unsigned int j) {

	for (int i = 0; i < data.size(); i++) {

		for (int j = 0; j < num_of_columns; j++) {

			data[i][j] = val;
		}
	}
	return;
}

Matrix Matrix::operator+ (const Matrix& val)const {
	Matrix temp;
	for (int i = 0; i < num_of_rows; i++) {
		for (int j = 0; j < num_of_columns; j++) {
			temp.data[i][j] = data[i][j] + val.data[i][j];
		}
		temp.num_of_rows = num_of_rows;
		temp.num_of_columns = num_of_columns;

	}
	return temp;
}


ostream& operator << (ostream& out, const Matrix& f) {
	out << f.data << endl; 
//The ouput has to be the number of rows, numbers, endl and the new vector from the matrix addition
// I'm not sure how to put the new vector //(result from the addition) here to the overload << operator
	return out;
}


}


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

//Main.cpp File

#include "Matrix.h"
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

typedef vector<vector<int>> Matrices;
typedef vector<int> rowSize;

int main() {
	ifstream fin("input.txt");

	if (!fin) {
		cout << "File Not Inserted" << endl;
		system("pause");
		return -1;
	}

	ofstream fout("output.txt");

	unsigned int rows, columns;

	int matrixVal_1, matrixVal_2;

	fin >> rows >> columns;

	Matrices Matrix_1(rows, rowSize(columns));

	for (int i = 0; i < Matrix_1.size(); i++) {
		for (int j = 0; j < columns; j++) { 

			fin >> matrixVal_1; 

			Matrix_1[i][j] = matrixVal_1; 
			

		}

	}

	Matrices Matrix_2(rows, rowSize(columns));

	for (int i = 0; i < Matrix_2.size(); i++) { 

		for (int j = 0; j < columns; j++) { 

			fin >> matrixVal_2; 


			Matrix_2[i][j] = matrixVal_2; 
			
		}

	}


	Matrix matx_1(Matrix_1), matx_2(Matrix_2);
	Matrix sumOfMatrix = matx_1 + matx_2;
	fout << rows << " " << columns << endl;

	fout << sumOfMatrix << endl;


	fin.close();
	fout.close();


	system("pause");
	return 0;
}



Example file
input.txt

5 5

16 5 9 4 5
20 8 7 4 2
-35 5 -13 1 2
10 8 66 4 7
-45 5 -15 1 6
-90 5 -16 18 50

10 4 12 45 45
20 8 7 4 26
-35 5 -13 1 45
10 78 66 4 23
-45 56 -15 1 6
-90 34 -16 18 50]
Last edited on
Looks like you accidentally double-posted, please delete your other thread: http://www.cplusplus.com/forum/general/276789/

And please edit this post and add [code] and [/code] tags to format your code.
Last edited on
Topic archived. No new replies allowed.