Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | An efficient C ++ program to compute sum for given array of cell indexes ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Compute sum of all elements , sum of every row and sum every column ; Compute the desired sum for all given cell indexes ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE #define R  3 NEW_LINE #define C  3 NEW_LINE using namespace std ; struct Cell { int r ; int c ; } ; void printSums ( int mat [ ] [ C ] , struct Cell arr [ ] , int n ) { int sum = 0 ; int row [ R ] = { } ; int col [ C ] = { } ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { sum += mat [ i ] [ j ] ; col [ j ] += mat [ i ] [ j ] ; row [ i ] += mat [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { int ro = arr [ i ] . r , co = arr [ i ] . c ; cout << sum - row [ ro ] - col [ co ] + mat [ ro ] [ co ] << endl ; } } int main ( ) { int mat [ ] [ C ] = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; struct Cell arr [ ] = { { 0 , 0 } , { 1 , 1 } , { 0 , 1 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printSums ( mat , arr , n ) ; return 0 ; }