answersLogoWhite

0

In order to sum the elements above and below the diagonal, the array must be perfectly square. To guarantee the array is square we'll use a class template to implement an array of N arrays of length N. The following program demonstrates the minimal implementation required to sum the elements above and below the diagonal and will work for any array type for which operator+ is defined, which includes all the built-in numeric types such as int and double. operator<< has also been overloaded to assist in printing the array.

#include

#include #include

template

class square_array {

private:

std::array, N> data; // the 2D array

public:

T sum_above (void) const;

T sum_below (void) const;

const std::array& operator[] (const size_t row) const { return data[row]; }

std::array& operator[] (const size_t row) { return data[row]; }

};

template

T square_array::sum_above (void) const {

T sum = 0;

for (size_t row=0; row

for (size_t col=row+1; col

sum += data[row][col];

return sum;

}

template

T square_array::sum_below (void) const {

T sum = 0;

for (size_t row=0; row

for (size_t col=0; col

sum += data[row][col];

return sum;

}

template

std::ostream& operator<< (std::ostream& os, const square_array& sa) {

os << '{';

for (size_t row=0; row

os << '{';

for (size_t col=0; col

os << sa[row][col];

if (col

}

os << '}';

if (row

}

os << '}';

return os;

}

int main (void) {

std::default_random_engine engine;

std::uniform_int_distribution dist (1, 9);

square_array sa; // 5x5 square array of int

// initialise elements with random values (range: 1 to 9)

for (size_t row=0; row<5; ++row)

for (size_t col=0; col<5; ++col)

sa[row][col] = dist (engine);

std::cout<<"Array = "<

std::cout<<"Sum above: "<

std::cout<<"Sum below: "<

}

User Avatar

Wiki User

8y ago

What else can I help you with?

Continue Learning about Engineering

Write a program to input the square matrix and display the sum of diagonal elements?

#include&lt;iostream&gt; #include&lt;random&gt; #include&lt;time.h&gt; int main() { std::default_random_engine generator ((unsigned)time(0)); std::uniform_int_distribution&lt;int&gt; distribution (1,9); std::cout &lt;&lt; "Array:\n" &lt;&lt; std::endl; int a[3][3]; for (size_t r=0; r!=3; ++r) { for (size_t c=0; c!=3; ++c) { a[r][c] = distribution (generator); std::cout &lt;&lt; a[r][c] &lt;&lt; '\t'; } std::cout &lt;&lt; std::endl; } std::cout &lt;&lt; std::endl; int diagonal=0, anti_diagonal=0; for (size_t i=0; i!=3; ++i) { diagonal += a[i][i]; anti_diagonal += a[2-i][i]; } std::cout &lt;&lt; "Diagonal sum : " &lt;&lt; diagonal &lt;&lt; std::endl; std::cout &lt;&lt; "Anti-diagonal sum : " &lt;&lt; anti_diagonal &lt;&lt; std::endl; }


Are oil pipelines above or below ground?

both


How do you find missing numbers in a array with elements below its size?

Please rephrase your question. An array usually has a fixed size and I don't recall ever having to "go below its size". This implies that the missing elements are not within the range of the array.


The main method of a program has?

The Java main method of a program is the place where the program execution begins. A main method would look like below public static void main(String[] args){ }


Give sample program?

class firstprog { public static void main (String args[]) { System.out.pritnln("My first Program in Java "); } } Simple Java Programe Write the above program in Ms dos's edit or Notepad of Windows save it with "firstprog.java" run the commands given below in command prompt of Ms Dos to get the output Remamber Java is case sensative so mentain capital and small letter. 1. javac firstprog.java 2. java firstprog

Related Questions

What is meant by tridiagonal matrix?

A tridiagonal matrix is one in which the only non-zero elements are on the principal diagonal, and the two diagonals immediately next to it: one below and the other above.


Which elements boils above 2500 degrees?

See a complete list at the link below.


What elements are beside aluminum in the periodic table?

Silicon on its right , Boron above, and Gallium below it.


How come elements below uranium have equally short half-lives as the elements above uranium?

This affirmation is not correct; the half lives are different.


An element in the activity series can replace any element in?

Below it on the list on the periodic table


Is the network administration program a good choice for your friend if his marks are either 1 point above or below the class average?

No


How do you Derive The Number of reflexive relation of a Set 's' having n elements?

make a table as I did below for the set {a,b,c} with 3 elements. A table with all n elements will represent all the possible relations on that set of n elements. We can use the table to find all types of relations, transitive, symmetric etc. | a | b | c | --+---+---+---+ a | * | | | b | | * | | c | | | * | The total number of relations is 2^(n^2) because for each a or b we can include or not include it so there are 2 possibilities and there are n^2 elements so 2^(n^2) total relations. A relation is reflexive if contains all pairs of the form {x,x) for any x in the set. So this is the diagonal of your box. THESE ARE FIXED! No, in reflexive relation we still can decide to include or not include any of the other elements. So we have n diagonal elements that are fixed and we subtract that from n^2 so we have 2^(n^2-n) If you do the same thing for symmetric relations you will get 2^(n(n+1)/2). We get this by picking all the squares on the diagonal and all the ones above it too.


What is an analogy for above?

above - sky as below - ground above - airplane as below - subway


What is Jamaica above or below?

cuba is above and the Caribbean sea is below


What is a variance covariance matrix?

Briefly, the variance for a variable is a measure of the dispersion or spread of scores. Covariance indicates how two variables vary together. The variance-covariance matrix is a compact way to present data for your variables. The variance is presented on the diagonal (where the column and row intersect for the same variable), while the covariances reside above or below the diagonal.


As above so below?

As above so below? How about water? Not always so.


How can you do the same thing as the program below but using strings and arrays in C language?

Program below?!