C++

C++ Initialize Array

“A collection of related data objects stored at adjacent memory locations and accessible at random using an array’s indices is referred to as an array in C++ or any other programming language. When there are few objects, we can use ordinary variables (var1, var2, var3,..), but when there are many items and many instances, managing them with regular variables becomes challenging. An array is designed to hold several instances in a single variable.”

Advantages of the Array in C++

  • Employing the array index to provide random access to elements.
  • Fewer lines of code are needed because a single array with many elements is created.
  • All the elements are easily accessible.
  • A single loop makes it simple to traverse the array.
  • Sorting gets simple because there are fewer lines of code needed to complete it.

Disadvantages of the Array in C++

  • Permits entering a predetermined, fixed number of elements that are chosen at the time of declaration. A C array is not dynamic, as opposed to a linked list.
  • Due to the necessity to manage the elements in line with the new memory allocation, element insertion and deletion can be expensive.

How to Declare an Array in C++

The method used to create an array is identical to that used to create variables. Declaring the array comes first. After declaring the array, we have the option of initializing it immediately or later. We must mention three items when declaring the array: the array’s name, data type, size, and other details. The array’s name also serves as a pointer to its very first component.

Example 1

In this example, two integer arrays are initialized, one with a size that was not provided during creation and the other with a size that was specified, but fewer elements were initialized.

#include <iostream>

using namespace std;

int main() {
   int Arr_1[] = {2, 9, 6, 3, 2};
   int Arr_2[10] = {12, 68, 28, 32, 76, 49, 84};

   for (int i=0; i<sizeof(Arr_1)/sizeof(*Arr_1); i++) {
cout<< Arr_1[i] <<"  ";
   }
cout<<endl;
   for (int i=0; i<sizeof(Arr_2)/sizeof(*Arr_2); i++) {
cout<< Arr_2[i] <<"  ";
   }
}

Initially, we created an array as “Arr_1” with an empty size and initialized it with five integers. After that, we declared another array with the size given as “10”. The elements are inserted in the array with the specified size of an array. Then, with the for loop, we have printed the elements of both the arrays “Arr_1” and “Arr_2,” respectively. Note that in the for loop, we have a conditional statement that “i” should be less than the size of the given array.

The two different arrays are generated in the output. The first array elements are the same as in the array. The second array shows three zero in last, as we have only inserted seven elements and the size of the array given is “10”.

Example 2

We can assign an element list to the array and specify its size. A list’s elements could be fewer than the allotted number in this case. After that, the array is created by the compiler with the specified size, and the elements are assigned one by one, starting at the beginning of the array.

#include <iostream>

using namespace std;

int main() {
  int nums[5] = {3, 7, 1, 52, 25};
cout<< "The integer elements are: ";
  for (const int &ele :nums) {
cout<<ele<<"  ";
  }
cout<< "\nThe integer elements are: ";
  for (int i = 0; i< 5; ++i) {
cout<<nums[i] <<"  ";
  }
  return 0;
}

A size of “5” has been specified for the array that we have declared to be “nums.” Inside the array, we have added elements that are less than the array’s total size. Note that we have occupied four indexes in the array as the array index starts from zero. Then, we used a range-based for loop to print array elements. Const int &ele has been substituted for int “ele” as the range declaration in our range-based loop. The const int &ele is preferred, nonetheless, for the following reasons:

For each iteration when using int ele, the elements of the array are simply copied to the variable “ele”. However, instead of transferring the data to a new variable, “&ele” accesses the array items’ memory addresses directly. It uses little memory. The elements of the array are not being changed; we are just printing them. Const is used; as a result to prevent unintentionally changing the array’s values. After that, we utilize the conventional for loop method for printing the elements of the array.

Both the for loop methods displayed the same array element in the output:

Example 3

Previously, we have given examples of an integer. Now, in this example, the string array is created and displayed.

#include <iostream>

using namespace std;

int main() {

   string A1[] = {"live" , "long", "life" , "bye"};
   string A2[10] = {"Hi" , "my", "linux" , "geeks", "good", "luck", "user"};
   for (int i=0; i<sizeof(A1)/sizeof(*A1); i++) {
cout<< A1[i] <<"  ";
   }
cout<<endl;
   for (int i=0; i<sizeof(A2)/sizeof(*A2); i++) {
cout<< A2[i] <<"  ";
   }
}

We have defined an array as “Arr1” and declared it with string elements. After that, we defined our second array and inserted elements with the given array size. Then, with the for loop method, we printed the array elements.

The output generates the following array of string elements.

Example 4

In this example, we have taken numbers from the users and stored them in an array.

#include <iostream>

using namespace std;

int main() {

  int arr[5];

cout<< "input five numbers: " <<endl;
  for (int i = 0; i>arr[i];
  }
cout<< "The integers are: ";
  for (int n = 0; n < 5; ++n) {
cout<<arr[n] <<"  ";
  }
return 0;
}

We have created the array as “arr,” and the size given to the array is five. The user is then prompted to enter five integers in an array using the cout statement, which was printed after that. They have once more iterated from n= 0 to n= 4 using a for loop. We collected a user input for each iteration and stored it as an arr[n]. Then, to print every element of the array, we again used a for loop.

The output of the array element is displayed here after getting the input from the user.

Conclusion

The array is regarded as a crucial component in C++ since it facilitates memory management and increases program effectiveness. Due to its capability to provide multidimensional data storage, it can be utilized in a variety of techniques. When it is essential to store elements of the same data type, it is always best to use an array.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.