以下二維陣列給出了這個線性系統:
1.0 0.0 -1.0 -4.9 -5.9 -6.9 -7.9
0.0 1.0 2.0 4.4 5.4 6.4 7.4
0.0 0.0 0.0 5.7 5.7 -3.3 -3.3
0.0 0.0 0.0 2.9 2.9 2.9 2.9
0.0 0.0 0.0 7.0 -1.0 -3.0 -3.0
0.0 0.0 -20.0 -65.9 -89.9 -100.9 128.9
每當我在主對角線上得到 0 時(當行等于列時),我想改變行的順序,所以我的主對角線上沒有零。
在這種情況下,第 2 行(從 0 開始)應該與第 5 行(也從 0 開始)進行交易,因為這樣,主對角線上沒有 0。
我已經這樣做了,但我正在“洗掉”第一行并將其附加到線性系統的末尾。我應該如何使這個邏輯知道在哪里確切地交易行?
代碼如下:
void change_order(double linear[6][7], unsigned int qty) {
double aux[100];
// dynamically create an array of pointers of size `m`
double **matrix = (double **)malloc((qty 1) * sizeof(double *));
// dynamically allocate memory of size `n` for each row
for (int r = 0; r < qty 1; r ) {
matrix[r] = (double *)malloc((qty 1) * sizeof(double));
}
for (int i = 0; i < qty; i ) {
for (int j = 0; j < qty 1; j ) {
if (i == 0)
aux[j] = linear[i][j];
}
}
for (int i = 0; i < qty; i ) {
for (int j = 0; j < qty 1; j ) {
matrix[i][j] = linear[i][j];
}
}
remove_line(matrix, 0, qty);
for (int i = 0; i < qty; i ) {
for (int j = 0; j < qty 1; j ) {
linear[i][j] = matrix[i][j];
}
}
for (int i = 0; i < qty; i ) {
for (int j = 0; j < qty 1; j ) {
if (i == qty- 1) {
linear[i][j] = aux[j];
}
}
}
}
void remove_line(double ** linear, int row, unsigned int qty) {
qty--;
free(linear[row]);
while (row < qty) {
linear[row] = linear[row 1];
row ;
}
}
int main() {
double matrix[][7] = {
{1.0, 0.0, -1.0, -4.9, -5.9, -6.9, -7.9},
{0.0, 1.0, 2.0, 4.4, 5.4, 6.4, 7.4},
{0.0 , 0.0, 0.0, 5.7, 5.7, -3.3, -3.3},
{0.0 , 0.0, 0.0, 2.9, 2.9, 2.9, 2.9},
{0.0 , 0.0, 0.0, 7.0, -1.0, -3.0, -3.0},
{0.0 , 0.0, -20.0, -65.9, -89.9, -100.9, 128.9}
};
change_order(matrix, 6);
}
示例輸入:
0 3 2 28
4 0 2 24
2 3 0 16
4 2 1 0
可以兌換:
4 0 2 24
2 3 0 16
4 2 1 0
0 3 2 28
uj5u.com熱心網友回復:
如果我正確理解您的要求,請您嘗試以下操作:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 6
#define COLS 7
/*
* search for a trade line to be swapped below the n'th row
*/
int search_trade(double matrix[][COLS], int qty, int n)
{
for (int i = n 1; i < qty; i ) {
if (matrix[i][n] != 0.0) {
return i; // i'th row is a nice trade
}
}
return -1; // not found
}
/*
* swap m'th row and n'th row
*/
void swap(double matrix[][COLS], int qty, int m, int n)
{
int j;
double tmp;
for (j = 0; j < qty 1; j ) {
tmp = matrix[m][j];
matrix[m][j] = matrix[n][j];
matrix[n][j] = tmp;
}
}
void change_order(double linear[][COLS], int qty) {
for (int i = 0; i < qty; i ) {
if (linear[i][i] == 0.0) { // found 0 in the diagonal
int k = search_trade(linear, qty, i); // search for the trade row
if (k < 0) { // no applicable trade
fprintf(stderr, "cannot find the row to swap. abort.\n");
exit(1);
} else {
swap(linear, qty, i, k); // swap i'th row and k'th row
}
}
}
}
/*
* print the elements of the matrix
*/
void matprint(double matrix[][COLS], int qty)
{
for (int i = 0; i < qty; i ) {
for (int j = 0; j < qty 1; j ) {
printf("%.2f%s", matrix[i][j], j == qty ? "\n" : " ");
}
}
printf("\n");
}
int main() {
double matrix[][COLS] = {
{1.0, 0.0, -1.0, -4.9, -5.9, -6.9, -7.9},
{0.0, 1.0, 2.0, 4.4, 5.4, 6.4, 7.4},
{0.0 , 0.0, 0.0, 5.7, 5.7, -3.3, -3.3},
{0.0 , 0.0, 0.0, 2.9, 2.9, 2.9, 2.9},
{0.0 , 0.0, 0.0, 7.0, -1.0, -3.0, -3.0},
{0.0 , 0.0, -20.0, -65.9, -89.9, -100.9, 128.9}
};
matprint(matrix, ROWS);
change_order(matrix, ROWS);
matprint(matrix, ROWS);
}
輸出:
0.00 1.00 2.00 4.40 5.40 6.40 7.40
0.00 0.00 0.00 5.70 5.70 -3.30 -3.30
0.00 0.00 0.00 2.90 2.90 2.90 2.90
0.00 0.00 0.00 7.00 -1.00 -3.00 -3.00
0.00 0.00 -20.00 -65.90 -89.90 -100.90 128.90
1.00 0.00 -1.00 -4.90 -5.90 -6.90 -7.90
0.00 1.00 2.00 4.40 5.40 6.40 7.40
0.00 0.00 -20.00 -65.90 -89.90 -100.90 128.90
0.00 0.00 0.00 2.90 2.90 2.90 2.90
0.00 0.00 0.00 7.00 -1.00 -3.00 -3.00
0.00 0.00 0.00 5.70 5.70 -3.30 -3.30
您會看到第 2 行和第 5 行交換了。
主要概念是:
- 尋找值 0 的對角線元素。
- 如果找到 0,則搜索在同一列中具有非零值的交易行。
- 如果沒有找到交易行,程式將列印一條錯誤訊息并中止。
- 如果找到交易行,則交換行。
[編輯]
回答您的評論,代碼假定列數 == 行數 1。由于您提供的示例有 4x4 矩陣,讓我添加一個額外的列:
double matrix[][COLS] = {
{0, 3, 2, 28, -1},
{4, 0, 2, 24, -1},
{2, 3, 0, 16, -1},
{4, 2, 1, 0, -1}
};
(請注意,值 -1 是一個虛擬值,到目前為止毫無意義。)
并將這些#define行修改為:
#define ROWS 4
#define COLS 5
然后程式會輸出:
0.00 3.00 2.00 28.00 -1.00
4.00 0.00 2.00 24.00 -1.00
2.00 3.00 0.00 16.00 -1.00
4.00 2.00 1.00 0.00 -1.00
4.00 0.00 2.00 24.00 -1.00
0.00 3.00 2.00 28.00 -1.00
4.00 2.00 1.00 0.00 -1.00
2.00 3.00 0.00 16.00 -1.00
which shows the rows are properly rearranged having no 0 values in the diagonal. (BTW your expected result breaks having 0 in the diagonal in the last row.)
uj5u.com熱心網友回復:
**您可以像下面這樣創建一個主函式://在傳遞線性函式之后
int i,j, temp; // declare i and j as global variables**
for(i=0,i<qty 1,i )
{
for (j=0;j<qty 1;j )
{
if(i==j & matrix[i][j]==0)
{
remove_line;
temp = i;
break;
}
}
} // here we are looking for a zero in the diagonal.
for (;i<qty 1;i )
{
if(matrix[i][j]!=0)
{
matrix[temp][j] = linear[i][j]
}
}
// **here we are increasing the rows till we get a non zero element and then
interchanging the values.**
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327474.html
