Matrix and vector multiplication

Hello, so I have an assigment in uni to write program that multiplies matrix by the vector. Also it must be able to add vector or matrixes together. vector.h and vector.cpp was provided as an example and they seem to work. I tried wiriting matrix.h and matrix.cpp according to the example. Any comments would be appreciated. Thanks

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//-------------------------------------------------------------------
/*vector.h*/
/*==========================*/
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED

#include <cstddef>
#include <stdio.h>
#include <cstdlib>

class Matrix;
class Vector
{
public:
   Vector(){n=0;x=0;}
   Vector(int n){allocation(n);}
   Vector(char* file);
   Vector(Vector& A);

   ~Vector();

   void allocation(int ln);
    void out(char* s);
    void naujas(int ln);
    void operator =(const Vector& A);
    double& operator [](int i);

    friend Vector operator+(const Vector& A, const Vector& B);
private:
    int n; 
    double *x;
};
#endif // VECTOR_H_INCLUDED

/*matrix.h*/
/*===========================*/
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED

#include <cstddef>
#include <stdio.h>
#include <cstdlib>

class Matrix
{
public:
   Matrix(){n=0;m=0;y=0;}
   Matrix(int n){allocation(n,m);}
   Matrix(char* file);
   Matrix(Matrix& A);

   ~Matrix();

   void allocation(int ln, int lm);
    void print(char* s);
    void naujas(int ln);  
private:
    int n; //Matrix rows
    int m; // Matrix columns
    double *y;
};
#endif // MATRIX_H_INCLUDED

/*vector.cpp*/
/*=========================*/
#include "vector.h"
#include <assert.h>
#include <cstdlib>

void Vector::allocation(int ln)
{
    n=ln;
    x=new double[n];
    assert(x!=0);
}

Vector::Vector(char* file)
{
    FILE* in;
    int n;
    double dl;

    in = fopen(file, "rt");
    assert(in != 0);

    fscanf(in,"%d\n", &n);
    allocation(n);

    for(int i=0;i<n;i++){
        fscanf(in, "%lf", &dl);
        x[i]=dl;
    }
    fclose(in);
}

Vector::~Vector()
{
    if(x!=0)
        delete []x;
}

void Vector::out(char* s)
{
    printf("%s:\n",s);
    if(x==0){
        printf("Empty\n\n");
        return;
    }
    for(int i=0;i<n;i++){printf("%8.4f",x[i]);}
    printf("\n\n");
}

Vector operator +(const Vector& A, const Vector& B)
{
    if(A.n != B.n){
        printf("Klaida vektoriaus klaseje: blogas indeksas\n");
        exit(1);
    }
    Vector Temp(A.n);
    for(int i=0;i<A.n;i++){Temp.x[i]=A.x[i]+B.x[i];
    }
    return Temp;
}

void Vector::operator =(const Vector& A)
{
    if(n!=A.n) {naujas(A.n);}
    for(int i=0;i<n;i++) {x[i]=A.x[i];}
}

void Vector::naujas(int ln)
{
    if(n==ln){return;}
    if(x!=0){delete []x;}
    allocation(n);
}

Vector::Vector(Vector& A)
{
    allocation(A.n);
    for(int i=0;i<n;i++) {x[i]=A.x[i];}
}

double& Vector::operator [](int i)
{
    if(i<0 || i>=n){
        printf("Klaida\n");
        exit(1);
    }
    return x[i];
}

/*matrix.cpp*/
/*============================*/
#include "matrix.h"
#include <assert.h>
#include <cstdlib>
#include <iostream>
using namespace std;


void Matrix::allocation(int ln, int lm)
{
    n=ln;
    m=lm;

    double *y= (double *)malloc(n*m*sizeof(double));

    assert(y!=0);
}

Matrix::Matrix(char* file)
{
    FILE* in;
    int n,m;
    double dl;

    in = fopen(file, "rt");
    assert(in != 0);

    fscanf(in,"%d", &n);
    fscanf(in,"%d", &m);
    allocation(n,m);

    for(int i=0;i<n; i++){
            printf("\n");
    for(int j=0; j<m; j++){
        fscanf(in, "%lf", &dl);
        *(y+i*m+j)=dl;

    }
}
    fclose(in);
}

Matrix::~Matrix()
{
       for (int i=0;i<n;i++){
        delete []y;}
}

