Knowee
Questions
Features
Study Tools

int calculateMatrixSum(int rows, int columns, int **matrix) { int i, j, sum=0; if((rows>0)&&(columns>0)) { for(i=0;i<rows;i++) { sum =0; for(j=0;j<columns;j++) { if(i==j) { if(matrix[i][j]/2!=0) sum += matrix[i][i]; } } } return sum; } else return sum; }

Question

int calculateMatrixSum(int rows, int columns, int **matrix) {
int i, j, sum=0; if((rows>0)&&(columns>0)) { for(i=0;i<rows;i++) { sum =0; for(j=0;j<columns;j++) { if(i==j) { if(matrix[i][j]/2!=0) sum += matrix[i][i]; } } } return sum; } else return sum;
}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This function, calculateMatrixSum, calculates the sum of the elements of a matrix that are located at the diagonal (where the row index is equal to the column index) and are not divisible by 2.

Here is a step-by-step explanation:

  1. The function takes three parameters: rows and columns (the dimensions of the matrix), and matrix (the 2D array representing the matrix).

  2. It initializes sum to 0. This variable will hold the sum of the diagonal elements of the matrix that are not divisible by 2.

  3. It checks if the number of rows and columns is greater than 0. If not, it returns sum (which is 0 in this case).

  4. If the number of rows and columns is greater than 0, it enters a nested loop. The outer loop iterates over the rows of the matrix, and the inner loop iterates over the columns of the matrix.

  5. Inside the inner loop, it checks if the current row index is equal to the current column index (i.e., if the element is on the diagonal of the matrix).

  6. If the element is on the diagonal, it checks if the element is not divisible by 2 (i.e., if dividing the element by 2 does not yield a remainder of 0).

  7. If the element is not divisible by 2, it adds the element to sum.

  8. After iterating over all the elements of the matrix, it returns sum.

However, there is a mistake in the code. The sum = 0; statement is inside the outer loop, which means sum is reset to 0 for each row. This statement should be placed before the outer loop to correctly calculate the sum of the diagonal elements.

This problem has been solved

Similar Questions

Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151

Write C Program to Find the Sum of Each Row and Column of a 2x2 Matrix  -  15 marksSample InputEnter the co-efficients of the matrix23 4580 97Sample OutputSum of the 0 row is = 68Sum of the 1 row is = 177Sum of the 0 column is = 103Sum of the 1 column is = 142

int sum = 0;3int[,] matrix = new int[,] { {1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0} };for (int i = 0; i < matrix.GetLength(0); i++) {4int product = matrix[i,0];5for (int j = 1; j < matrix. GetLength(1); j++) {product *= matrix[i, j];sum += product;7}89}10Console.WriteLine(sum);Image not displaying?What value is printed out?

#include <stdio.h>int main() {int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};printf("%d", matrix[1][2]);return 0;}     Prints the element in the second row, third column     Prints the element in the third row, second column     Prints the sum of all elements in the matrix     Causes a compilation error

Find Sum of each Row and each Column using Functions and PointersWrite a C program to find the sum of elements of each row and sum of elements of each column of a two-dimensional array using pointers.Note: Write the functions read1(), display(), sumOfEachRow() and sumOfEachColumn() in Program1009a.c.Sample Test CasesTest Case 1:Expected Output:Enter·row·and·column·sizes·:·2 2Enter·4·elements·:·1 2 3 4The·given·matrix·is1·2·3·4·Sum·of·row·-·0·elements·=·3Sum·of·row·-·1·elements·=·7Sum·of·column·-·0·elements·=·4Sum·of·column·-·1·elements·=·6Test Case 2:Expected Output:Enter·row·and·column·sizes·:·3 3Enter·9·elements·:·1 2 3 4 5 6 7 8 9The·given·matrix·is1·2·3·4·5·6·7·8·9·Sum·of·row·-·0·elements·=·6Sum·of·row·-·1·elements·=·15Sum·of·row·-·2·elements·=·24Sum·of·column·-·0·elements·=·12Sum·of·column·-·1·elements·=·15Sum·of·column·-·2·elements·=·18Test Case 3:Expected Output:Enter·row·and·column·sizes·:·3 2Enter·6·elements·:·11 22 33 44 55 66The·given·matrix·is11·22·33·44·55·66·Sum·of·row·-·0·elements·=·33Sum·of·row·-·1·elements·=·77Sum·of·row·-·2·elements·=·121Sum·of·column·-·0·elements·=·99Sum·of·column·-·1·elements·=·132

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.