Answer to Question #282232 in C for Chinna

Question #282232

Consider a square matrix A of size Nx N and an integer X which is an element of A. Find the row number R and column number C of X in A, and calculate the sum of Rand even, find the sum of the digits of all even numbers in the matrix, and if the sum is odd, then find the sum of digits of all odd numbers in the matrix.



Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or while printing, as these contribute to the standard outp



Constraints: 1) 1<N<= 35.



II) 0 <= RC < N.



III) X is always an element of A.



IV) Elements of A are unique.

1
Expert's answer
2021-12-24T01:20:40-0500
def fun(a):
    r = len(a)
    c = len(a[0])
    x = r + c
    s = 0

    for i in range(r):
        for j in range(c):
            if a[i][j]%2 == x%2:
                # if a[i][j] has the same odd parity as x
                s += a[i][j]
    return s

import random
r = 3
c = 3
a = []
for i in range(r):
    a.append([random.randint(0, 9) for _ in range(c)])
print("Matrix a:", a)
for i in range(r):
    print(a[i])
    
print("fun(a) =", fun(a))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS