1.An Array is a group of logically related data items of same data types addressed by a common name .
3.As array stored in contiguous(physically adjacent)memory location , we can traverse all the elements(data items) in single run , hence Arrays falls under Linear Data Structure.
Syntax:<dataType> arrayName[<arraySize>]
Note: ArraySize is the total indexes that Array can have data items / elements stored.
Each elements are accessed by their individual index values.</i>
- 1.Simple Example On Array As Per Above Definition (Using List of Constants)
- 2. Simple Example On Array As Per Above Definition(User Input)
- 3. Accessing Each Elements Through Each Index Of An Array .
- 1. Largest Element in an array
- 2. Sum Of Each Element in an array
- 3. Addition of two Row Matrices/Row Vectors and Column Matrices / Column Vectors
- 4. Multiplication of Row Vector x Column Vector [(1 x m) x (m x 1)] producing Singleton Matrix(Result)
- 5. Multiplication of Row Vector x Column Vector [(1 x m) x (m x 1)] producing Singleton Matrix(Result)-[Type 2]
- 6. Transpose of a Column Vector is Row Vector
- 7. Transpose of a Row Vector is Column Vector
- 8. Subtraction of two Row Matrices/Row Vectors and Column Matrices / Column Vectors
- 9. Insert An Element Into The Last Index Of An Array[With Explanation]
- 10. Insert An element Into The First Index Of An Array[With Explanation]
- 11. Insert An Element Into Any Positional Index Of An Array[With Explanation]
- 12. Delete An Element From Last Index Of An Array
- 13. Delete An Element From First Index Of An Array[With Explanation]
- 14. Delete An Element From Any Postional Index Of An Array[With Explanation]
- 1. Representation of 2D Array(Using List of Constants)
- 2. Representation of 2D Array(User Input)
- 3. Representation of 2D Array in Matrix Format
- 4. Addition of 2D Matrix
- 5. Subtraction of 2D Matrix
- 6. Multiplication of Column Vector x Row Vector [(m x 1) x (1x n)] producing m x n matrix
- 7. Multiplication of two 2D Matrices [With Explanation ]
- 8. Creation of Null Matrix (mxn) Format
- 9. Assigning Signs (+/-)of Square Matrix in (mxn) Format
- 10. Calculation of Determinants of Sqaure Matrix (1x1 , 2x2 and 3x3) Matrix Using Sign(+/-) and Null Matrix.
- 11. Transpose of a 2D Matrix
- 12. Adjoint Matrix and Cofactor of 1x1 Matrix
- 13. Adjoint Matrix and Cofactor of 2x2 Matrix
- 14. Adjoint Matrix and Cofactor of 3x3 Matrix
- 15.Print out the last digit entered in a 3x3 matrix also Display the same in a Matrix format , through Traversal
- 16.Program to find second largest element in an 3x3 array[With Explanation]
- 17.Program to find second smallest element in an 3x3 array[With Explanation]
- 18.Program for Upper Triangular Matrix of Square Matrix(m x n)[With Explanation]
- 19.Program for Lower Triangular Matrix of Square Matrix(m x n)[With Explanation]
- 20.Program Showing only Diagonal of a Matrix(m x n)[With Explanation]
- 21.Check whether a Matrix (m x n) is Sparse / not [With Explanation]
Syntax:<dataType> arrayName[<arraySize>→<Column Size/Row Size>]
As One Dimensional Array is expandible either row wise or column wise as we can project according to its array size(i.e. Single Directional) , hence to be called as : "Vector". A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.

<dataType> arrayName[<row size>][<column size>]
Two Dimensional Array is structured in such a way that it takes first number of rows and then number of columns and store value accordingly in a physically adjacent memory location. It is also called as "Array of Arrays". One may depict it as table like in Relational Database or like Matrix format to perform some mathematical operations.

