answersLogoWhite

0


Best Answer

Place each dimension in a separate array subscript. E.g., a table of 5 rows of 6 integers could be declared as follows:

int table[5][6];

A cuboid of 3x4x5 integers could be declared as follows:

int cube[3][4][5];

Of course, with static arrays, the dimensions are known in advance, and they are fixed for each dimension. In other words, in an array of 5x6 elements, each of the 5 elements in the first dimension has exactly 6 elements in the second dimension. This is also true of dynamic arrays (the only difference being the dimensions are not known in advance). However, sometimes you want an array where the number of elements in each dimension can vary. That is, the first element in the first dimension might only have 1 element, but the second might have 2 or more elements. In this case you are no longer dealing with a traditional multi-dimensional array, you are dealing with a 1 dimensional array of 1 dimensional arrays: or an array of arrays.

The following code demonstrates this concept, but rather than using dynamic C-style arrays, it uses an array of vectors instead. Vectors are always the preferred method of instantiating dynamic arrays in C++ because, unlike an array, a vector knows exactly how many elements it contains, and is therefore much easier to work with, especially when passing dynamic arrays into external functions.

The following code increases the size of the second dimension by one for each first dimension, thus creating a triangular table of values, rather than the more traditional rectangular table. The values are taken from the standard multiplication tables, thus the first row contains 1x1, the second contains 2x1 and 2x2, the third contains 3x1, 3x2 and 3x3, and so on. The final row contains the complete 12 times table, from 12x1 to 12x12.

#include<iostream>

#include<iomanip>

#include<vector>

int main()

