C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

ガウスの消去法を実装するためのC++プログラム


これは、ガウスの消去法を実装するためのC++プログラムです。連立方程式の線形システムを分析するために使用されます。これは主に、解が直接得られるように、行演算によって連立方程式を対角行列形式に縮小することに焦点を当てています。

アルゴリズム

Begin
   n = size of the input matrix
   To find the elements of the diagonal matrix:
   Make nested for loops j = 0 to n and i = 0 to n
      The element in the first row and the first column is made 1
      and then the remaining elements in the first column are made 0.
      Similarly, the elements in the second row and the second
      column is made 1, and then the other elements in the second
      column are reduced to 0 and so on.
   Print all calculated solution values.
End

#include<iostream>
using namespace std;
int main() {
   int i,j,k,n; // declare variables and matrixes as
   input
   float a[10][10],b,x[10];
   printf("\nEnter the size of matrix: ");
   scanf("%d",&n);
   printf("\nEnter the elements of augmented matrix (rowwise):\ n");
   for(i=1; i<=n; i++) {
      for(j=1; j<=(n+1); j++) {
         cout << "A[" << i << ", " << j << " ]=";
         cin >> a[i][j];
      }
   }
   //to find the elements of diagonal matrix
   for(j=1; j<=n; j++) {
      for(i=1; i<=n; i++) {
         if(i!=j) {
            b=a[i][j]/a[j][j];
            for(k=1; k<=n+1; k++) { 
               a[i][k]=a[i][k]-b*a[j][k];
            }
         }
      }
   }
   cout<<"\nThe solution is:\n";
   for(i=1; i<=n; i++) {
      x[i]=a[i][n+1]/a[i][i];
      cout<<"x"<<i << "="<<x[i]<<" ";
   }
   return(0);
}

出力

Enter the size of matrix: 3
Enter the elements of augmented matrix row-wise:
A[1, 1 ]=1
A[1, 2 ]=2
A[1, 3 ]=-4
A[1, 4 ]=2
A[2, 1 ]=7
A[2, 2 ]=6
A[2, 3 ]=-2
A[2, 4 ]=-5
A[3, 1 ]=0
A[3, 2 ]=-3
A[3, 3 ]=-5
A[3, 4 ]=-8
The solution is:
x1=-2.89831 x2=2.5678 x3=0.059322

  1. シーザー暗号を実装するC++プログラム

    これは、平文の各文字が別の文字に置き換えられて暗号文を形成するモノアルファベット暗号です。これは、換字式暗号方式の最も単純な形式です。 この暗号システムは、一般にシフト暗号と呼ばれます。コンセプトは、各アルファベットを、0から25の間の固定数で「シフト」された別のアルファベットに置き換えることです。 このタイプのスキームでは、送信者と受信者の両方がアルファベットをシフトするための「秘密のシフト番号」に同意します。この0から25までの数字が暗号化の鍵になります。 「シーザー暗号」という名前は、「3シフト」が使用されている場合のシフト暗号を表すために使用されることがあります。 プロセス

  2. 隣接行列を実装するためのC++プログラム

    グラフの隣接行列は、サイズV x Vの正方行列です。VはグラフGの頂点の数です。この行列では、各辺にV個の頂点がマークされています。グラフにiからjの頂点までのエッジがある場合、i thの隣接行列に 行とjth 列は1(または加重グラフの場合はゼロ以外の値)になります。それ以外の場合、その場所は0を保持します。 隣接行列表現の複雑さ: 隣接行列表現は、計算中にO(V2)のスペースを取ります。グラフに最大数のエッジと最小数のエッジがある場合、どちらの場合も必要なスペースは同じになります。 入力: 出力: 0 1 2 3 4