# What is the C plus plus program to find the sum of elements above and below the diagonal?

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<iostream>

#include<array> #include<random>

template<typename T, const size_t N>

class square_array {

private:

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

public:

T sum_above (void) const;

T sum_below (void) const;

const std::array<T, N>& operator[] (const size_t row) const { return data[row]; }

std::array<T, N>& operator[] (const size_t row) { return data[row]; }

};

template<typename T, const size_t N>

T square_array<T, N>::sum_above (void) const {

T sum = 0;

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

for (size_t col=row+1; col<N; ++col)

sum += data[row][col];

return sum;

}

template<typename T, const size_t N>

T square_array<T, N>::sum_below (void) const {

T sum = 0;

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

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

sum += data[row][col];

return sum;

}

template<typename T, const size_t N>

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

os << '{';

for (size_t row=0; row<N; ++row) {

os << '{';

for (size_t col=0; col<N; ++col) {

os << sa[row][col];

if (col<N-1) os << ',';

}

os << '}';

if (row<N-1) os << ',';

}

os << '}';

return os;

}

int main (void) {

std::default_random_engine engine;

std::uniform_int_distribution<int> dist (1, 9);

square_array<int, 5> 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 = "<<sa<<std::endl;

std::cout<<"Sum above: "<<sa.sum_above()<<std::endl;

std::cout<<"Sum below: "<<sa.sum_below()<<std::endl;

}

### 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. Read More

### What elements are beside aluminum in the periodic table?

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

### 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. Read More

### Which elements do not reacts with dilute hydrochloric acid?

The elements that do not react with dilute hydrochloric acid are those in the activity series below H. Those above H will react with it. Read More

### 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 Read More

### Which of the following elements is most similar in behavior to calcium?

I don't see any elements listed... but the elements that are most similar to calcium in behavior would be those directly above or below it; Mg (Magnesium) and Sr (Strontium). Read More

### Which elements below does the Hazard Communication Standard require on each hazardous chemical label?

E. All of the above choices are correct. Read More

### 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… Read More

### What elements would you expect exhibit properties most similar to those of aluminum?

The elements immediately above and below aluminum in the same column of a wide form periodic table: boron and gallium. As is usual for relatively light elements, aluminum is more like the element below it, gallium, than the one above it: Aluminum and gallium are both metals, while boron is a nonmetal. In compounds with oxyanions, however, boron is similar to aluminum compounds with oxyanions. Read More

### 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. Read More

### How is hydrogen different from the elements below it?

Hydrogen is a gas, the elements below it are metals. Read More

### What is an analogy for above?

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

### Why do the elements S and O form a covalent bond?

If the electronegativity difference between two elements is above 1.7, then ionic bond is formed between these two. If the electronegativity difference is below 1.7, then they form covalent bond. In the case of sulphur and oxygen the electronegativity difference will be below 1.7 Read More

### What flag is for scuba diving?

The flag for "Diver below" is a red flag with a diagonal white stripe. Read More

### What are two elements that have properties similar to calcium?

Magnesium and strontium are the two elements that are most chemically like calcium. They are its immediate neighbors, above and below respectively, in the same column of a periodic table, wide form column 2. Read More

### Why are their elements below the periodic table?

The elements are placed below to preserve the principle of classification by keeping elements with similar properties in single column. Read More

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

Program below?! Read More

### What are the elements that are one atomic number above one one atomic number below your element?

For now I can't recall, except that this ?'n HAS a Valid Answer ... Help Call. Read More

### What is the median if the range is 8-20?

It's impossible to tell given the data. The median is the value dividing the half of the elements above the value and the half below it. Read More

### Write a Program to find sum of all elements above and below the main diagonal of a square matrix?

#include<iostream.h> #include<conio.h> void main() { int a=0,b=0,i,j,s,c[10][10]; //initialising matrix cout<<"Enter size of square of Matrix \n"; cin>>s; cout<<"Enter Values into Matrix of side(s) "<<s<<"\n"; for(i=0; i<s; i++) // Input Matrix { for(j=0; j<s; j++) { cin>>c[i][j]; } } cout<<"\nThe Given Matrix is\n"; // Output MAtrix for(i=0; i<s; i++) { cout<<"\n"; for(j=0; j<s; j++) { cout<<c[i][j]; } } cout<<"\n"; for(i=0; i<s; i++) // Loop to add elemnts below main diagonal { for(j=0; j<s; j++) if(i>j) a… Read More

### Above is to below as?

Since the relationship between above and below is that they are opposites,the second pair in the analogy can be any two opposites. Example: above - below as up -down Read More

### Are metamorphic rocks formed above or below ground?

they are formed above and below ground Read More

### What can you see above and below?

What you can see above and below could include: air empty space cobwebs Places: above and below a desk above and below a counter above and below a chair or couch above and below a bed ceiling-floor Read More

### What are two elements that have the same properties as sodium?

These elements are lithium and potassium. Read More

### Are covalent bonds made up of metals and nonmetals?

Two elements will form ionic compound if the difference in electronegativity between the two elements is above 1.7 and two elements will form covalent compound if the difference in electronegativity between the two elements is below 1.7 Metals and non-metals will form generally ionic compounds. Read More

### Which are the elements that follow uranium in the periodic table of elements?

These elements are called transuranic elements; see the link below for the list of elements. Read More

### Is Louisiana above or below the equator?

Louisiana is above the equator. No part of the continental US is below the equator. This assumes that you are comfortable with the completely arbitrary convention of calling north above and south below. Read More

### What layers are above and below the ozone layer?

Above: the rest of the stratosphere Below: the rest of the troposphere Read More

### What are the best sentences for the words above below and within?

I was not below hi, I was above! The evil within me spoke. Read More

### Is it hotter above below or on the Equator?

It is hotter on the Equator. The more you go above or below it, the colder it gets. Read More

### Is statistical significance below or above 0.05?

If the outcome is below or equal to 0.05, then it is statistically significant; above is not. Read More

### Where do you put the lettuce on a cheeseburger above or below the beef?

above or below, you will taste no difference. Read More

### How do you write a c program to determine if a matrix is symmetric or not?

A matrix is symmetric when it is a perfect square and the transpose of the matrix is equal to the original matrix. The implementation is reasonably straightforward and can be done without actually creating the transposed matrix. We simply imagine a dividing line through the main diagonal (top-left to bottom right) and compare the corresponding elements on each side of that diagonal. bool is_symmetric (int* a, int rows, int cols) { if (rows!=cols) return false… Read More

### What is BE Condensate?

BE condensate is the state of matter below solids, like plasma is above gases. It only occurs at absolute zero(0˚ Kelvin, -273.15˚ Celsius, -459.67˚ Fahrenheit) or for some elements a few billionths of a Kelvin degree above absolute zero. Read More