{

using namespace std;

typedef vector<int> array1d;

// Build a 1 dimensional array of vectors of increasing length:

array1d A[12];

for( size_t x=0; x<12; ++x )

{

array1d& B = A[x];

for( size_t y=0; y<x+1; ++y )

B.push_back(( x+1 )*( y+1 ));

}

// Print array:

for( size_t x=0; x<12; ++x )

{

for( size_t y=0; y<A[x].size(); ++y )

cout<<setw(3)<<A[x][y]<<" ";

cout<<endl;

}

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you enter dimensions of matrix in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.


What is the product of matrix A and C?

It's matrix C.


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


Write a program to display Identity matrix?

#include &lt;stdio.h&gt; void main() { int a,b,c,i,j; printf("Enter the number of rows for square matrix : "); scanf("%d",&amp;a); for(i=1;i&lt;=a;i++) { for(c=1,j=1;j&lt;=a;j++,c++) { if(c==i) printf("1 "); else printf("0 "); } printf("\n"); } getch(); }


Perform addition multiplication subtraction of 2-D array using Operator Overloading in C plus plus?

#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;random&gt; template&lt;const size_t R, const size_t C&gt; class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix&amp; source); Matrix (Matrix&amp;&amp; source); Matrix&amp; operator= (const Matrix&lt;R,C&gt;&amp; source); Matrix&amp; operator= (Matrix&lt;R,C&gt;&amp;&amp; source); ~Matrix () {} public: // accessors row_type&amp; row (const size_t index) { return m_data[index]; } const row_type&amp; row (const size_t index) const { return m_data[index]; } row_type&amp; operator[] (const size_t index) { return m_data[index]; } const row_type&amp; operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix&lt;R,C&gt;&amp; operator+= (const Matrix&lt;R,C&gt;&amp;); Matrix&lt;R,C&gt;&amp; operator-= (const Matrix&lt;R,C&gt;&amp;); }; template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix() { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = 0; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix(const Matrix&lt;R,C&gt;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = source.m_data[row][col]; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;::Matrix(Matrix&lt;R,C&gt;&amp;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator= (const Matrix&lt;R,C&gt;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator= (Matrix&lt;R,C&gt;&amp;&amp; source) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator+= (const Matrix&lt;R,C&gt;&amp; rhs) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt;&amp; Matrix&lt;R,C&gt;::operator-= (const Matrix&lt;R,C&gt;&amp; rhs) { for (size_t row=0; row&lt;R; ++row) for (size_t col=0; col&lt;C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt; operator+ (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R,C&gt;&amp; rhs) { Matrix&lt;R,C&gt; sum (lhs); return sum += rhs; } template&lt;const size_t R, const size_t C&gt; Matrix&lt;R,C&gt; operator- (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R,C&gt;&amp; rhs) { Matrix&lt;R,C&gt; sub (lhs); return sub -= rhs; } template&lt;const size_t R, const size_t C, const size_t R1, const size_t C1&gt; Matrix&lt;R,C1&gt; operator* (const Matrix&lt;R,C&gt;&amp; lhs, const Matrix&lt;R1,C1&gt;&amp; rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix&lt;R,C1&gt; mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template&lt;const size_t R, const size_t C&gt; std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const Matrix&lt;R,C&gt;&amp; m) { for (size_t row=0; row&lt;R; ++row) { for (size_t col=0; col&lt;C; ++col) { std::cout &lt;&lt; m[row][col] &lt;&lt; '\t'; } std::cout &lt;&lt; std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution&lt;int&gt; distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix&lt;rows, cols&gt; a, b; for (size_t row=0; row&lt;rows; ++row) { for (size_t col=0; col&lt;cols; ++col) { a[row][col] = distribution (generator); b[row][col] = distribution (generator); } } std::cout &lt;&lt; "Matrix a:\n\n" &lt;&lt; a &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix b:\n\n" &lt;&lt; b &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a + b:\n\n" &lt;&lt; a + b &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a - b:\n\n" &lt;&lt; a - b &lt;&lt; '\n' &lt;&lt; std::endl; Matrix&lt;cols, rows&gt; c; for (size_t row=0; row&lt;rows; ++row) { for (size_t col=0; col&lt;cols; ++col) { c[col][row] = distribution (generator); } } std::cout &lt;&lt; "Matrix c:\n\n" &lt;&lt; c &lt;&lt; '\n' &lt;&lt; std::endl; std::cout &lt;&lt; "Matrix a * c:\n\n" &lt;&lt; a * c &lt;&lt; '\n' &lt;&lt; std::endl; }

Related questions

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.


How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.


What is the product of matrix A and C?

It's matrix C.


Can you multiply a 2x2 matrices?

You can definitely multiply 2x2 matrices with each other. In fact you can multiply a AxB matrix with a BxC matrix, where A, B, and C are natural numbers. That is, the number of columns of the first matrix must equal the number of rows of the second matrix--we call this "inner dimensions must match."


C plus plus program -matrix multiplication using class?

for(int i=0;i


A c program to find maximum number in a 3 by 3 matrix?

int matrix[][]; // the matrix to find the max in int max = matrix[0][0]; int r,c; for(r = 0; r &lt; 3; ++r) { for(c = 0; c &lt; 3; ++c) { if(matrix[r][c] &gt; max) { max = matrix[r][c]; } } } // max is now the maximum number in matrix


How do you multiply 3x3 matrices by 1x3 or 3x1?

First of all, if we have any two matrices of sizes mxn and pxq where m, n, p and q are natural numbers, then we must have n=p to be able to multiply the matrices. The result is an mxq matrix. For example, a 3x1 matrix has m=3 and n=1. We can multiply it with any matrix of size 1xq. For example a 2x3 matrix can be multiplied with a 3x1 matrix which has 3 rows and 1 column and the result is a 2x1 matrix. (2x3) multiplies by (3x1) gives a (2x1) matrix. The easy way to remember this is write the dimension of Matrix A and then Matrix B. The two inner numbers must be the same and the two outer numbers are the dimensions of the matrix you have after multiplication. For example Let Matrix A have dimensions (axb) multiply it by matrix B which has dimensions (bxc) = the result is matrix of dimensions ac. Using the trick we would remind ourselves by writing (a,b)x(b,c)=(a,c). This is technically wrong because the numbers are dimensions, but it is just a method to help students remember how to do it. So, a 3x3 matrix can be multiplied by a 3x 1 but not by a 1,3 matrix. How do you do it? Just multiply each entry in the first row of A by each entry in the first column of B and add the products. Do the same for the next row etc. Many (or should I honestly say MOST) people use their fingers and go along row one and then down column one. The add the products of the entries as they do that. Then they do the same for row two and column two etc. It really does help!


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


Write a program to display Identity matrix?

#include &lt;stdio.h&gt; void main() { int a,b,c,i,j; printf("Enter the number of rows for square matrix : "); scanf("%d",&amp;a); for(i=1;i&lt;=a;i++) { for(c=1,j=1;j&lt;=a;j++,c++) { if(c==i) printf("1 "); else printf("0 "); } printf("\n"); } getch(); }


C Matrix addition program?

How we can addition Two Matrix plz send coding in C language mahesh dhanotiya astah_mahesh@rediff.com how i can built a square matrix in c,


Addition of two matrices?

The two matrices and their answer must be of the same dimensions. Each element of the answer matrix is the sum of the elements in the corresponding elements on the matrices that are being added. In algebraic form, if A = {aij} where 1 &le; i &le; m, 1 &le; j &le; n is an mxn matrix B = {bij} where 1 &le; i &le; m, 1 &le; j &le; n is an mxn matrix and C = {cij} = A + B, then C is an mxn matrix and cij = aij + bij for all 1 &le; i &le; m, 1 &le; j &le; n


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

We have to proof that A+B=B+A we know from defn of matrix addition that (ij )th element of B+A is bij+aij Since aij &amp;bij are real no's aij +bij =bij+aij (1