Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4 Problem 2 : Matrix Diagonal Sums 4 . 1 Overview A problem that occasionally arises in numerical computing when working with Matrices ( 2

4 Problem 2: Matrix Diagonal Sums
4.1 Overview
A problem that occasionally arises in numerical computing when working with Matrices (2D Arrays) is to compute the sums of the Diagonals of a matrix. The main diagonal of a matrix is comprised of all elements at indices (0,0),(1,1),(2,2), and so forth. There are several numbering schemes for diagonals but we will use the one represented in the following diagram which also shows the sums of the diagonals.
sumdiag.png
Figure 6: 5 by 5 matrix with diagonals colored. The diagram shows our numbering scheme for diagonals and the corresponding diagonal sums. Diagonal numbers start at 0 and progress counter-clockwise around the bottom and right of the matrix. For square matrices, the main diagonal is always numbered as the #rows-1 or #cols-1 which are equal.
A code is provided in the file sumdiag_base.c which computes the sums of diagonals and stores them in a vector. The algorithm does so using the most "natural" approach of walking down each diagonal and totaling its elements then storing the result in the associated vector element. As you survey the code, note the use of various convenience functions such as mget(mat,i,j) and vset(vec,i,x) to interact with the matrix and vector types used.
int sumdiag_BASE_NORMAL(matrix_t mat, vector_t vec){
if(vec.len !=(mat.rows + mat.cols -1)){
printf("sumdiag_base: bad sizes
");
return 1;
}
for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Give the phase and the missing properties of P, T, v and x.

Answered: 1 week ago