희소 행렬 확인

CBeginner
지금 연습하기

소개

희소 행렬 (Sparse matrix) 은 0 의 개수가 0 이 아닌 요소의 개수보다 많은 행렬입니다. 이 단계별 랩에서는 C 프로그래밍을 사용하여 2 차원 배열이 희소 행렬인지 확인하는 방법을 배우겠습니다.

참고: 코딩을 연습하고 gcc 를 사용하여 컴파일하고 실행하는 방법을 배우려면 직접 ~/project/main.c 파일을 생성해야 합니다.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

2 차원 배열 생성

이 단계에서는 행렬 요소를 저장할 2 차원 배열을 생성합니다. 사용자로부터 행과 열의 수를 입력받은 다음, 중첩 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]);
      }
   }

   // Checking if the matrix is sparse or not
    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]);
      }
   }

   // Printing the matrix
   printf("The matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         printf("%d ", matrix[i][j]);
      }
      printf("\n");
   }

   // Checking if the matrix is sparse or not
    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;
}

프로그램 완성

여기서는 프로그램에 최종적인 마무리를 할 것입니다. 사용자로부터 제공된 행렬이 희소 행렬 (sparse matrix) 인지 여부를 확인합니다. 그런 다음 희소 행렬인지 아닌지 결과를 출력합니다.

#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]);
      }
   }

   // Printing the matrix
   printf("The matrix:\n");
   for (i = 0; i < row; i++)
   {
      for (j = 0; j < column; j++)
      {
         printf("%d ", matrix[i][j]);
      }
      printf("\n");
   }

   // Checking if the matrix is sparse or not
    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;
}

요약

이 단계별 랩 (lab) 에서 우리는 2 차원 배열이 희소 행렬 (sparse matrix) 인지 확인하는 방법을 배웠습니다. 희소 행렬의 개념과 C 프로그래밍 언어로 행렬이 희소 행렬인지 여부를 결정하는 방법을 배웠습니다. 2 차원 배열을 생성하고, 사용자 입력을 받고, 행렬을 출력한 다음, 제공된 행렬이 희소 행렬인지 확인하는 로직을 작성했습니다.