二维数组和矩阵乘法:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define ROWS 4
#define COLUMNS 5
/*
*two dimensional array.
*/
typedef struct TwoDArray{
int rows;
int columns;
int** elements;
}TwoDArray,*TwoDArrayPtr;
/*
*two dimensional array.
*/
typedef struct TwoDStaticArray{
int rows;
int columns;
int elements[ROWS][COLUMNS];
}TwoDStaticArray,*TwoDStaticArrayPtr;
/*
*initialize the two-dimensional array.
*/
TwoDArrayPtr initTwoDArray(int paraRows,int paraColumns){
int i;
TwoDArrayPtr resultPtr = (TwoDArrayPtr)malloc(sizeof(struct TwoDArray));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->elements = (int**)malloc(paraRows*sizeof(int*));
for(i=0;i<paraRows;i++){
resultPtr->elements[i]=(int*)malloc(paraRows*sizeof(int));
}//of for it
return resultPtr;
}//of initTwoDArray
/*
*randomize the data of the array
*/
void randomizeTwoDArray(TwoDArrayPtr paraPtr,int paraLowerBound,int paraUpperBound){
int i,j;
for(i=0;i<paraPtr->rows;i++){
for(j=0;j<paraPtr->columns;j++){
paraPtr->elements[i][j] = rand()%(paraUpperBound-paraLowerBound)+paraLowerBound;
}//of for j
}//of for i
}//of randomizetwodarray
/*
*printf the array
*/
void printTwoDArray(TwoDArrayPtr paraPtr)
{
int i,j;
for(i=0;i<paraPtr->rows;i++){
for(j=0;j<paraPtr->columns;j++){
printf("%d, ",paraPtr->elements[i][j]);
}//of for j
printf("\r\n");
}//of for i
}//of inittwodarray
/*
*maxtrix multiply
*/
TwoDArrayPtr matrixMultiply(TwoDArrayPtr paraPtr1,TwoDArrayPtr paraPtr2){
int i,j,k,sum;
if(paraPtr1->columns != paraPtr2->rows){
printf("matrices cannot be multiplied.\r\n");
return NULL;
}//of if
TwoDArrayPtr resultPtr = initTwoDArray(paraPtr1->rows,paraPtr2->columns);
for(i=0;i<paraPtr1->rows;i++){
for(j=0;j<paraPtr2->columns;j++){
sum=0;
for(k=0;k<paraPtr1->columns;k++){
sum+=paraPtr1->elements[i][j]*paraPtr2->elements[k][j];
} //of for k
resultPtr->elements[i][j]=sum;
printf("sum= %d, ",sum);
}//of for j
}//of for i
return resultPtr;
}
/*
*test
*/
void twoDArrayTest(){
TwoDArrayPtr tempPtr1,tempPtr2,tempPtr3;
tempPtr1 = initTwoDArray(3,2);
randomizeTwoDArray(tempPtr1,1,5);
printf("the first matrix:\r\n");
printTwoDArray(tempPtr1);
tempPtr2 = initTwoDArray(2,4);
randomizeTwoDArray(tempPtr2,4,9);
printf("the first matrix:\r\n");
printTwoDArray(tempPtr2);
tempPtr3 = matrixMultiply(tempPtr1,tempPtr2);
printf("the result:\r\n");
printTwoDArray(tempPtr3);
}
TwoDStaticArrayPtr initTwoDStaticArray(){
int i,j;
TwoDStaticArrayPtr resultPtr = (TwoDStaticArrayPtr)malloc(sizeof(struct TwoDStaticArray));
resultPtr->rows = ROWS;
resultPtr->columns = COLUMNS;
for(i=0;i<ROWS;i++){
for(j=0;j<COLUMNS;j++){
resultPtr->elements[i][j]= i*10+j;
printf("(%d,%d):%d;",i,j,&(resultPtr->elements[i][j]));
}//of for j
}//of for i
return resultPtr;
}
int main(){
twoDArrayTest();
TwoDStaticArrayPtr tempPtr = initTwoDStaticArray();
return 1;
}//of main
压缩矩阵:
#include<stdio.h>
#include<malloc.h>
typedef int elem;
typedef struct Triple{
int i;
int j;
elem e;
}Triple,*TriplePtr;
typedef struct CompressedMatrix{
int rows,columns,numElements;
Triple*elements;
}compressedMatrix,*CompressedMatrixPtr;
CompressedMatrixPtr initCompressedMatrix(int paraRows,int paraColumns,int paraElements,int**paraData){
int i;
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->numElements = paraElements;
resultPtr->elements = (TriplePtr)malloc(paraElements*sizeof(struct Triple));
for(i=0;i<paraElements;i++){
resultPtr->elements[i].i=paraData[i][0];
resultPtr->elements[i].j=paraData[i][1];
resultPtr->elements[i].e=paraData[i][2];
}//of for i
return resultPtr;
}//of initCompressedMatrix
/*
*print the compressed matrix
*/
void printCompressedMatrix(CompressedMatrixPtr paraPtr){
int i;
for(i=0;i<paraPtr->numElements;i++){
printf("(%d,%d):%d\r\n",paraPtr->elements[i].i,paraPtr->elements[i].j,paraPtr->elements[i].e);
}
}//of printCompressedMatrix
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr){
//step1 allocate space
int i,tempColumn,tempPosition;
int *tempColumnCounts = (int*)malloc(paraPtr->columns*sizeof(int));
int *tempOffsets = (int*)malloc(paraPtr->columns*sizeof(int));
for(i=0;i<paraPtr->columns;i++){
tempColumnCounts[i]=0;
}//of for i
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows= paraPtr->columns;
resultPtr->columns= paraPtr->rows;
resultPtr->numElements= paraPtr->numElements;
resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements*sizeof(struct Triple));
//step2.one scan to calculate offsets
for(i=0;i<paraPtr->numElements;i++){
tempColumnCounts[paraPtr->elements[i].j]++;
}
tempOffsets[0]=0;
for(i=1;i<paraPtr->columns;i++){
tempOffsets[i]=tempOffsets[i-1]+tempColumnCounts[i-1];
printf("tempOffsets[%d] = %d\r\n",i,tempOffsets[i]);
}
//step3
for(i=0;i<paraPtr->numElements;i++){
tempColumn = paraPtr->elements[i].j;
tempPosition = tempOffsets[tempColumn];
resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;
tempOffsets[tempColumn]++;
}
return resultPtr;
}
/*
*test
*/
void compressedMatrixTest(){
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++){
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
int tempMatrix2[4][3] = {{0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6}};
for(i = 0; i < tempElements; i ++){
for(j = 0; j < 3; j ++) {
tempMatrix1[i][j] = tempMatrix2[i][j];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.\r\n");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.\r\n");
printCompressedMatrix(tempPtr2);
}// Of main
/**
* The entrance.
*/
int main(){
compressedMatrixTest();
return 1;
}// Of main
运行结果: