- for (int i = 0; i < 3; i++) // This loop is run for each row.
- {
- for(int j = 0; j < 4; j++) // This loop is run for each column
- {
- matrix[i][j] = i+j; // We are inserting (value = i+j) into i-th row & j-th column.
- }
- }
Learn 2D Matrix vs 2D Vector |
---|
INTRODUCTION 2D matrix also known as array of arrays has their size allocated in the beginning, hence are static mode of row-column representation. CREATION AND INSERTION INTO 2D MATRIX
To populate each row and column we write code as:
We will be traversing 0th row, 1st row, 2nd row (0-based indexing) and 0th, 1st, 2nd, 3rd column respectively for each row. CREATION AND INSERTION IN 2D VECTORS
For the above code the outer loop runs for 3 times i.e 3 rows. Main advantage of 2d vectors lies in the fact that you are only assigning the memory according to your requirement, and are not bound to allocate fixed spaces even if they are not required unlike 2d arrays which have there size initialized in the beginning. In the above examples you now have the idea that 2d arrays means no. of columns are fixed for each row which is not with the 2d vectors. |
Published: | November 24, 2022 |
Author: | admin |
Category: | Full Tutorials |
Views: | 27 |
This article was posted in Full Tutorials. Bookmark the permalink. Follow comments with the RSS feed for this post.Post a Comment or leave a trackback: Trackback URL.
|
|