Lab # 13 Two Dimentional Arrays in C++
Lab # 13 Two Dimentional Arrays in C++
every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a' is
the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.
int main(){
int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
for (int i = 0; i < 3; i++){
for (int j = 0; j < 4; j++){
cout << "array[" << i << "][" << j << "] = " << array[i]
[j] << endl;
}
}
return 0;
}
Example#2: Storing elements in a matrix and printing it.
#include <iostream>
using namespace std;
int main (){
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<"Enter a[“i”][“j“]: ";
cin>>arr[i][j];
}
}
cout<<"\n printing the elements ....\n";
for(i=0;i<3;i++)
{
cout<<endl;
for (j=0;j<3;j++)
{
cout<<arr[i][j];
}
}
}
Example # 3: Transpose a 2D
#include <iostream>
int main() {
const int rows = 3;
const int columns = 4;
int matrix[rows][columns] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
return 0;
}
Dynamic Array
Dynamic arrays in C++ refer to arrays whose size can be dynamically allocated at runtime.
Unlike static arrays, where the size is fixed at compile-time, dynamic arrays allow you to
allocate memory for the array during program execution. This is useful when you don't know the
size of the array beforehand or when you need to change the size dynamically.
In C++, dynamic arrays are typically created using pointers and memory allocation operators like
new and delete (or malloc and free, though it's generally recommended to use the former in
C++).
#include <iostream>
using namespace std;
int main() {
int size;
return 0;
}