هذا هو نص الكود المتوفر لدي وأرجو ممن كان لديه مساعدة إضافية ألا يبخل بها عليّ لأن المشروع عليه 30% من العلامة وأنا في حالة ميؤوس منها
public class RowNode
{
public RowNode nextRow;
public int iRow;
public CellNode firstCell;
}
public class CellNode
{
public CellNode nextCell;
public int iCol;
public int iValue;
}
The second structure of data storing we can use to present a sparse matrix is that using the linked lists for each row and each column. So it needs two pointers: Next in this row and next in this column.
struct node {
node *nextcol;
node *nextrow;
int row;
int col;
int value;
};
class matrix
{
node **colhead; //the head element of columns
node **rowhead; //the head element of rows
int rows,cols; // number of rows and columns of a matrix
operations of the class
}
Supplementing two attributes, rows and cols are needed to present number of rows and number of columns of a matrix. With these structures of data, we can implement the operations on matrices easier.
Matrix class detail definition
With the definition of a data structure as above, a sparse matrix is an object of class matrix. Some operations (methods) of class matrix can be defined as:
class matrix {
node **colhead; //head pointer of cols
node **rowhead; // head pointer of rows
int rows,cols; //number of row and column of a matrix
public:
matrix(char fn[LGMAX]); // init a matrix by reading data from a file
matrix(int m, int n); //init a matrix with m rows and n cols
matrix(int m); // init a unit marix
// find a element which is located by row and col
node *find(int row,int col);
//get value of element at (row,col), return 0
float get_element(int row,int col);
if not exist
// insert a element or remove
void insert_remove(int row,int col,float value);
//element if value==0 this operation
//is needed when perfroming inverse
void inverse(); // this operation is used to inverse a matrix
void read_file(char fn[LGMAX+1]); //read data from file
void write_file(char fn[LGMAX+1]); //write data to file
void add(matrix B); //addition 2 matrices
void multi(matrix B); //mutiply 2 matrices
void display(); // display matrix to screen
~matrix();
int getrows(); // get number of row of a matrix
int getcols(); // get number of col of a matrix
};
Addition algorithm
To compute value of element at (row,col) of the addition matrix, at first we find the elements in the two source matrices respectively. If there is no existence of at least one element at (row,col), it means that in the result matrix we have no element at this cell (row,col). But if there is existence of at least one element at cell (row,col) then we have to compute the value of result matrix element at cell (row,col). After computing value, we add this value (of course this value is non zero) to result matrix at location of (row,col) by using add_item operation.
void matrix::add(matrix B) {
node *lead1, *lead2;
int m,n,m1,n1,i,j,kq;
//rows1 and cols1 are stored size of matrix 1 when call init_matrix1
m=rows;
n=cols;
m1=b.rows;
n1=b.cols;
if ((m!=m1) || (n!=n1))
{
box_str("Size of matrices is not the same !!");
return;
}
matrix c(rows,cols);
for (i=0;i for (j=0;j
{
kq=0;
if (rowhead->nextcol!=NULL)
{
lead1=rowhead->nextcol;
while (lead1!=NULL)
{
if ((lead1->row==(i+1)) && (lead1->col==(j+1)))
kq=kq+lead1->value;
lead1=lead1->nextcol;
}
}
if (b.rowhead->nextcol!=NULL)
{
lead2=b.rowhead->nextcol;
while (lead2!=NULL)
{
if ((lead2->row==(i+1)) && (lead2->col==(j+1)))
kq=kq+lead2->value;
lead2=lead2->nextcol;
}
}
if (kq!=0)
c.insert_remove(i+1,j+1,kq);
}
char fn[LGMAX+1];
cout<<"Write to file:";gotoxy(7,25);cin>>fn;
c.write_file(fn);
cout<<"FIRST MATRIX";
display();
getch();
cout<<"SECOND MATRIX";
b.display();
getch();
cout<<"THE RESULT OF ADDITION MATRICE";
c.display();
}
Multiply algorithm
Similar to adding two matrices, the result matrix of multiplying two matrices is computed as follows:
We will use the original algorithm that uses loop commands.
for (i=0;i for(j=0;j {
c[j]=0;
for(k=0;k c[j]=c[j]+a[k]*b[k][j];
}
But because we do not store zero elements, the loop commands will be transformed to new form as follows:
void matrix:: multi(matrix B)
{
node *lead1, *lead2;
int m,n,i,j,kq,k,m1,n1;
//rows1 and cols1 are stored size of matrix 1 when call init_matrix1
m=rows;
n=b.cols;
if (cols!=b.rows)
{
box_str("Sizes of matrices are not allow for multiplication...");
return;
}
matrix c(rows,b.cols);
for (i=0;i {
if (rowhead->nextcol!=NULL) //
for(k=0;k {
kq=0;
lead1=rowhead->nextcol;
while (lead1!=NULL)
{
lead2=b.colhead[k]->nextrow;
while (lead2!=NULL)
{
if (lead1->col==lead2->row)
kq=kq+lead1->value*lead2->value;
lead2=lead2->nextrow;
}
lead1=lead1->nextcol;
}
if (kq!=0)
c.insert_remove(i+1,k+1,kq);
}
}
char fn[LGMAX+1];
gotoxy(5,24);cout<<"Write to file:";gotoxy(7,25);cin>>fn;
c.write_file(fn);
gotoxy(22,2);cout<<"FIRST MATRIX";
display();
getch();
gotoxy(22,2);cout<<"SECOND MATRIX";
b.display();
getch();
gotoxy(22,2);cout<<"THE RESULT OF MULTIPLY MATRICE";
c.display();
}