行列形式で線形方程式を表現するC++プログラム
これは、線形方程式を行列形式で表すC++プログラムです。
アルゴリズム
Begin 1) Take the no of variables n and the coefficients of each variable as input. 2) Declare a matrix[n][n] and constant[n][1]. 3) Make for loops i = 0 to n-1 and j = 0 to n-1 to take the coefficients of each variable as the elements of the matrix. 4) Display the matrix by using nested for loops. End
例
#include<iostream>
using namespace std;
int main(void) {
char variable[] = { 'x', 'y', 'z', 'd' };
cout << "Enter the number of variables in the
equations: ";
int n;
cin >> n;
cout << "\nEnter the coefficients of each variable for
each equation, ax + by + cz + ... = d:";
int matrix[n][n];
int constant[n][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
cin >> constant[i][0];
}
cout << "Matrix representation is: "<<endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << " " << matrix[i][j];
}
cout << " " << variable[i];
cout << " = " << constant[i][0];
cout << "\n";
}
return 0;
} 出力
Enter the number of variables in the equations: 3 Enter the coefficients of each variable for each equation, ax + by + cz + ... = d: 1 2 3 4 5 6 7 9 8 5 2 1 Matrix representation is: 1 2 3 x = 4 5 6 7 y = 9 8 5 2 z = 1
-
接続行列を使用してグラフを表現するC++プログラム
グラフの接続行列は、メモリに保存するグラフの別の表現です。この行列は正方行列ではありません。接続行列の次数はVxEです。ここで、Vは頂点の数、Eはグラフのエッジの数です。 この行列の各行に頂点を配置し、各列にエッジを配置します。エッジe{u、v}のこの表現では、列eの場所uとvに対して1でマークされます。 隣接行列表現の複雑さ 接続行列表現は、計算中にO(Vx E)のスペースを取ります。完全グラフの場合、エッジの数はV(V-1)/2になります。したがって、接続行列はメモリ内でより大きなスペースを取ります。 入力 出力 E0 E1 E2
-
隣接行列を使用してグラフを表現するC++プログラム
グラフの隣接行列は、サイズV x Vの正方行列です。Vは、グラフGの頂点の数です。この行列では、各辺にV個の頂点がマークされています。グラフにiからjの頂点までのエッジがある場合、i thの隣接行列に 行とjth 列は1(または加重グラフの場合はゼロ以外の値)になります。それ以外の場合、その場所は0を保持します。 隣接行列表現の複雑さ 隣接行列表現はO(V 2 )計算中のスペースの量。グラフに最大数のエッジと最小数のエッジがある場合、どちらの場合も必要なスペースは同じになります。 入力 出力 0 1 2 3 4 5