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
|
#include "SparseMatrix.h"
#include <cstdlib> // for exit() call
#include <iomanip> // to format output from overloaded outstream operator <<
#include <iostream>
using namespace std;
//+------------------------------------------------------------------+
//| constructs a matrix according to the data in the file "filename" |
//+------------------------------------------------------------------+
SparseMatrix::SparseMatrix( char *filename )
{
infile.open( filename );
struct SparseMatrix{
unsigned int l, c;
double val;
};
}
SparseMatrix* transpose(double A[][100], unsigned int numlines, unsigned int numcols, \
char *err, unsigned int *numEl){
struct SparseMatrix{ unsigned int l, c; double val; };
*numEl = 0; // number of elements in Matrix that aren't null
unsigned int i, j; // i = line index, j = column index
SparseMatrix *pTuplu;
for (i = 0; i<numlines; i++)
for (j = 0; j<numcols; j++)
if (A[i][j])
(*numEl)++;
double pondere = *numEl;
pondere = pondere / (numlines*numcols);
if (pondere >= 0.0015 && pondere <= 0.03){
*err = 1;
pTuplu = new SparseMatrix[*numEl];
unsigned int k = 0;
for (i = 0; i<numlines; i++)
for (j = 0; j<numcols; j++)
if (A[i][j]){
pTuplu[k].l = i + 1;
pTuplu[k].c = j + 1;
pTuplu[k].val = A[i][j];
k++;
}
return pTuplu;
}
else{
*err = 0;
return 0;
}
}
void main(){
double Matrix[25][100], val;
unsigned int numlines, numcols, i, j;
unsigned int numEl;
char err;
numlines = 25;
numcols = 100;
SparseMatrix *pMatRara = NULL;
for (i = 0; i<numlines; i++)
for (j = 0; j<numcols; j++)
Matrix[i][j] = 0;
char opt = 'y';
while (opt == 'y'){
do{
cout << "Give the row index of the not null element: ";
cin >> i;
} while (i>25 || i == 0);
do{
cout << "Give the column index of the not null \
element: ";
cin >> j;
} while (j>100 || j == 0);
cout << "Give the value of the not null element: ";
cin >> val;
if (!Matrix[i - 1][j - 1])
Matrix[i - 1][j - 1] = val;
else{
cout << "Given position contains an element with not \
null value! Overwrite the element?(y/n): ";
cin >> opt;
if (opt == 'y')
Matrix[i - 1][j - 1] = val;
}
cout << "Continue?(y/n): ";
cin >> opt;
}
pMatRara = transpose(Matrix, numlines, numcols, &err, &numEl);
if (err){
cout << "Structure of the type Sparse Matrix is:" << endl;
for (i = 0; i<numEl; i++)
cout << pMatRara[i].l << "\t" << pMatRara[i].c << "\t" << \
pMatRara[i].val << endl;
delete[] pMatRara;
}
else
cout << "Two-dimensional array does not meet the \
criteria of sparse matrix!" << endl;
}
//+--------------------------------------------------+
//| return the dot product of the corresponding rows |
//+--------------------------------------------------+
ostream& operator<<( ostream& os, const SparseMatrix &sm )
{
os << "rows = " << sm.rows << " cols = " << sm.columns << "\n";
// replace this comment with your code
return os;
}
|