0% found this document useful (0 votes)
14 views4 pages

Arrays

Arrays in C++ are a group of similar elements stored in contiguous memory locations. There are two types of arrays: single dimensional arrays and multidimensional arrays. Arrays provide advantages like code optimization and easy data manipulation but have a fixed size. Arrays can be passed to functions by providing only the array name. Multidimensional arrays allow grouping of data into rows and columns to represent matrices or tables.

Uploaded by

Sajjad hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Arrays

Arrays in C++ are a group of similar elements stored in contiguous memory locations. There are two types of arrays: single dimensional arrays and multidimensional arrays. Arrays provide advantages like code optimization and easy data manipulation but have a fixed size. Arrays can be passed to functions by providing only the array name. Multidimensional arrays allow grouping of data into rows and columns to represent matrices or tables.

Uploaded by

Sajjad hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Arrays

Like other programming languages, array in C++ is a group of similar types of elements that
have contiguous memory location.

Advantages of Array

o Code Optimization (less code)


o Random Access
o Easy to traverse data
o Easy to manipulate data
o Easy to sort data etc.

Disadvantages of Array

o Fixed size

Array Types

There are 2 types of arrays in C++ programming:

1. Single Dimensional Array


2. Multidimensional Array

Single Dimensional Array

Let's see a simple example of C++ array, where we are going to create, initialize and traverse array.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
6. //traversing array
7. for (int i = 0; i < 5; i++)
8. {
9. cout<<arr[i]<<"\n";
} }

Page 1 of 4
Array Example: Traversal using for each loop

We can also traverse the array elements using for each loop. It returns array element one by one.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
6. //traversing array
7. for (int i: arr)
8. {
9. cout<<i<<"\n";
}
}

Passing Array to Function

In C++, to reuse the array logic, we can create function. To pass array to function in C++, we need
to provide only array name.

function_name(arrayname); //passing array to function

Passing Array to Function Example: print array elements

1. #include <iostream>
2. using namespace std;
3. void printArray(int arr[5]);
4. int main()
5. {
6. int arr1[5] = { 10, 20, 30, 40, 50 };
7. int arr2[5] = { 5, 15, 25, 35, 45 };
8. printArray(arr1); //passing array to function
9. printArray(arr2);
}

Page 2 of 4
void printArray(int arr[5])
{
cout << "Printing array elements:"<< endl;
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
}

Passing Array to Function Example: Print minimum number

1. #include <iostream>
2. using namespace std;
3. void printMin(int arr[5]);
4. int main()
5. {
6. int arr1[5] = { 30, 10, 20, 40, 50 };
7. int arr2[5] = { 5, 15, 25, 35, 45 };
8. printMin(arr1);//passing array to function
9. printMin(arr2);
}
void printMin(int arr[5])
{
int min = arr[0];
for (int i = 0; i > 5; i++)
{
if (min > arr[i])
{
min = arr[i];
}
}
cout<< "Minimum element is: "<< min <<"\n";
}

Page 3 of 4
Multidimensional Arrays

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int test[3][3]; //declaration of 2D array
6. test[0][0]=5; //initialization
7. test[0][1]=10;
8. test[1][1]=15;
9. test[1][2]=20;
test[2][0]=30;
test[2][2]=10;
//traversal
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout<< test[i][j]<<" ";
}
cout<<"\n"; //new line at each row
}
return 0;
}

Page 4 of 4

You might also like