Arrays in Programming


THERE ARE THREE TYPES OF ARRAYS IN PROGRAMMING.

1. One dimensional array
2. 2d array
3. 3d array


While Creation, we must assign values according to the rows in the matric. We have to forget about the columns while assigning values to a multidimensional array.

Let's see examples of all of these arrays.

1. One Dimensional Array

#include<iostream>
using namespace std;

int main(){
int one[3] = {22, 33, 11}; //Arrat declaration

for(int i=0; i<3; i++){ //Loop to pring array
cout<<one[i]<<endl;
}

return 0;
}


2. Printing a 2*2 matrix using 2 Dimensional Array

#include<iostream>
using namespace std;

int main(){

int two[2][2] = {{1, 2}, {3, 4}}; //Array Declaration

for(int i=0; i<2; i++){ //Loop to print loop
for(int j=0; j<2; j++){ //Nested loop to print both rows and columns
cout<<two[i][j]<<" "; //Printing the array. The space will be between the columns.
}
cout<<endl; //This will add space between the rows.
}


3. Printing ABC using multidimensional array

#include<iostream>
using namespace std;

int main(){

char abc[5][5] = {{'a', 'b', 'c', 'd', 'e'}, {'f', 'g', 'h', 'i', 'j'}, {'k', 'l', 'm', 'n', 'o'}, {'p', 'q', 'r', 's', 't'}, {'u', 'v', 'w', 'x', 'y'}};

for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++){
cout<<abc[i][j]<<" ";
}
cout<<endl;
}
return 0;
}



This will print something like this:


alphabets printed using multi-dimensional array