void Matrix::print(char* s)
{
    printf("%s:\n",s);
    if(y==0){
        printf("Empty\n\n");
        return;
    }
    for(int i=0;i<n;i++){ printf("\n");
            for (int j=0;j<m;j++){printf("%lf ", *(y+i*m+j));}}
    printf("\n\n");
}

/*double& Matrix::operator [][](int i, int j)

/*main.cpp*/
/*======================*/
#include <iostream>
#include "vector.h"
#include "matrix.h"

using namespace std;

int main()
{
    Matrix M("m_a.dat");

    // commented this part out, beause without it program works
    /*Matrix N("m_b.dat");
    M.print("M");
    N.print("N");

    Vector A("v_a.dat");
    Vector B("v_b.dat");
    Vector C("v_c.dat");
    Vector D(3);

    A.out("A");
    B.out("B");
    C.out("C");

    D = A+B+C;
    D.out("D=A+B+C");*/
    return 0;
}

Last edited on
> double *y= (double *)malloc(n*m*sizeof(double));
..
> delete []y;
This is wrong.
You can't mix and match your allocators.
malloc pairs with free
new pairs with delete
new[] pairs with delete[]

> #include <cstddef>
> #include <stdio.h>
> #include <cstdlib>
Now try replacing all your printf / scanf etc with corresponding C++ methods.
Hi, sorry it took me so long. I replaced scanf and printf as you said. Also changed matrix declaration to new, but my program still doesnt work. I think the problem might be in matrix allocation or something to do with pointers (I still dont understand them well) This is matrix.h and matrix.cpp now. I am really lost and dont know what to do here. Thanks.

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
/*matrix.h*/
/*====================*/
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED

#include <cstddef>
#include <stdio.h>
#include <cstdlib>

class Matrix
{
public:
   Matrix(){n=0;m=0;matrix=0;}
   Matrix(int n){allocation(n,m);}
   Matrix(char* file);
   Matrix(Matrix& A);

   ~Matrix();

   void allocation(int ln, int lm);
    void print(char* s);
private:
    int n; //Matrix rows
    int m; // Matrix columns
    double **matrix;
};

#endif // MATRIX_H_INCLUDED

/*matrix.cpp*/
/*====================*/
#include "matrix.h"
#include <assert.h>
#include <cstdlib>
#include <stdio.h>
#include <cstddef>
#include <iostream>
#include <fstream>

using namespace std;

void Matrix::allocation(int ln, int lm)
{
    n=ln;
    m=lm;

    *matrix= new double [n*m];
    for (int i=1;i<n;i++){
            matrix[i]=matrix[0]+i*m;
    }
    assert(matrix!=0);
}

Matrix::Matrix(char* file)
{
    ifstream inFile;
    inFile.open(file);
    inFile >> n;
    inFile >> m;
    allocation(n,m);

    for(int i=0;i<n; i++){
    for(int j=0; j<m; j++){

        inFile >> matrix[n][m];
    }
}

   inFile.close();
}

Matrix::~Matrix()
{
      if (n) delete [] matrix[0];
        delete [] matrix;
}

void Matrix::print(char* s)
{
    cout << s;
    if(matrix==0){
        cout << "Empty" << endl;
        return;
    }
    for(int i=0;i<n;i++){ cout << endl;;
            for (int j=0;j<m;j++){cout << matrix[n][m];}}
    cout << endl;
}
Last edited on
> *matrix= new double [n*m];
You need to new yourself a matrix, before dereferencing it.
Thanks, got it working like this:
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
void Matrix::allocation(int ln, int lm)
{
    n=ln;
    m=lm;

    matrix= new double*[n];
        for (int i=0;i<n;i++){
    matrix[i]= new double[m];
    }
}

Matrix::Matrix(char* file)
{
    double temp;
    ifstream inFile;
    inFile.open(file);
    inFile >> n;
    inFile >> m;
    allocation(n,m);

    for(int i=0;i<n; i++){
    for(int j=0; j<m; j++){
        inFile >> temp;
        matrix[i][j]=temp;
    }
}
   inFile.close();
}

Matrix::~Matrix()
{
      for(int i=0;i<n;i++){
            delete [] matrix[i];}
        delete [] matrix;
}
Topic archived. No new replies allowed.