int main(int argc, char* argv[]) { int k = 0; int n = 0; int flag = 0; int** array; printf("Please input the line and row number of the matrix:\n"); scanf("%d",&n); //Create matrix printf("Start create a %d * %d matrix.\n",n,n); array = (int**)malloc(sizeof(int*)*n); for(k = 0; k < n; ++k) { array[k] = (int*)malloc(sizeof(int)*n); } printf("If you want get an identity matrix, please input 0, while if you want an invertible matrix, just input 1.\n"); scanf("%d",&flag); if(flag == 0) { printf("Your input word is %d, so you want an identity matrix:\n",flag); //get indentity matrix getIdentityMatrix(n, array); //put indentity matrix into file putIdentityMatrixIntoFile(n, array); //print identity matrix printIdentityMatrix(n, array); } else if(flag == 1) { printf("Your input word is %d, so you want an invertible matrix:\n",flag); //get indentity matrix getIdentityMatrix(n, array); //get invertible matrix getInvertibleMatrix(n, array); //put invertible matrix into file putInvertibleMatrixIntoFile(n, array); //print invertible matrix printInvertibleMatrix(n, array); } else { printf("Error: You input a wrong number!\n"); return -1; } //free matrix printf("Free matrix.\n"); freeMatrix(n, array); return 1; }
注:代码中最后出现的freeMatrix函数是我专门为了释放数组内存写的,代码如下:
void freeMatrix(int n, int** array) { int k = 0; for(k = 0; k < n; ++k) { free(array[k]); } free(array); }
void getIdentityMatrix(int n, int** array) { int r = 0; int c = 0; for(r = 0; r < n; ++r) { for(c = 0; c < n; ++c) { if(r == c) array[r][c] = 1; else array[r][c] = 0; } } }
mainRowNum = (int)(rand()%(n-1));这句话是随机的在矩阵0~n-1行中选择一个主行作初等行变换,第一个循环是将主行中的元素进行一定的处理
array[mainRowNum][k])*((int)(rand()%5 - 10)后存入中介数组tempArray中(处理方式是我随便写的,也可以是别的运算方式)。
((UINT16_MAX - (array[mainRowNum][k])*((int)(rand()%5 - 10))) < 0) || ((UINT16_MAX*(-1)) - (array[mainRowNum][k])*((int)(rand()%5 - 10)) > tempArray[k])则是为了判断采用上述处理方式对矩阵元素进行运算后,元素数值是否会溢出。(如果你想了解更多,请看这里:点击打开链接)
void getInvertibleMatrix(int n, int** array) { int i = 0; int j = 0; int k = 0; int mainRowNum = 0; int* tempArray = NULL; srand((int)time(NULL)); int transformTime = (int)(rand()%1000); printf("We will do %d times tansformation.\n",transformTime); tempArray = (int*)malloc(sizeof(int)*n); for(i = 0; i < transformTime; ++i) { mainRowNum = (int)(rand()%(n-1)); for(k = 0; k < n; ++k) if(((UINT16_MAX - (array[mainRowNum][k])*((int)(rand()%5 - 10))) < 0) || ((UINT16_MAX*(-1)) - (array[mainRowNum][k])*((int)(rand()%5 - 10)) > tempArray[k])) tempArray[k] = (array[mainRowNum][k]); else tempArray[k] = (array[mainRowNum][k])*((int)(rand()%5 - 10)); for(j = 0; j < n; ++j) if(mainRowNum != j) for(k = 0; k < n; ++k) { if(((UINT16_MAX - array[j][k]) < tempArray[k]) || ((UINT16_MAX*(-1)) - array[j][k] > tempArray[k])) array[j][k] = array[j][k]/4; else array[j][k] = array[j][k] + tempArray[k]; } } free(tempArray); }
int putInvertibleMatrixIntoFile(int n, int** array) { FILE* fp = NULL; int i = 0; int j = 0; if((fp = fopen("input","w"))==NULL) { printf("Error: writing file error!\n"); return -1; } for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) { if(j != (n-1)) fprintf(fp,"%d\t", array[i][j]); else fprintf(fp,"%d", array[i][j]); } fputs("\n",fp); } fclose(fp); return 1; }
gcc -o invertible invertiblematrix.c我们以5*5的矩阵为例试一下
原文:http://blog.csdn.net/liu1075538266/article/details/51484985