疎行列のチェック

CCBeginner
オンラインで実践に進む

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

疎行列とは、0 の数が非ゼロ要素の数よりも多い行列です。この手順に沿った実験では、C 言語を使って二次元配列が疎行列かどうかを確認する方法を学びます。

注:コーディングを練習し、gcc を使ってコンパイルと実行方法を学ぶには、自分で~/project/main.cファイルを作成する必要があります。

cd ~/project
## main.cを作成する
touch main.c
## main.cをコンパイルする
gcc main.c -o main
## mainを実行する
./main

二次元配列の作成

このステップでは、行列要素を格納するための二次元配列を作成します。行数と列数のユーザー入力を受け取り、その後、ネストした for ループを使ってユーザーに 1 つずつ行列要素を入力してもらいます。

#include <stdio.h>

int main()
{
   int matrix[10][10], row, column, i, j;
   printf("Enter the number of rows and columns of the matrix:\n");
   scanf("%d%d", &row, &column);

   printf("Enter the elements of the matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         scanf("%d", &matrix[i][j]);
      }
   }

   // 行列が疎行列かどうかを確認する
    int counter = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (matrix[i][j] == 0)
                counter++;
        }
    }

    if(counter > (row * column) / 2)
        printf("The matrix is a sparse matrix\n");
    else
        printf("The matrix is not a sparse matrix\n");

   return 0;
}

行列を表示する

ここでは、既にユーザー入力を受け取っているネストした for ループを使って行列を表示します。

#include <stdio.h>

int main()
{
   int matrix[10][10], row, column, i, j;
   printf("Enter the number of rows and columns of the matrix:\n");
   scanf("%d%d", &row, &column);

   printf("Enter the elements of the matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         scanf("%d", &matrix[i][j]);
      }
   }

   // 行列を表示する
   printf("The matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         printf("%d ", matrix[i][j]);
      }
      printf("\n");
   }

   // 行列が疎行列かどうかを確認する
    int counter = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (matrix[i][j] == 0)
                counter++;
        }
    }

    if(counter > (row * column) / 2)
        printf("The matrix is a sparse matrix\n");
    else
        printf("The matrix is not a sparse matrix\n");

   return 0;
}

プログラムを完成させる

ここでは、プログラムに最後の仕上げを加えます。ユーザーが提供した行列が疎行列かどうかを確認します。その後、疎行列であるかどうかの結果を表示します。

#include <stdio.h>

int main()
{
   int matrix[10][10], row, column, i, j;
   printf("Enter the number of rows and columns of the matrix:\n");
   scanf("%d%d", &row, &column);

   printf("Enter the elements of the matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         scanf("%d", &matrix[i][j]);
      }
   }

   // 行列を表示する
   printf("The matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         printf("%d ", matrix[i][j]);
      }
      printf("\n");
   }

   // 行列が疎行列かどうかを確認する
    int counter = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (matrix[i][j] == 0)
                counter++;
        }
    }

    if(counter > (row * column) / 2)
        printf("The matrix is a sparse matrix\n");
    else
        printf("The matrix is not a sparse matrix\n");

   return 0;
}

まとめ

この手順に沿った実験では、二次元配列が疎行列かどうかを確認する方法を学びました。疎行列の概念と、C 言語でコードを書いて行列が疎行列かどうかを判断する方法を学びました。二次元配列を作成し、ユーザー入力を受け取り、行列を表示し、最後に、提供された行列が疎行列かどうかを確認するロジックを書きました。