# C++ Matrix Program (Class)

Page 1 of 1

## 5 Replies - 1365 Views - Last Post: 12 February 2014 - 08:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=339869&amp;s=5cc85a6c7c317ebaf42367ac1eb683bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 R2B Boondocks

• D.I.C Head

Reputation: 12
• Posts: 203
• Joined: 19-September 12

# C++ Matrix Program (Class)

Posted 12 February 2014 - 07:47 AM

Hey guys,

I was wondering if anyone could give me some pointers. I'm trying to come up with a matrix class so that I can do a class assignment that has me go through 3 dif iterative methods that work off of matrices (Jacobi, Gauss-seidel, SOR with user input for the omega value). I'm sure there are algorithms out there already for those three so that shouldn't be a problem.

What my issues are trying to finish this matrix class so that I can achieve the following: Generate a random, diagonally dominant matrix problem and save to file A and b into separate files, Load a matrix of size nxn from a file named A.txt, Load a vector of size n from a file named b.txt, then solve the problem Ax=b using the 3 methods above.

matrix.h
```#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED

class matrix
{
public:
matrix(int n);
void gensol();
void genb();
void display();

private:
int N;
double ** A;
double *Sol;
double *b;
};

#endif // MATRIX_H_INCLUDED

```

matrix.cpp
```#include <cmath>
#include <stdlib.h>
#include <iomanip>
#include <iostream>

#include "matrix.h"

using namespace std;

matrix::matrix(int n)
{   double sum=0;
N=n;
A = new double * [N];
Sol = new double [N];
b = new double [N];
gensol();

for (int i=0; i<N; i++)
{A[i]= new double [N];}
int r;

for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{A[i][j]=( (double) (rand()%10000) )/100.0;
r=rand()%2;
if(r==1){A[i][j]=A[i][j]*-1;}
}

//enforcing diagonal dominance
for (int row=0; row<N; row++)
{
for(int col=0; col<N; col++)
{if(row!=col) {sum= sum+abs(A[row][col]);}
}
A[row][row]=sum+100;
sum=0;
}
genb();

}

void matrix::display()
{
for(int i=0; i<N; i++)
{for(int j=0; j<N; j++)
{cout<<setw(10)<<A[i][j];}
cout<<setw(15)<<Sol[i];
cout<<setw(15)<<b[i];
cout<<endl;

}

}

void matrix::gensol()
{
int r;
for(int row=0; row<N; row++)
{Sol[row]=( (double) (rand()%10000) )/100.0;
r=rand()%2;
if(r==1){Sol[row]=Sol[row]*-1;}
}
}

void matrix::genb()
{

double temp = 0;
for(int row=0; row<N; row++)
{
for(int col=0; col<N; col++)
{
temp=(A[row][col]*Sol[col])+temp;
}
b[row]=temp;
temp=0.0;

}

}

```

main.cpp
Not developed yet

I'm trying to just get this thing to be created first.

*EDIT*

Do you guys think I can follow this and then just add my functions that are already declared to it to get everything in order?

http://www.cs.umb.ed...pts/cs410-7.pdf

This post has been edited by R2B Boondocks: 12 February 2014 - 07:57 AM

Is This A Good Question/Topic? 0

## Replies To: C++ Matrix Program (Class)

### #2 jimblumberg

Reputation: 4648
• Posts: 14,578
• Joined: 25-December 09

## Re: C++ Matrix Program (Class)

Posted 12 February 2014 - 08:03 AM

Let's start with your class definition. Why all the mysterious variable and function names? Why not use variable names that actually mean something in the content they are meant to be used?

Next think about using std::vector instead of the pointers for your arrays. Since you're going to be doing multiple things with this class I would try to make the constructor very simple, just initialize the elements to zero. Create another function that fills the elements with actual values if actually needed.

Oh and I recommend you create your main() earlier rather than later so you can test you class as you move along.

Jim
Was This Post Helpful? 1

### #3 modi123_1

• Suitor #2

Reputation: 10690
• Posts: 41,639
• Joined: 12-June 08

## Re: C++ Matrix Program (Class)

Posted 12 February 2014 - 08:10 AM

Quote

What my issues are trying to finish this matrix class

Would you be more specific - what is preventing you from finishing? What are you blocking points?
Was This Post Helpful? 0

### #4 R2B Boondocks

• D.I.C Head

Reputation: 12
• Posts: 203
• Joined: 19-September 12

## Re: C++ Matrix Program (Class)

Posted 12 February 2014 - 08:12 AM

Not a bad idea. I'd hate to be "finished" and then have to figure out where I went wrong when testing in main. Vector matrices are the same as array matrices except, of course, one is vector based and the other is array right?

As far as variable / function names it couldn't hurt to clarify them a bit...matrix_A and vector_b for example.

Thanks Jim!

- Modi

error: no match for 'operator<<' in 'std::cout << matrix(3)'

I'm assuming this came up because I hadn't yet overloaded the << operator?
Was This Post Helpful? 0

### #5 jimblumberg

Reputation: 4648
• Posts: 14,578
• Joined: 25-December 09

## Re: C++ Matrix Program (Class)

Posted 12 February 2014 - 08:21 AM

Quote

Vector matrices are the same as array matrices except, of course, one is vector based and the other is array right?

They are similar, std::vector handles all "dynamic" aspects so you don't have to worry about new/free, and vectors always know their sizes.

Jim
Was This Post Helpful? 0

### #6 R2B Boondocks

• D.I.C Head

Reputation: 12
• Posts: 203
• Joined: 19-September 12

## Re: C++ Matrix Program (Class)

Posted 12 February 2014 - 08:23 AM

Sounds like I'll be switching to vectors
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }