Initialization of One Dimensional Array
The syntax for declaration of One Dimensional Array is as follows:
data_type array_name[array_size]= {value_1, value_2, value_3,......., value_n};
Example:
int marks[5] ={88, 90, 95, 87, 99};
It is also possible to not provide the array size when we initialize the array in the function declaration itself. In this case, the compiler calculates the size of the array.
As in the above example, the following would also be correct.
int marks[ ]= {88, 90, 95, 87, 99};
Functions & Array
Moving forward with arrays and their use in the program, let us understand how arrays are dealt with in a function. Two possible scenarios that arise here are:
- Function Declaration and Definition with array as a parameter
- Calling function with arrays as an argument
Function Declaration with Array as a Parameter:
First, we shall discuss how functions are declared and defined in C++ with arrays as parameters. We can pass arrays into function as parameters for further use in it.
The syntax to declare such functions is:
Function_data_type function_name (data_type array_name [size]);
Example 1
int total (int marks[100]);
We can then define the function as we generally do.
#include <iostream>
using namespace std;
int total (int marks[])
{
// finds a random value between 0 and 100
int p= random()%100;
// the index value at position p of the array is displayed
return marks[p];
}
Hence, we can pass an array as a parameter in the above function. This code block illustrates how the concepts we have discussed are implemented.
Function Calling with Array as a Parameter:
The function call contains the function’s argument, and hence, it must be chosen properly and declared so that then we can pass the array correctly. In this case, we need to pass the value of a memory address only.
Syntax: Function_data_type function_name (data_type array_name [size]);
Example 1
#include <bits/stdc++.h>
using namespace std;
int addElementsOfArray(int arr[], int N) // Passing array and size of array as parameter
{
int sum = 0;
for (int i = 0; i < N; i++)
{
sum = sum += arr[i]; // Adding elements of array to sum variable
}
return sum;
}
int main()
{
int N = 10;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "Sum of all the array elements is equal to: " << addElementsOfArray(arr, N);
return 0;
}
Output
Sum of all the array elements is equal to: 55
Here, we can even pass an array by reference by using the pointer, but the notable thing here is arrays are by default passed by reference in C++. Whatever changes we will make to array elements inside the function will be reflected array itself.
Example 2
#include <bits/stdc++.h>
using namespace std;
void modifyElementsOfArray(int arr[], int N) // Passing array and size of array as parameter
{
int sum = 0;
for (int i = 0; i < N; i++)
{
arr[i] += 1; // Adding 1 to all elements of array.
}
}
int main()
{
int N = 10;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "Array without modification: ";
for (int i = 0; i < N; i++)
{
cout << arr[i] << " ";
}
cout << endl;
cout << "Array with modification: ";
modifyElementsOfArray(arr, N); // calling the modifyELementsofArray function
for (int i = 0; i < N; i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output:
Array without modification: 1 2 3 4 5 6 7 8 9 10
Array with modification: 2 3 4 5 6 7 8 9 10 11
Try and compile with online c++ compiler.
FAQs
-
How can we implement arrays dynamically in C++?
We can use vectors for the implementation of dynamic arrays in C++.
-
Do we use call by value or call by reference in C++?
We don’t need to use call-by-reference because arrays are passed by reference in C++ by default, even if we call them by value.
-
Is there any difference between a declaration of the array as int[ ] a and int a[ ]?
There is no difference between these two types of declaration, both are legal statements.
Key Takeaways:
The above blog taught us about arrays, mainly Single Dimension Array. We analyzed how the single-dimensional arrays are declared, initialized, and used within a program. Furthermore, sum of two arrays & the use of arrays within a function as a parameter and an argument was discussed along with their code implementation. For learning more about such programming concepts, visit here.
Recommended Readings: