answersLogoWhite

0


Best Answer

#include<iostream>

#include<vector>

class triangular_matrix_t

{

private:

std::vector < std::vector<unsigned> > m_matrix;

unsigned m_gradiant;

public:

triangular_matrix_t (unsigned rows, unsigned initial_length = 1,

unsigned gradiant = 1): m_matrix (rows), m_gradiant (gradiant)

{

unsigned row_length = initial_length;

for (unsigned row=0; row<rows; ++row)

{

m_matrix[row].resize (row_length);

row_length += m_gradiant;

}

}

void set (unsigned row, unsigned column, unsigned value)

{

if (row<m_matrix.size())

{

if (column<m_matrix[row].size())

{

m_matrix[row].at(column) = value;

}

}

}

unsigned at(unsigned row, unsigned column)

{

if (row<m_matrix.size() && column<m_matrix[row].size())

return(m_matrix[row].at(column));

return 0;

}

unsigned get_height() const {

return(m_matrix.size()); }

unsigned get_length(const unsigned row) const {

return row<m_matrix.size() ? m_matrix[row].size() : 0; }

unsigned get_gradiant() const {

return m_gradiant; }

};

std::ostream& operator<< (std::ostream& os, triangular_matrix_t& matrix)

{

for (unsigned row=0; row<matrix.get_height(); ++row)

{

for (unsigned col=0; col<matrix.get_length(row); ++col)

{

os << matrix.at(row, col) <<'\t';

}

os << std::endl;

}

return os;

}

int main()

{

triangular_matrix_t matrix (10);

for (unsigned row=0; row<matrix.get_height(); ++row)

{

for (unsigned col=0; col<matrix.get_length(row); ++col)

{

matrix.set (row, col, (row+1)*(col+1));

}

}

std::cout << "Triangular matrix with 10 rows:\n" << std::endl;

std::cout << matrix << std::endl;

}

Output:

Triangular matrix with 10 rows:

1

2 4

3 6 9

4 8 12 16

5 10 15 20 25

6 12 18 24 30 36

7 14 21 28 35 42 49

8 16 24 32 40 48 56 64

9 18 27 36 45 54 63 72 81

10 20 30 40 50 60 70 80 90 100

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Code for triangular matrix using c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which country is using the code plus 44?

The UK


4x-7y equals 13 -2x plus 7y equals -3?

One way to solve this system of equations is by using matrices. Form an augmented matrix in which the first 2x2 matrix is the coefficient matrix and the 2x1 matrix on its right is the answer. Now apply Gaussian Elimination and back-substitution. Using this method gives x=5 and y=1.


If properties of matrix addition let A B C D the m n matrix then 1 A plus B B plus A?

Yes. Matrix addition is commutative.


Does a triangular pyramid have 4 triangular faces?

No, it has five plus the bottom :D * * * * * The previous answer is incorrect: a pyramid with 5 faces plus the bottom would be a pentagon based pyramid! A triangular based pyramid, or tetrahedron, DOES have 4 triangular faces.


How many triangular faces does a triangular pyramid have?

a triangular pyramid has 4 triangular sides


C plus plus program -matrix multiplication using class?

for(int i=0;i


What would be the result if you add matrices B plus A instead A plus B?

It would be no different. Matrix addition is Abelian or commutative. Matrix mutiplication is not.


What is the answer do this matrix in arbitrary form 3x-6y-z equals 6 and x plus y plus 3x equals 5?

It is a singular matrix.


What is the answer to this matrix in arbitrary form- 3x 6y-z equals 9 and x plus y plus 3z equals 4?

It is a singular matrix.


How many surfaces has a triangular prism have?

A triangular prism has five surfaces: two triangular faces, and three rectangular faces.


What shape has 2 triangular faces at opposite ends plus 3 other rectangular faces?

triangular prism


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal