gpt4 book ai didi

c - 查找矩阵 print 中间三行元素之和是否等于中间三列元素之和的程序

转载 作者:行者123 更新时间:2023-11-30 20:56:10 25 4
gpt4 key购买 nike

我正在尝试编写一个C程序来查找中间三行的元素之和是否等于矩阵打印中间三列的元素之和。到目前为止我能够找到矩阵中所有列和行的总和。

#include<stdio.h>
void main()
{

static int array[10][10];
int i, j, m, n,sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
if (m>=5&& n>=5)
{
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
else
{
printf("The matrix should be a 5 by 5 or bigger");
}
}

最佳答案

试试这个

int mid1 = (m-3)/2;
int mid2 = (n-3)/2;
int sum1=0,sum2=0;
//suppose m is 9 index(0-8), so this for loop will add the index 3,4,5
for(i=mid1; i<mid1+3; i++)
{
for(j=0; j<n; j++)
{
sum1+=array[i][j];
}
}
for(i=0; i<m; i++)
{
for(j=mid2; j<mid2+3; j++)
{
sum2+=array[i][j];
}
}
if(sum1==sum2)
//equal
else
//not equal

关于c - 查找矩阵 print 中间三行元素之和是否等于中间三列元素之和的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27397552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com