ガウスザイデル法を実装するC++プログラム
ガウスザイデル法は、反復法で線形連立方程式を解くために使用されます。これは、ガウスザイデル法を実装するためのC++プログラムです。
アルゴリズム
Begin Take the dimensions of the matrix p and its elements as input. Take the initials values of x and no of iteration q as input. While q>0 Make a for loop i = 0 to p-1 initialize n[i] = (b[i] / a[i][i]). Make a for loop i = 0 to p-1 If (j == i) n[i] = n[i] - ((a[i][j] / a[i][i]) * m[j]). m[i] = n[i]. Decrease q. /* Here, a[i][j] = input matrix. b[i] = this array takes values of the right side of equation. m[i] = stores initial values of x. */ Return 0 End
例
#include<iostream>
#include<conio.h>
using namespace std;
int main(void) {
float a[10][10], b[10], m[10], n[10];
int p = 0, q = 0, i = 0, j = 0;
cout << "Enter size of 2D array : ";
cin >> p;
for (i = 0; i < p; i++) {
for (j = 0; j < p; j++) {
cout << "a[" << i << ", " << j << " ]=";
cin >> a[i][j];
}
}
cout << "\nEnter values to the right side of equation\n";
for (i = 0; i < p; i++) {
cout << "b[" << i << ", " << j << " ]=";
cin >> b[i];
}
cout << "Enter initial values of x\n";
for (i = 0; i < p; i++) {
cout << "x:[" << i<<"]=";
cin >> m[i];
}
cout << "\nEnter the no. of iteration : ";
cin >> q;
while (q> 0) {
for (i = 0; i < p; i++) {
n[i] = (b[i] / a[i][i]);
for (j = 0; j < p; j++) {
if (j == i)
continue;
n[i] = n[i] - ((a[i][j] / a[i][i]) * m[j]);
m[i] = n[i];
}
cout<<"x"<<i + 1 << "="<<n[i]<<" ";
}
cout << "\n";
q--;
}
return 0;
} 出力
Enter size of 2D array : 2 a[0, 0 ]=1 a[0, 1 ]=2 a[1, 0 ]=3 a[1, 1 ]=4 Enter values to the right side of equation b[0, 2 ]=1 b[1, 2 ]=2 Enter initial values of x x:[0]=0 x:[1]=0 Enter the no. of iteration : 3 x1 = 1. x2 = -0.25 x1 = 1.5 x2 = -0.625 x1 = 2.25 x2 = -1.1875
-
AVLツリーを実装するためのC++プログラム
AVLツリーは自己平衡二分探索木であり、左右のサブツリーの高さの差がすべてのノードで複数になることはありません。 ツリーの回転は、AVLツリーの要素の順序を妨げることなく構造を変更する操作です。ツリー内で1つのノードを上に移動し、1つのノードを下に移動します。これは、ツリーの形状を変更したり、小さいサブツリーを下に移動したり、大きいサブツリーを上に移動したりして高さを低くしたりするために使用され、多くのツリー操作のパフォーマンスが向上します。回転の方向は、木のノードが移動する側に依存しますが、他の人は、どの子がルートの場所をとるかに依存すると言います。これは、AVLツリーを実装するためのC+
-
STLにSet_Symmetric_differenceを実装するC++プログラム
これは、set_symmetric_differenceを実装するためのC++プログラムです。 2つのセットの対称差は、一方のセットには存在するが、もう一方のセットには存在しない要素によって構築されます。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_symmetric_difference (set1, set1 + n, set2, se