CoCubes Coding Question - Sum of Individual rows and columns

Find sum leaving our row and col: CoCubes coding question



PROBLEM STATEMENT:
You are given a function,
int FindSumLeavingOutRowCol(int** arr, int m, int n, int i,int j);
 The function takes  a two-dimensional array 'arr', its number of rows 'm', its number of columns 'n' and integers 'i' and 'j' as input. Implement the function to find and return the sum of elements of the array leaving out the elements of the i and j column. The algorithm to find the sum is as follows:
1.Iterate over every row except for i row,and keep on adding each element except for the elements of j column to a variable 'sum'.
NOTE:
1.Row and column indices start from 0.
2. Value of i and j from 1.

CODING:
int FindSumLeavingOutRowCol(int** arr, int m, int n,int j);
/* write your code here */

}

***********************

Program

***********************

/*
 * C program to accept a matrix of order M x N and find the sum
 * of each row and each column of a matrix
 */
#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);
    printf("Enter the co-efficients 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;
    }
}

No comments:

Post a Comment