Matrix

The program that I wrote below outputs a random 3x3 matrix to a .txt file. I am trying to create/output a random diagonally dominant matrix to a .txt file where:

|a11| >= |a12| + |a13|
|a22| >= |a21| + |a23|
|a33| >= |a31| + |a32|

I'm stuck. Any help will be appreciated.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>

using namespace std;

//nxn Matrix = 3x3
const int ROWS_A = 3;
const int COLS_A = 3;


//Function Declarations
double random();
void fillMatrix(double[ROWS_A][COLS_A]);
void printMatrix(double[ROWS_A][COLS_A]);

int main()
{
	double matrix[ROWS_A][COLS_A];
	srand(time(NULL)); 
	
	fillMatrix(matrix);
	printMatrix(matrix);
	
	return 0;
	
}
///////////////////////////////////////////////////////////////////////////
//Functions
double random()
{
	double r = rand() % 20 +1;
	return r;
}

///////////////////////////////////////////////////////////////////////////
void fillMatrix(double newMatrix[ROWS_A][COLS_A])
{
	
	for (int i=0; i<ROWS_A; i++)
	{
		for (int j=0; j<COLS_A; j++)
		{
			newMatrix [i][j] = random();
		}
	}
}
///////////////////////////////////////////////////////////////////////////
void printMatrix(double newMatrix[ROWS_A][COLS_A])
{
	ofstream outFile;
	outFile.open("A.txt");
	
	for (int i=0; i<ROWS_A; i++)
	{
		for (int j=0; j<COLS_A; j++)
		{
			outFile << setw(2) << newMatrix [i][j] << "  ";
		}
		outFile << endl;
	}
	outFile.close();
}
'I am stuck' does not give us anything that we can work with to help you. We cannot read your mind.
I am trying to create a diagonally dominant matrix. My program is only creating a random matrix. How would I go about creating a diagonally dominant matrix?
Last edited on
Topic archived. No new replies allowed.