Matrix
Feb 12, 2014 at 7:55pm UTC
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:
|a
11 | >= |a
12 | + |a
13 |
|a
22 | >= |a
21 | + |a
23 |
|a
33 | >= |a
31 | + |a
32 |
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();
}
Feb 12, 2014 at 8:18pm UTC
'I am stuck' does not give us anything that we can work with to help you. We cannot read your mind.
Feb 12, 2014 at 9:07pm UTC
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 Feb 16, 2014 at 3:35pm UTC
Topic archived. No new replies allowed.