SizeOf() Operator in Arrays basically used to calculate size of an Array (in bytes) and Array's length i.e. number of Rows and Columns in a Matrix(Array). When it is sizeof(arr) → it becomes sizeof(int *) points to the array which have definite and adjacent rows and columns . When it is sizeof(arr_var[0]) → it becomes sizeof(int). For rows i.e. sizeof(arr_var[0]) it calculates sizes of int(4) in columns and gives the result while only columns i.e sizeof(arr_var[0][0]), it calculates size of int(4) only in a single column i.e. constant 4 only and gives the result . For sizeof(arr) i.e. sizeof(int *) multiplies sizeof(int) i.e. 4 with number of rows and columns. While sizeof a pointer is always 8 bytes.
Size of Array : sizeof(arr_var)
Row Length : sizeof(arr_var) / sizeof(arr_var[0])
Column Length : sizeof(arr_var[0]) / sizeof(arr_var[0][0]);
- 1.Size and Length of an Array in One Dimensional Matrix [With Explanation]
- 2.Size and Length of an Array in (m xn) Square Matrix{1x1 , 2x2 , 3x3 etc....} [With Explanation]
- 3.Size and Length of an Array in (m xn) Matrix with Variable Rows and Columns {4x2, 4x3, 1x2 ....etc.} [With Explanation]
One Dimensional
--------------------------
Function Declaration : <return_data_Type> functionName (<dataType1>[],<dataType2>[],......,<dataTypeN-1>[],<dataTypeN>[]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [],<dataType> <arrayVarName2> [],......,<dataType> <arrayVarNameN-1>[],<dataType> <arrayVarNameN>[]);
Function Call: functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
If any Array is returned from a function :
Return : return <arrayVarName>[Size of Array];
- 1.Addition of Every Elements of 1-D Array(Using Function) [With Explanation]
- 2.Largest Element in an 1-D Array(Using Function)
- 3.Addition of Two 1-D Arrays[Square Matrix](Using Function)
- 4.Subtraction of Two 1-D Arrays[Square Matrix](Using Function)
- 5.Multiplication of Two 1-D Arrays[Square Matrix](Using Function)
- 6. Second Largest Element in 1-D Array(Using Function)[With Explanation]
- 7. Second Smallest Element in 1-D Array(Using Function)[With Explanation]
- 8. Display The Last Element in 1-D Array Through Array Traversal Technique(Using Function)
- 9. Insert An Element From Last Index In An 1-D Array(Using Function)[With Explanation]
- 10. Insert An Element From First Index In An 1-D Array(Using Function)[With Explanation]
- 11. Insert An Element From Any Positional Index In An 1-D Array(Using Function)[With Explanation]
- 12. Delete An Element From Last Index In An 1-D Array(Using Function)[With Explanation]
- 13. Delete An Element From First Index In An 1-D Array(Using Function)[With Explanation]
- 14. Delete An Element From Any Positional Index In An 1-D Array(Using Function)[With Explanation]
- 1.Checking 2 arrays(Square Matrix) have Unique and Distinct elements(Using Function)
- 2.Union of 2 Distinct Arrays(Square Matrix)[i.e Intersection Elements are not Present] using Bubble Sorting [With Explanation](Using Function)
- 3.Union of 2 Arrays(Square Matrix)[When Arrays are not Distrinct i.e. Intersection Elements are Present] using Bubble Sorting [With Explanation](Using Function)
- 4.Intersection Between Two Arrays in a Sqaure Matrix (Using Function)
Note : Its not necessary that if swapping or any other functional action done inside function, we have to return array with its size i.e. array[size], We can also return its size only.
int swapping(int array[], int size){
int pos1, pos2;
cout << "Enter the position of the first element: ";
cin >> pos1;
cout << "Enter the position of the second element: ";
cin >> pos2;
int temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
return a[size];
}
Not necessary we return a[size] :
int swapping(int array[], int size){
int pos1, pos2;
cout << "Enter the position of the first element: ";
cin >> pos1;
cout << "Enter the position of the second element: ";
cin >> pos2;
int temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
return size;
}
Already Swapping Conducted under the body of the function;
That is we can return anything but if swapping or assignment or any other functional actions is done with the array its already gets applied to the array under the body of function and we can get the swapped array if we dispaly it.
Two Dimensional
--------------------------
We have to declare the column size first:
(with define macro)
#define size value[value→Value are integers such as 1,2,3 , ....etc.]
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value],<dataType2>[][size/value],......,<dataTypeN-1>[][size/value],<dataTypeN>[][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value],<dataType> <arrayVarName2> [][size/value],......,<dataType> <arrayVarNameN-1>[][size/value],<dataType> <arrayVarNameN>[][size/value]);
Function Call: functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
If any Array is returned from a function :
Return : return <arrayVarName>[Size of Row][size/value];
(with const (constant) modifier)
const int size = value; [value→Value are integers such as 1,2,3 , ....etc.]
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value],<dataType2>[][size/value],......,<dataTypeN-1>[][size/value],<dataTypeN>[][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value],<dataType> <arrayVarName2> [][size/value],......,<dataType> <arrayVarNameN-1>[][size/value],<dataType> <arrayVarNameN>[][size/value]);
Function Call: functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
If any Array is returned from a function :
Return : return <arrayVarName>[Size of Row][size/value];
Three Dimensional
--------------------------
We have to declare both the column size and row size first:
(with define macro)
#define size value[value→Value are integers such as 1,2,3 , ....etc.]
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value][size/value],<dataType2>[][size/value][size/value],......,<dataTypeN-1>[][size/value][size/value],<dataTypeN>[][size/value][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value][size/value],<dataType> <arrayVarName2> [][size/value][size/value],......,<dataType> <arrayVarNameN-1>[][size/value][size/value],<dataType> <arrayVarNameN>[][size/value][size/value]);
Function Call: functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
If any Array is returned from a function :
Return : return <arrayVarName>[No. of Page][size/value][size/value];
(with const (constant) modifier)
const int size = value; [value→Value are integers such as 1,2,3 , ....etc.]
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value][size/value],<dataType2>[][size/value][size/value],......,<dataTypeN-1>[][size/value][size/value],<dataTypeN>[][size/value][size/value]);
Function Definition : <return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value][size/value],<dataType> <arrayVarName2> [][size/value][size/value],......,<dataType> <arrayVarNameN-1>[][size/value][size/value],<dataType> <arrayVarNameN>[][size/value][size/value]);
Function Call: functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
If any Array is returned from a function :
Return : return <arrayVarName>[No. of Page][size/value][size/value];
- 1. 3D Array Passing in Parameters of Function [With constant value by 'define' Macro at Row Size and Column Size]
- 2. 3D Array Passing in Parameters of Function [With constant value by const Modifier at Row Size and Column Size]
- 3. 3D Array Passing in Parameters of Function [With fixed Value in Row Size and Column Size]
Note : Though we make tables and represent Array as Row and Columns they are actuallly adjacent memory locations arranged one after the other as shown below:
Hence After passing 2-D and 3-D arrays in Function Parameter like : int arr[][] (2D)and int arr [] [] [](3D) , it can take 1st block of memory without its size mentioned but next adjacent blocks it asks user the size and the program cannot run without it . As it fail to refer the next adjacent chunk of memory without knowing its size. It is a disadvantage as we cannot use the next memory as user defined during runtime and we have to make everytime the size constant during compilation. The above problem can be solved using pointers and pointer arithmetic.
NULL is defined as Zero . Such as in character '\0' is Null character i.e. it is present at the end of array of character whose value is 0.
int size=1 , *ptr;
size = size - 1; // i.e. 0 now
ptr = &size;
if(*ptr == NULL){
exit(0);
}
- 1. Sum Of All Elements Of An Array [Through Recursion of Array/Recursive Array]
- 2. Factorial Of All Elements Of An Array [Through Recursion of Array/Recursive Array][with Explanation]
- 3. Matrix Multiplication of 1D array [Through Recursion of Array/Recursive Array][with Explanation]
- 4. Matrix Addition of 1D array [Through Recursion of Array/Recursive Array][with Explanation]
- 5. Matrix Subtraction of 1D array [Through Recursion of Array/Recursive Array][with Explanation]
- 6.Insert An Element Into The Last Index Of An Array [Through Recursion of Array/Recursive Array]
- 7.Insert An element Into The First Index Of An Array [Through Recursion of Array/Recursive Array]
- 8.Insert An Element Into Any Positional Index Of An Array [Through Recursion of Array/Recursive Array]
- 9.Delete An Element From Last Index Of An Array [Through Recursion of Array/Recursive Array]
- 10.Delete An Element From First Index Of An Array [Through Recursion of Array/Recursive Array]
- 11.Delete An Element From Any Postional Index Of An Array [Through Recursion of Array/Recursive Array]
- 12.Second Largest Element Of An Array [Through Recursion of Array/Recursive Array]
- 13.Second Smallest Element Of An Array [Through Recursion of Array/Recursive Array]
- 14.Largest Element Of An Array [Through Recursion of Array/Recursive Array]
- 15.Smallest Element Of An Array [Through Recursion of Array/Recursive Array]
- 16.Displaying 1D Array in Reverse Order [Through Recursion of Array/Recursive Array]
- 1. Multiplication of 2D Arrays [Through Recursion of Array/Recursive Array][Explained in Details]
- 2. Multiplication of Column Vector x Row Vector [(m x 1) x (1x n)] producing m x n matrix [Through Recursion of Array/Recursive Array][Explained in Details]
- 3. Addition of 2D Matrix [Through Recursion of Array/Recursive Array]
- 4. Subtraction of 2D Matrix [Through Recursion of Array/Recursive Array]
- 5. Determinants upto 2x2 Square Matrix using Cofactors[Through Recursion of Array/Recursive Array][Explained in Details]
- 6. Determinants of Any Square Matrix using Cofactors[Through Recursion of Array/Recursive Array][Explained in Details]
- 7. Creation Of Null Matrix[Through Recursion of Array/Recursive Array]
- 8. Assigning Signs (+/-)of Square Matrix in (mxn) Format[Through Recursion of Array/Recursive Array]
- 9. Transpose of a 2D Matrix[Through Recursion of Array/Recursive Array]
- 10. Display All Cofactors Of A 2D Matrix[Through Recursion of Array/Recursive Array]
- 11. Adjoint Of Any Square 2D Matrix [Through Recursion of Array/Recursive Array]
- 12.Inverse Of Any Square 2D Matrix [Through Recursion of Array/Recursive Array]
- 13. Find 2nd Largest Number in 2D Array{With Explainatory Note} [Through Recursion of Array/Recursive Array]
- 14. Find 2nd Smallest Number in 2D Array{With Explainatory Note} [Through Recursion of Array/Recursive Array]
- 15. Find Smallest Number in 2D Array [Through Recursion of Array/Recursive Array]
- 16. Find Largest Number in 2D Array [Through Recursion of Array/Recursive Array]
- 17. Displaying Upper Triangular Matrix [Through Recursion of Array/Recursive Array]
- 17.A. Displaying Upper Triangular Matrix [Arrangement of Recursion Logic Type 1 ]
- 17.B. Displaying Upper Triangular Matrix [Arrangement of Recursion Logic Type 2 ]
- 17.C. Displaying Upper Triangular Matrix [Arrangement of Recursion Logic Type 3 ]
- 18. Displaying Lower Triangular Matrix [Through Recursion of Array/Recursive Array]
- 18.A. Displaying Lower Triangular Matrix [Arrangement of Recursion Logic Type 1 ]
- 18.B. Displaying Lower Triangular Matrix [Arrangement of Recursion Logic Type 2 ]
- 18.C. Displaying Lower Triangular Matrix [Arrangement of Recursion Logic Type 3 ]
- 19. Displaying Diagonal of a 2D Square Matrix [Through Recursion of Array/Recursive Array]
- 19.A. Displaying Diagonal of a 2D Square Matrix [Arrangement of Recursion Logic Type 1 ]
- 19.B. Displaying Diagonal of a 2D Square Matrix [Arrangement of Recursion Logic Type 2 ]
- 19.C. Displaying Diagonal of a 2D Square Matrix [Arrangement of Recursion Logic Type 3 ]
- 20. Checking A Matrix Is Sparse Or Not [Through Recursion of Array/Recursive Array]
- 21. Displaying 2D Matrix in Reverse Order [Through Recursion of Array/Recursive Array]
Note : Recursive call of a function starts from first initial starting point of function and ends till it reach the base case while in Loop , Loop starts from the starting condition of Loop and run till the condition ends(returns false by relational operators) , hence inside Recursive function , we focus only on condition rather assigning any other value inside the recursive function required for the desired output as when recursive function gets called , it again starts from first initial starting point of function, while in loop only loop block is focused which can run inside any function and we can assign any variable outside the loop block as per the condition to get the desired output.
Suppose:
int main(){
int a = 5;
func( a, 0);
}
int func(int a , int i) //Function's Initial starting point.
{
int count = 0;
//Base Condition
if(i==a){
return count;
}
//Relational Condition and Iteration
if( i < a){
count++;
func(a, i+1);
}
}
Note : The Result will be 0 only as function call starts from function's starting point.
Suppose:
int main(){
int a = 5;
func( a, 0);
}
int func(int a , int i)
{
int count = 0;
for(i =0 ;i < a; i++) //Starting of Loop
{
count = count +1; //body of the loop and the loop block
}
return count;
}
Note : The Result will be 5 as the Loop only run its block .
- Accessing Constant Elements of 1D Array (Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 2D Array (Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 3D Array (Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 1D Array (Without Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 2D Array (Without Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 3D Array (Without Assigning Pointer to Array) [Through Pointers]
- Accessing Constant Elements of 1D Array (Assigning Pointer to Array Using For Loop) [Through Pointers]
- Accessing Constant Elements of 2D Array (Assigning Pointer to Array Using For Loop) [Through Pointers]
- Accessing Constant Elements of 3D Array (Assigning Pointer to Array Using For Loop) [Through Pointers]
- Accessing Constant Elements of 1D Array (Without Assigning Pointer to Array Using For Loop) [Through Pointers]
- Accessing Constant Elements of 2D Array (Without Assigning Pointer to Array Using For Loop) [Through Pointers]
- Accessing Constant Elements of 3D Array (Withoutc Assigning Pointer to Array Using For Loop) [Through Pointers]
- 1. Accessing User Defined Elements of 1D Array (Assigning Pointer to Array ) [With User Defined Size and Elements]
- 2. Accessing User Defined Elements of 2D Array (Assigning Pointer to Array ) [With User Defined Size and Elements]
- 3. Accessing User Defined Elements of 3D Array (Assigning Pointer to Array ) [With User Defined Size and Elements]
- 1. Accessing User Defined Elements of 1D Array (Without Assigning Pointer to Array ) [With User Defined Size and Elements]
- 2. Accessing User Defined Elements of 2D Array (Without Assigning Pointer to Array ) [With User Defined Size and Elements]
- 3. Accessing User Defined Elements of 3D Array (Without Assigning Pointer to Array ) [With User Defined Size and Elements]
- 1. Accessing Elements of 1D Array Using Function [Through Pointers] [Without Assigning Pointers To Array]
- 2. Accessing Elements of 2D Array Using Function [Through Pointers] [Without Assigning Pointers To Array]
- 3. Accessing Elements of 3D Array Using Function [Through Pointers] [Without Assigning Pointers To Array]
- 1. Accessing Elements of 1D Array Using Function [Through Pointers] [ Assigning Pointers To Array]
- 2. Accessing Elements of 2D Array Using Function [Through Pointers] [ Assigning Pointers To Array]
- 3. Accessing Elements of 3D Array Using Function [Through Pointers] [ Assigning Pointers To Array]
- 1. Accessing Elements of 1D Array [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 2. Accessing Elements of 2D Array [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 3. Accessing Elements of 3D Array [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 1. Accessing Elements of 1D Array [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
- 2. Accessing Elements of 2D Array [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
- 3. Accessing Elements of 3D Array [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
- 1. Accessing Elements of 1D Array On Functions [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 2. Accessing Elements of 2D Array On Functions [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 3. Accessing Elements of 3D Array On Functions [Using Pointers To An Array Of 'n' Elements][With Pointer Arithmetic]
- 1. Accessing Elements of 1D Array On Functions [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
- 2. Accessing Elements of 2D Array On Functions [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
- 3. Accessing Elements of 3D Array On Functions [Using Pointers To An Array Of 'n' Elements][Without Pointer Arithmetic]
For 1D Array :
int a[5] = {1,2,3,4,5};
int *p = a;
Then:
*p → Points to first index of an array.
*(p+0)→ Also points to first index of an array.
*(p+1)→ Points to 2nd index of an Array.
*(p+2)→ Points to 3rd index of an Array.
....etc.
For 2D Array :
int a[2][2] = {{1, 2}, {3, 4}};
int *p = (int *)a;;
Then:
*p → Points to first index of an array.
*(p+0)→ Also points to first index of an array.
*(p+1)→ Points to 2nd index of an Array.
*(p+2)→ Points to 3rd index of an Array.
....etc.
For 3D Array :
int a[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
int *p = (int *)a;
Then:
*p → Points to first index of an array.
*(p+0)→ Also points to first index of an array.
*(p+1)→ Points to 2nd index of an Array.
*(p+2)→ Points to 3rd index of an Array.
....etc.
Hence this proves the memory which stores data value of Array are adjacent,
irrespective of their dimension change .
For 1D Array :
int a[5] = {1,2,3,4,5};
Then:
*a → Points to first index of an array.
*(a+0)→ Also points to first index of an array.
*(a+1)→ Points to 2nd index of an Array.
*(a+2)→ Points to 3rd index of an Array.
....etc.
For 2D Array :
int a[2][2] = {{1, 2}, {3, 4}};
Then:
*(*a) → Points to first index of an array.
*(*(a + 0)+0))→ Also points to first index of an array.
*(*(a + 0) + 1)→ Points to 2nd index of an Array.
*(*(a + 1) + 0)→ Points to 3rd index of an Array.
....etc.
For 3D Array :
int a[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
Then:
*(*(*a)) → Points to first index of an array.
*(*(*(a + 0) + 0) + 0)→ Also points to first index of an array.
*(*(*(a + 0) + 0) + 1)→ Points to 2nd index of an Array.
*(*(*(a + 0) + 1) + 0)→ Points to 3rd index of an Array.
....etc.
For 1D Array :
int a[5] = {1,2,3,4,5};
int *p = a;
Then:
for(int i=0;i<5;i++){
cout << *(p+i) << endl;
}
Note: Here we can write it as *(p+i*1) as columns in a row is single .
We know ,Integer's address size is 4 bytes each (for 64 bits)
_________________________________
|1 | 2 | 3 | 4 | 5 |
|3000 |3004 |3008 |3012 | 3016 |
-----------------------------------
So, p holds the 1st address of the array i.e. arr[0][0] = 3000;
Now,
When i = 0 :
*(p+0) = *(3000+0) = *(3000) = 1
When i = 1 :
*(p+1) = *(3000+4 bytes) = *(3004) = 2
When i = 2 :
*(p+2) = *(3000+4 bytes + 4 bytes) = *(3008) = 3
When i = 3 :
*(p+3) = *(3000+4 bytes + 4 bytes + 4 bytes) = *(3012) = 4
When i = 4 :
*(p+4) = *(3000+4 bytes + 4 bytes + 4 bytes + 4 bytes) = *(3016) = 5
→ We are iterating between each address through 'i' variable.
For 2D Array :
int a[2][2] = {{1, 2}, {3, 4}};
int *p = (int *)a;
Then:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << *(p + i * 2 + j) << endl;
}
}
Note: Here 2 refers to no. of columns in a row , i.e. Here 2 columns hence i*2 .
And j variable iterate between each addresses of columns.
So, if we consider i =0 and j =0 , p will point to : *(p+ 0*2 + 0 ) = *(p+ 0 + 0 ) i.e. arr[0][0].
Similarly : *(p+ 0*2 + 1 ) = *( p+ 0 + 1 ), i.e. arr[0][1]
*(p+ 1*2 + 0) = *( p+ 2 + 0 ) , i.e. arr[1][0]
*(p + 1*2 + 1) = *( p+ 2 + 1 ) i.e. arr[1][1]
More Specifically :(Integer , address size is 4 bytes each (for 64 bits) )
_______________
| 1 | 2 |
|3000 | 3004 |
--------------
| 3 | 4 |
|3008 | 3012 |
______________
So, we know how Arithmetic addition occurs,
As : int *p = (int *) arr ; Where arr[2][2] = {{1, 2}, {3, 4}};
Which means p holds the 1st address of arr i.e. arr[0][0] = 3000
Hence if we makes its output : cout << *p << endl ; it will give us 1;
When *(p+0+0) => *(3000 + 0 + 0 )= *(3000) = arr[0][0] = 1
When *(p+0+1) => *(3000 + 0 + 1 )= *(3000 + 0 + 4 bytes) = *(3004) =>arr[0][1] = 2
When *(p+2+0) => *(3000 + 2 + 0 )= *(3000 + 4 bytes + 4 bytes+ 0) = *(3008) =>arr[1][0] = 3
When *(p+2+1) => *(3000 + 2 + 1 )= *(3000 + 4 bytes + 4 bytes + 4 bytes) = *(3012) =>arr[1][1] = 4
For 3D Array :
int a[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
int *p = (int *)a;
Then:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
cout << *(p + i * 4 + j * 2 + k) << endl;
}
}
}
Here we can tell that 4 is rows * columns i.e. 2 * 2 i.e. Each page has 2 columns in each 2 rows ,
Hence i*rows*columns i.e. i*2*2 = i*4 (Here). Next we will access specific 2 columns in each rows ,
Hence j * columns (j*2 here). And to iterate between those addresses of columns we need 'k' .
We can represent the same as :
Page : 1
_______________
| 1 | 2 |
|3000 | 3004 |
--------------
| 3 | 4 |
|3008 | 3012 |
______________
Page : 2
_______________
| 5 | 6 |
|3016 | 3020 |
--------------
| 7 | 8 |
|3024 | 3028 |
______________
Then in Same Way :
p holds the 1st address of arr i.e. arr[0][0][0] = 3000
*(p + 0*4 + 0*2 + 0) = *(p + 0 + 0 + 0) = *(3000 + 0 + 0 + 0) = *(3000) = 1
*(p + 0*4 + 0*2 + 1) = *(p + 0 + 0 + 1) = *(3000 + 0 + 0 + 4) = *(3004) = 2
*(p + 0*4 + 1*2 + 0) = *(p + 0 + 2 + 0) = *(3000 + 0 + 4 + 4 + 0) = *(3008) = 3
*(p + 0*4 + 1*2 + 1) = *(p + 0 + 2 + 1) = *(3000 + 0 + 4 + 4 + 4 ) = *(3012) = 4
*(p + 1*4 + 0*2 + 0) = *(p + 4 + 0 + 0) = *(3000 + 4 + 4 + 4 + 4 +0 + 0 ) = *(3016) = 5
*(p + 1*4 + 0*2 + 1) = *(p + 4 + 0 + 1) = *(3000 + 4 + 4 + 4 + 4 +0 + 4 ) = *(3020) = 6
*(p + 1*4 + 1*2 + 0) = *(p + 4 + 2 + 0) = *(3000 + 4 + 4 + 4 + 4 +4 + 4 + 0 ) = *(3024) = 7
*(p + 1*4 + 1*2 + 1) = *(p + 4 + 2 + 1) = *(3000 + 4 + 4 + 4 + 4 +4 + 4 + 4 ) = *(3028) = 8
Therefore if we summarize for user input :
For 1D :
So if size = m (user input)
for( int i = 0 ; i < m ; i++){
cin >> /cout << *(p+i) << endl;
}
For 2D :
if rows = m (user input) and columns = n (user input)
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> /cout << *(p + i * n + j) << endl;
}
}
i.e. Pointer will point to each column of its corresponding row to take input
and generate its corresponding output .
For 3D:
if page = p (user input) ,rows = m (user input) and columns = n (user input)
for (int i = 0; i < p; i++)
{
for (int j = 0; j < m; j++)
{
for(int k =0 ; k < n ; k++)
{
cin >> /cout << *(p + i *m*n + j*n + k) << endl;
}
}
}
For 1D Array :
int a[5] = {1,2,3,4,5};
Then:
for(int i=0;i<5;i++){
cout << *(a+i) << endl;
}
We know ,Integer's address size is 4 bytes each (for 64 bits)
_________________________________
|1 | 2 | 3 | 4 | 5 |
|3000 |3004 |3008 |3012 | 3016 |
-----------------------------------
Now,
When i = 0 :
*(a+0) = *(3000+0) = *(3000) = 1
When i = 1 :
*(a+1) = *(3000+4 bytes) = *(3004) = 2
When i = 2 :
*(a+2) = *(3000+4 bytes + 4 bytes) = *(3008) = 3
When i = 3 :
*(a+3) = *(3000+4 bytes + 4 bytes + 4 bytes) = *(3012) = 4
When i = 4 :
*(a+4) = *(3000+4 bytes + 4 bytes + 4 bytes + 4 bytes) = *(3016) = 5
→ We are iterating between each address through 'i' variable.
For 2D Array :
int a[2][2] = {{1, 2}, {3, 4}};
Then:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << *(*(a + i) + j) << endl;
}
}
Note: *(a+i)→prioritizing and pointing to the row, and *(*(a + i) + j) → pointing to each column of each row.
More Specifically :(Integer , address size is 4 bytes each (for 64 bits) )
_______________
| 1 | 2 |
|3000 | 3004 |
--------------
| 3 | 4 |
|3008 | 3012 |
______________
So, we know how Arithmetic addition occurs,
when i =0 :
j = 0 :
*(*(a + i) + j) → *(*(a+0)+0) → *(*(3000 + 0) + 0 ) = *(3000 + 0 + 0 ) = *(3000) => arr[0][0] = 1
j = 1:
*(*(a + i) + j) → *(*(a+0)+1) → *(*(3000 + 0) + 1 ) => *(3000 + 1 )= *(3000 + 4 bytes) = *(3004) =>arr[0][1] = 2
when i = 1:
j =0 :
*(*(a + i) + j) → *(*(a+1)+0) → *(*(3000 + 1) + 0 ) => *(3000 + (4 bytes+ 4 bytes)+0)=> *(3008) =>arr[1][0] = 3
j = 1 :
*(*(a + i) + j) → *(*(a+1)+1) → *(*(3000 + 1) + 1 ) => *(3000 + (4 bytes+ 4 bytes)+ 4 bytes) = *(3012) => arr[1][1] = 4
For 3D Array :
int a[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
Then:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
cout << *(*(*(a + i) + j) + k) << endl;
}
}
}
We can represent the same as :
Page : 1
_______________
| 1 | 2 |
|3000 | 3004 |
--------------
| 3 | 4 |
|3008 | 3012 |
______________
Page : 2
_______________
| 5 | 6 |
|3016 | 3020 |
--------------
| 7 | 8 |
|3024 | 3028 |
______________
Then in Same Way :
*(*(*(a + i) + j) + k) = *(*(*(3000 + 0) + 0) + 0) = *(3000 + 0 + 0 + 0) = *(3000) = 1
*(*(*(a + i) + j) + k) = *(*(*(3000 + 0) + 0) + 1) = *(3000 + 0 + 0 + 4 bytes) = *(3004) = 2
*(*(*(a + i) + j) + k) = *(*(*(3000 + 0) + 1) + 0) = *(3000 + 0 + (4 bytes + 4 bytes )+ 0) = *(3000 + 8) = *(3008) = 3
*(*(*(a + i) + j) + k) = *(*(*(3000 + 0) + 1) + 1) = *(3000 + 0 + (4 bytes + 4 bytes )+ 4 ) = *(3012) = 4
*(*(*(a + i) + j) + k) = *(*(*(3000 + 1) + 0) + 0) = *(3000 + (4 + 4 + 4 + 4 )+0 + 0 ) = *(3016) = 5
*(*(*(a + i) + j) + k) = *(*(*(3000 + 1) + 0) + 1) = *(3000 + (4 + 4 + 4 + 4 )+0 + 4 bytes ) = *(3020) = 6
*(*(*(a + i) + j) + k) = *(*(*(3000 + 1) + 1) + 0) = *(3000 + (4 + 4 + 4 + 4 )+ (4 bytes + 4 bytes) + 0 ) = *(3024) = 7
*(*(*(a + i) + j) + k) = *(*(*(3000 + 1) + 1) + 1) = *(3000 + (4 + 4 + 4 + 4 )+ (4 bytes + 4 bytes) + 4 bytes ) = *(3028) = 8
Therefore if we summarize for user input :
For 1D :
So if size = m (user input)
for( int i = 0 ; i < m ; i++){
cin >> /cout << *(a+i) << endl;
}
For 2D :
if rows = m (user input) and columns = n (user input)
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> /cout << *(*(a + i) + j) << endl;
}
}
For 3D:
if page = p (user input) ,rows = m (user input) and columns = n (user input)
for (int i = 0; i < p; i++)
{
for (int j = 0; j < m; j++)
{
for(int k =0 ; k < n ; k++)
{
cin >> /cout << *(*(*(a + i) + j) + k) << endl;
}
}
}
How pointers eliminates the limitation of declaring constant sizes of Multi-Dimensional Arrays in Function? (2D , 3D ....etc.) - Pointers and Arrays in Functions
Note: Without assigning pointers to Array but accessing elements through pointers in Functions doesnot eliminates the predefined sizes of Multidimenstional Array i.e. we have to mention sizes of 2D , 3D etc. arrays before hand for input , workings and output.
For 1D Array:
//Function Declaration
int <func_name>(int [], int);
int main(){
int size;
cin >> size;
int a[size];
//FunctionCall
<func_name>(a, size);
}
//Function Definition
int <func_name>(int a[], int size)
{
for (int i = 0; i < size; i++)
{
cin >> / cout << *(a + i);
}
//Return Type
return *(a + size);
}
For 2D Array:
//Function Declaration
int <func_name>(int [][10], int , int );
int main(){
int m,n;
cin >> m >> n;
int a[m][n];
//FunctionCall
<func_name>(a, m,n);
}
//Function Definition
int <func_name>(int a[][10], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> / cout << *(*(a+i)+ j);
}
}
//Return Type
return *( * (a + rows) + cols) ;
}
For 3D Array:
//Function Declaration
int <func_name>(int [][10][10], int , int , int);
int main(){
int p, m,n;
cin >> p >> m >> n;
int a[p][m][n];
//FunctionCall
<func_name>(a, p , m,n);
}
//Function Definition
int <func_name>(int a[][10][10], int pg, int row, int col)
{
for (int i = 0; i < pg; i++)
{
for (int j = 0; j < row; j++)
{
for (int k = 0; k < col; k++)
{
cin >> / cout << *(*(*(a+i)+ j)+ k);;
}
}
}
//Return Type
return *(*(*(a + pg) +row) +col);
}
Note: With assigning pointers to Array and accessing elements through pointers eliminates the predefined sizes of Multidimenstional Array i.e. we doesnot have to mention sizes of 2D , 3D etc. arrays before hand for input , workings and output.
For 1D Array:
//Function Declaration
int <func_name>(int *, int);
int main(){
int size;
cin >> size;
int a[size];
//FunctionCall
<func_name>(a, size);
}
//Function Definition
int <func_name>(int *p, int size)
{
for (int i = 0; i < size; i++)
{
cin >> / cout << *(p + i);
}
//Return Type
return p[size];
}
For 2D Array:
//Function Declaration
int <func_name>(int *, int , int );
int main(){
int m,n;
cin >> m >> n;
int a[m][n];
//FunctionCall
<func_name>((int *)a, m,n);
}
//Function Definition
int <func_name>(int *p, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> / cout << *(p + i * cols + j);
}
}
//Return Type
return p[rows*cols];
}
For 3D Array:
//Function Declaration
int <func_name>(int *, int , int , int);
int main(){
int p, m,n;
cin >> p >> m >> n;
int a[p][m][n];
//FunctionCall
<func_name>((int *)a, p , m,n);
}
//Function Definition
int <func_name>(int *p, int pg, int row, int col)
{
for (int i = 0; i < pg; i++)
{
for (int j = 0; j < row; j++)
{
for (int k = 0; k < col; k++)
{
cin >> / cout << *(p + i * row * col + j * col + k);
}
}
}
//Return Type
return p[pg*row*col];
}
Syntax: DataType (*ArrayName)[ARRSIZE]
Note: int (*data)[10] → It defines array of 10 elements.