首页 文章

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

提问于
浏览
-3

我正在尝试编写一个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 回答

  • 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

    我们假设该数组有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
    

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

  • 0

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

    How to get row Data

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

    How to get Column Data

    与上面相同,循环 i 并修复 j ,正好相反 .

    #include<stdio.h>
    int main()
    {
    /*This code works for only odd numbers and you have to make necessary changes to accommodate for even number of rows and columns*/
    const int row = 5, colmn = 5;
    static int array[row][colmn];
    int i, j, m, n,sumRow = 0, sumColmn = 0;
    int middleRow = row / 2, middleColmn = colmn / 2;
    int howManyRows = 3;
    printf("Enter the elements of the matrix\n");
    
    /*
    * first get elements from user
    */
    for (i = 0; i < row; ++i)
    {
        for (j = 0; j < colmn; ++j)
        {
            printf("[%d][%d]",i,j);
            scanf("%d", &array[i][j]);
        }
    }
    /*
    * printing elements to cross check the output
    */
    for (i = 0; i < row; ++i)
    {
        for (j = 0; j < colmn; ++j)
        {
            printf("%d\t",array[i][j]);
        }
        printf("\n");
    }
    
    /*
    * logic : get rows to adds (outer loop will loop through the rows to consider)
    * inner loop is to get all the elements in each row
    */
    for(int threeRows =  0 - howManyRows/2 ; threeRows <= howManyRows/2 ;threeRows++)
    {    
        for (j = 0; j < row; ++j)
        {
            sumRow += array[j][middleColmn + threeRows];            
        }
    }
    
    /*
    * its the same as above just the opposite 
    */
    for(int threeColmn = 0 - howManyRows/2; threeColmn <= howManyRows/2 ;threeColmn++)
    {    
        for (j = 0; j < row ; ++j)
        {
            sumColmn += array[middleColmn + threeColmn][j];            
        }
    }
    
    printf("middleRow = %d  middleColmn = %d  sumRow = %d sumColmn = %d",middleRow, middleColmn,sumRow,sumColmn);
    if(sumRow == sumColmn) printf("\nThey are equal");
    else printf("\nnot equal");
    return 0;
    }
    

相关问题