How to initialise this program?

I've made a program which is designed to perform matrix multiplication (using a compressed sparse row matrices), but I'm not quite sure how to initialise it in the main program.

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
#include <iostream>
#include <stdlib.h>
#include <time.h>

class CSRMatrix{
	protected:
		long rows;
		long cols;
		long nz;
		double* val;
		long* col_ind;
		long* row_ptr;

	public:

		CSRMatrix(long inrows, long incols, long innz) {
			rows = inrows; cols = incols; nz = innz;
			val = new double[nz];
			col_ind = new long[nz];
			row_ptr = new long[inrows+1];

			long col_ind1[] = {0,1,2,3,0,1,2,3,2};
			long row_ptr1[] = {0,4,6,8,9};

			std::copy(col_ind1,col_ind1+nz,col_ind);
			std::copy(row_ptr1,row_ptr1+inrows+1,row_ptr);

			srand(static_cast<unsigned>(time(NULL)));

			// Create random values for val
			for (int i=0;i<innz;i++){
				val[i] = rand() % 10 + 1;
			}

		};
		//˜CSRMatrix(){ };
		void multiply(double* x,double* y) {
			for (int i=0; i < rows; i++) {
				y[i] = 0;
				for (int j=row_ptr[i]; j<row_ptr[i+1]; j++) {
					y[i] += val[j] * x[col_ind[j]];
				}
			}
			return;
		}

};


That's the class, in the main at the moment I have:

1
2
3
4
5
6
int main ()
{
	CSRMatrix(4,4,9); // Initialise
	double x[4] = { 3, 4, 1, 2}; // Create vector x
	return 0;
}


Not quite sure how to make it run (new to C++), thank you.
http://www.cplusplus.com/doc/tutorial/classes/

That will explain how to call methods on objects.

EDIT: Tour class constructor is dynamically allocating memory for various data members. Where is that memory being freed up?
Last edited on
I need to create a destructor for the class, but again I wasn't sure how to go about that at the moment, or where I could free up the memory.

Guidance would be appreciated if possible, thank you.
I've modified the main class now to have:

1
2
3
4
5
6
7
8
int main ()
{
	CSRMatrix y(4,4,9); // Initialise
	double x[4] = {3,4,1,2}; // Create vector x
	y.multiply(x*,y*);
	//std::cout << x;
	return 0;
}


However, it's giving me the error:

1>c:\users\oc\documents\visual studio 2012\projects\sparse\csr.cpp(52): error C2059: syntax error : ','

Again, sorry for bothering you!
Topic archived. No new replies allowed.