C++ arrays | types of arrays | declaration of arrays | single,two and multi dimensional array | example | passing arrays to function | My CS Tutorial
An array is a collection of similar items stored in contiguous memory locations.
There are couple of ways to declare an array.
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Array index starts with 0, which means the first array element is at index 0, second is at index 1 and so on. We can use this information to display the array elements. See the code below:-
#include <iostream>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
return 0;
}
20
30
40
50
Although this code worked fine, displaying all the elements of array like this is not recommended. When you want to access a particular array element then this is fine but if you want to display all the elements then you should use a loop like this:
#include <iostream>
using namespace std;
int main(){
int arr[] = {10,20,30,40,50};
int n=0;
while(n<=4){
cout<<arr[n]<<endl;
n++;
}
return 0;
}
two dimensional arrays
multidimensional arrays
Multidimensional arrays are also known as array of arrays. The data in multidimensional array is stored in a tabular form as shown in the diagram below:
int arr[2][3];
This array has total 2*3 = 6 elements.
• A three dimensional array:
int arr[2][2][2];
This array has total 2*2*2 = 8 elements.
• Two dimensional array
Lets see how to declare, initialize and access Two Dimensional Array elements.
int myarray[2][3];
Initialization:
We can initialize the array in many ways:
int arr[2][3] = {{10, 11 ,12} , {20 ,21 , 22}};
arr[0][0] – first element
arr[0][1] – second element
arr[0][2] – third element
arr[1][0] – fourth element
arr[1][1] – fifth element
arr[1][2] – sixth element
#include <iostream>
using namespace std;
int main(){
int arr[2][3] = {{10, 20, 30}, {40, 50, 60}};
for(int i=0; i<2;i++){
for(int j=0; j<3; j++){
cout<<"arr["<<i<<"]["<<j<<"]: "<<arr[i][j]<<endl;
}
}
return 0;
}
arr[0][1]: 20
arr[0][2]: 30
arr[1][0]: 40
arr[1][1]: 50
arr[1][2]: 60
Lets see how to declare, initialize and access Three Dimensional Array elements.
int myarray[2][3][2];
Initialization:
We can initialize the array in many ways:
int arr[2][3][2] = {
{ {1,-1}, {2, -2}, {3, -3}},
{ {4, -4}, {5, -5}, {6, -6}}
}
using namespace std;
int main(){
// initializing the array
int arr[2][3][2] = {
{ {1,-1}, {2,-2}, {3,-3} },
{ {4,-4}, {5,-5}, {6,-6} }
};
// displaying array values
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 2; z++) {
cout<<arr[x][y][z]<<" ";
}
}
}
return 0;
}
You can pass array as an argument to a function in c++ just like you pass variables as arguments. In order to pass array to the function you just need to mention the array name during function call like this:
function_name(array_name);
Example: Passing arrays to a function
In this example, we are passing two arrays a & b to the function sum(). This function adds the corresponding elements of both the arrays and display them.
#include <iostream>
using namespace std;
/* This function adds the corresponding
* elements of both the arrays and
* displays it.
*/
void sum(int arr1[], int arr2[]){
int temp[5];
for(int i=0; i<5; i++){
temp[i] = arr1[i]+arr2[i];
cout<<temp[i]<<endl;
}
}
int main(){
int a[5] = {10, 20, 30, 40 ,50};
int b[5] = {5, 5, 5, 5, 5};
//Passing arrays to function
sum(a, b);
return 0;
}
25
35
45
55
C++ arrays | types of arrays | declaration of arrays | single,two and multi dimensional array | example | passing arrays to function | My CS Tutorial
_______________________________________
C++ arrays | My CS Tutorial |
Arrays in c++
An array is a collection of similar items stored in contiguous memory locations. In programming, sometimes a simple variable is not enough to hold all the data. For example, lets say we want to store the marks of 200 students, having 200 different variables for this task is not feasible, we can define an array with size 200 that can hold the marks of all students.C++ arrays
An array is a collection of similar items stored in contiguous memory locations.
Declaring an array in C++
There are couple of ways to declare an array.
Method 1:
int arr[5];arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Method 2:
int arr[] = {10, 20, 30, 40, 50};Method 3:
int arr[5] = {10, 20, 30, 40, 50};Accessing Array Elements
Array index starts with 0, which means the first array element is at index 0, second is at index 1 and so on. We can use this information to display the array elements. See the code below:-
#include <iostream>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
return 0;
}
Output:
1020
30
40
50
Although this code worked fine, displaying all the elements of array like this is not recommended. When you want to access a particular array element then this is fine but if you want to display all the elements then you should use a loop like this:
#include <iostream>
using namespace std;
int main(){
int arr[] = {10,20,30,40,50};
int n=0;
while(n<=4){
cout<<arr[n]<<endl;
n++;
}
return 0;
}
Types of arrays
one dimensional aaraystwo dimensional arrays
multidimensional arrays
Multidimensional arrays are also known as array of arrays. The data in multidimensional array is stored in a tabular form as shown in the diagram below:
Multidimensional Array in C++
A two dimensional array:
int arr[2][3];
This array has total 2*3 = 6 elements.
• A three dimensional array:
int arr[2][2][2];
This array has total 2*2*2 = 8 elements.
• Two dimensional array
Lets see how to declare, initialize and access Two Dimensional Array elements.
How to declare a two dimensional array?
int myarray[2][3];
Initialization:
We can initialize the array in many ways:
Method 1:
int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};Method 2:
This way of initializing is preferred as you can visualize the rows and columns here.int arr[2][3] = {{10, 11 ,12} , {20 ,21 , 22}};
Accessing array elements:
arr[0][0] – first element
arr[0][1] – second element
arr[0][2] – third element
arr[1][0] – fourth element
arr[1][1] – fifth element
arr[1][2] – sixth element
Example: Two dimensional array in C++
#include <iostream>
using namespace std;
int main(){
int arr[2][3] = {{10, 20, 30}, {40, 50, 60}};
for(int i=0; i<2;i++){
for(int j=0; j<3; j++){
cout<<"arr["<<i<<"]["<<j<<"]: "<<arr[i][j]<<endl;
}
}
return 0;
}
Output:
arr[0][0]: 10arr[0][1]: 20
arr[0][2]: 30
arr[1][0]: 40
arr[1][1]: 50
arr[1][2]: 60
Three dimensional array
Lets see how to declare, initialize and access Three Dimensional Array elements.
Declaring a three dimensional array:
int myarray[2][3][2];
Initialization:
We can initialize the array in many ways:
Method 1:
int arr[2][3][2] = {1, -1 ,2 ,-2 , 3 , -3, 4, -4, 5, -5, 6, -6};Method 2:
This way of initializing is preferred as you can visualize the rows and columns here.int arr[2][3][2] = {
{ {1,-1}, {2, -2}, {3, -3}},
{ {4, -4}, {5, -5}, {6, -6}}
}
Three dimensional array example
#include <iostream>using namespace std;
int main(){
// initializing the array
int arr[2][3][2] = {
{ {1,-1}, {2,-2}, {3,-3} },
{ {4,-4}, {5,-5}, {6,-6} }
};
// displaying array values
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 2; z++) {
cout<<arr[x][y][z]<<" ";
}
}
}
return 0;
}
Output:
1 -1 2 -2 3 -3 4 -4 5 -5 6 -6
Passing array to function
You can pass array as an argument to a function in c++ just like you pass variables as arguments. In order to pass array to the function you just need to mention the array name during function call like this:
function_name(array_name);
Example: Passing arrays to a function
In this example, we are passing two arrays a & b to the function sum(). This function adds the corresponding elements of both the arrays and display them.
#include <iostream>
using namespace std;
/* This function adds the corresponding
* elements of both the arrays and
* displays it.
*/
void sum(int arr1[], int arr2[]){
int temp[5];
for(int i=0; i<5; i++){
temp[i] = arr1[i]+arr2[i];
cout<<temp[i]<<endl;
}
}
int main(){
int a[5] = {10, 20, 30, 40 ,50};
int b[5] = {5, 5, 5, 5, 5};
//Passing arrays to function
sum(a, b);
return 0;
}
Output:
1525
35
45
55
C++ arrays | types of arrays | declaration of arrays | single,two and multi dimensional array | example | passing arrays to function | My CS Tutorial
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment