【问题标题】:check 3 element of matrix 3 x 3 in axes x, y and diagonal if it's equal to each other检查轴 x、y 和对角线中矩阵 3 x 3 的 3 个元素是否相等
【发布时间】:2020-12-13 18:26:58
【问题描述】:

我想编写一个代码来检查 x、y 和对角线中 3 x 3 矩阵中的 3 个元素,并在输出中打印成功。谁能帮帮我

【问题讨论】:

  • 你填充一个矩阵,如果存在,你在这个矩阵中搜索 3 个值,这是你的问题?
  • 不,我需要检查矩阵中 x、y 和对角线中的元素
  • 您正在编写一个以 3x3 矩阵为输入并返回 true 或 false 的函数?

标签: arrays c matrix


【解决方案1】:

据我了解您的问题,我使用布尔函数来检查矩阵中是否存在三个值

我的代码:

#include <stdio.h>
#include <stdbool.h>

bool Exist(int rows,int cols,int matrix[rows][cols],int Val);

int main(void)
{
    int rows=3, cols=3;
    /*
    do
    {
        printf("Give me the number of rows :");
        scanf("%d",&rows);
    }while(rows<1);
    
    do
    {
        printf("Give me the number of cols :");
        scanf("%d",&cols);
    }while(cols<1);
    **/

   int arr[rows][cols];

   printf("\nThe filling of the Matrix :\n");

   for (int i = 0; i < rows; i++)
   {
       for (int j = 0; j < cols; j++)
       {
           scanf("%d", &arr[i][j]);
       }
   }

   for (int i = 0; i < rows; i++)
   {printf("\n");
      for (int j = 0; j < cols; j++)
      {
          printf("[%d]",arr[i][j]);
      }
   }

   printf("\n\n");
   int VAL_1=0,VAL_2=0,VAL_3=0;
   printf("\nGive me Value 1 & Value 2 & Value 3 :");
   scanf("%d%d%d",&VAL_1,&VAL_2,&VAL_3);
   if((Exist(rows,cols,arr,VAL_1)==false)||(Exist(rows,cols,arr,VAL_2)==false)||(Exist(rows,cols,arr,VAL_3)==false))
   {
       printf("\nThe three values don't exist in this matrix !\n\n");
   }
   else
   {
       printf("\nThe three values exist in this matrix !\n\n");
   }
   printf("\n\n");
   return 0;
}

bool Exist(int rows,int cols,int matrix[rows][cols],int Val)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if(i==0)
            {
                if((matrix[i][j]==Val)||(matrix[j][i]==Val))
                {
                    return true;
                }
            }
        }
       if(matrix[i][i]==Val)
       {
          return true;
       }
   }
   return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多