一个程序,用于查找中间三行中元素的总和是否等于矩阵打印的中间三列中元素的总和

时间:2014-12-10 09:36:24

标签: c

我正在尝试编写一个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");
    }
} 

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

假设数组有n行和m列。在这种情况下,您可以按以下方式计算中间三行和列的总和

if ( n >= 3 && m >= 3 )
{
   int k = ( n - 3 ) / 2;
   int l = ( m - 3 ) / 2;

   int cols_sum = 0;
   int rows_sum = 0;

   for ( i = k; i < k + 3; i++ )
   {
      for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
   }

   for ( i = 0; i < n; i++ )
   {
      for ( j = l; j < l + 3; j++ ) cols_sum += array[i][j];
   }

   if ( rows_sum == cols_sum ) /* print appropriate message */;     
}

这是一个示范程序

#include <stdio.h>

#define N   3
#define M   3
#define RANGE   3

int main( void )
{
    int array[N][M] =
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
    size_t n = N;
    size_t m = N;

    if ( n >= RANGE && m >= RANGE )
    {
        size_t k = ( n - RANGE ) / 2;
        size_t l = ( m - RANGE ) / 2;
        size_t i, j;

        int cols_sum = 0;
        int rows_sum = 0;

        for ( i = k; i < k + RANGE; i++ )
        {
            for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
        }

        for ( i = 0; i < n; i++ )
        {
            for ( j = l; j < l + RANGE; j++ ) cols_sum += array[i][j];
        }

        if ( rows_sum == cols_sum )
        {
            printf( "The sums of three middle rows and columns are equal "
                    "each other and have value %d\n", rows_sum );
        }
        else
        {
            printf( "The sums of three middle rows and columns are not equal "
                    "each other.\n"
                    "The sum of the rows has value %d "
                    "and the sum of the columns has value %d\n", 
                    rows_sum, cols_sum );
        }
    }

    return 0;
}

输出

The sums of three middle rows and columns are equal each other and have value 45

现在您需要的只是提供数组的适当尺寸和数据的用户输入。

答案 2 :(得分:0)

在您的代码中,您要求用户row and column count,但您已经决定了这一点,而且您无法使用用户输入动态创建array。但是,有一些方法可以根据用户请求创建数组,我们有Dynamic Memory Allocation概念来拯救。暂时使用预定义的大小。

如何获取行数据

在c中,您必须将行固定为iarray[i][j] i为行,j为列引用,并循环遍历{{1}获取所有行元素值

如何获取列数据

与上述相同,循环遍历j并修复i,正好相反。

j