Simple declaration, value putting and direct value display from 2D array
A simple code to declare a 2D array, direct value putting and value recall.
Learning Objectives
- Understanding simple declaration 2D array.
- Direct insertion and recall of data from a 2D array.
Source Code
|
Run Output
Code Understanding
int arr[2][3];
Here we are simply declaring a 2D array with expected 6 integer data members in 2 row and 3 column form.
arr[0][0]=10; //We are putting value 10 at row 0 and col 0 index.
arr[1][2]=20; //We are putting value 20 at row 1 and col 2 index
cout<<arr[0][0]<<endl; //We are recalling the value inserted by us at 0,0
cout<<arr[1][2]<<endl; //We are recalling value inserted by us at 1,2
cout<<arr[1][1]<<endl; //We are recalling an un-initialised value
The un-initialised values may keep a junk value or 0 as per the environment/compiler
Notes
- A declaration with initialisation does need to specify both bounds of a 2D Array like
int arr[2][3]; and not int arr[][3]; - Any number of dimensions can be given to an array for ex.
int arr[2][3][1][4]; is also possible and is a 4 dimensional array.
Common Errors
- Typically all array members should be given some initial values to avoid any unwanted junk values.
Suggested Filename(s): dec-ins-recall.cpp,decinsre.cpp
CSKC| Created: 15-Oct-2019 | Updated: 15-Oct-2019|