Question
use python and one loop to solve this problem

Q6: Secondary Trace You are given a function that takes a square array, matrix, and returns the sum of the secondary diagonal values for matrix. This function employs a nested 2D for-loop. Write a function secondary trace single loop that solves the same problem using only one for-loop. For example, on the picture below secondary diagonal consists of values: a03, a12, a21, a30. a00 a01 a02 a03 a10 all a12 a13 a20 a21 a22 a23 a30 a31 a32 a33
def secondary trace (matrix) : 1 Return sum of values on secondary diagonal. >>mO[, 0], [0, 111 > ml[, 1, 1], [2, 2, 21,[3, 3, 311 >>> m2 [[3, 4],[6, 911 >>> secondary trace (m0) >>> secondary trace (ml) >>> secondary trace (m2) 10 Tr TT 11 for i in range (len (matrix)): for j in range (1en (matrix)): if ilen (matrix) - -1: sum += matrix [i] [j] return sum
def seconday trace single loop (matrix): IT TT 11 Return sum of values on secondary diagonal >>> mo = [[1, 0], [0, 1]] >>> m2 = [[3, 4], [6, 911 >>> seconday trace single loop (mO) >>>seconday trace single loop (mi) >>> seconday trace single loop (m2) 10 1 YI # YOUR CODE IS HERE
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def secondary_trace_single_loop(matrix):
diag_sum=0
size = len(matrix[0])#find the lenth of the column
for i in range(size):#loop untill the column ends
diag_sum += matrix[i][size-i-1]#sum the secong diagonal elemnt
return diag_sum#return the sum of diagonal


m0=[[1,0],[0,1]]
m1=[[1,1,1],[2,2,2],[3,3,3]]
m2=[[3,4],[6,9]]
print(secondary_trace_single_loop(m0))#call the function with matrix and print the results
print(secondary_trace_single_loop(m1))
print(secondary_trace_single_loop(m2))

output and code sneps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
use python and one loop to solve this problem Q6: Secondary Trace You are given a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q2 15 Points Let A € Mnxn(R). Define trace(A) = {1-1 Qiji (i. e. the sum...

    Q2 15 Points Let A € Mnxn(R). Define trace(A) = {1-1 Qiji (i. e. the sum of the diagonal entries) and tr : Mnxn(R) +R, A H trace(A). Q2.1 2 Points Show that U = {A € Mnxn(R): trace(A) = 0} is a subspace of Mnxn (R). Please select file(s) Select file(s) Q2.2 4 Points Compute dim(im(tr)) Enter your answer here and dim(ker(tt) Enter your answer here each (1pt) Justify your answer. (2pt) Enter your answer here Q2.3 5 Points...

  • PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one...

    PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one function named multiply. This function should accept one parameter, which you can assume will be a list with one or more integers in it. (If you don’t know what I mean when I say “list of integers”, go do the reading!) The function should loop through all of the numbers in the list, and multiply all of the numbers together. It should return the...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT