C++

How to print an array in C++

The array is the group of elements of a similar type that are placed in contiguous memory locations. The main thing in the array is it can be referenced with the help of an index to a unique identifier. We can declare the array as our own choice. When we declare the array, the elements field within the square brackets. The syntax of the array in C++ is data type variable name [rang] = {elements}, range could be defined automatically if we assign elements. In this case, you can leave square brackets empty where the type is valid, like integer and float. These elements start from zero to so on. The obvious thing is we must declare before its use.

By default, the initialization of the array is from left to right. We can say that none of its elements could be set as any particular location of the memory of the array. After setting the range or element of the array, we can give values after the equal sign in the curly braces {}. We can explicitly initialize specific values when we declare them. The number of values shall not be greater than the range that we set as a range of the array.

Insert and print array:

Here we show you how we simply initialize, insert and print an array. We can access the value of the array just like we access the simple variable of the identical data type. If we exceed the limit of the array, there is no error in compile-time, but it can cause a runtime error.

#include <iostream>

using namespace std;
int a [] = {4, 8, 16};
int main ()
{
cout << a[0]<<endl;
cout << a[1]<<endl;
cout << a[2]<<endl;
return 0;
}

Here add our input-output stream and add namespace standards. Then we initialize an integer array with the name of ‘a’ and assign it some values. In the main body of the code, we simply display the array with its indexes. To make our output readable, we print every value to a new line with the help of the endl statement.

Print array with loop:

In the above example, we use a cout statement for every index that makes our code lengthy and takes space in the memory. We use the loop to cout our array; this makes our code short and saves our time and space.

#include <iostream>
using namespace std;
int arr [10] = {12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
int main ()
{
  for (int i=0 ; i<10 ; i++ )
  {
    cout << arr[i]<<"\t";
  }
  return 0;
}

Now we can see that we initialized a long array with the length of 10 and assigned members at each index. Then we write a loop, and the limit of the loop is the same as the limit of the array in the main body of the code. In the loop, we just write the cout statement along with the endl and display each member of the array that starts from zero until the condition is false.

Get value and print array:

As we know that in programming, there are a lot of problems to solve, so we need something that has versatility in our development. The array can allow us to enter your value. That array will store it in its indexes, and we can use these values according to our choice or condition.

#include <iostream>
using namespace std;
int main()
{
   int b[5];
   for (int i = 0; i < 5; i++)
   {
      cout << "Enter Value for index " << i <> b[i];
   }
   cout << "\n You Entered\n";
   for (int i = 0; i < 5; i++)
   {
      cout << "At index : " << i << " ,Value is : " << b[i] << " \n";
   }
   return 0;
}

Here we include our library and namespace and start the main body of the program. In our main function, we initialized our array with the data type of integer. After that, we start our loop and ask the user to enter the values at every loop index. We save these values in their respective indexes. Then we start another loop to display the values that we entered in the earlier loop.

Get the size and value, then print array:

As we said above, the array gives us many facilities to make us comfortable while coding. Here we talk that we can also define the size of our array. To save our memory at run time. If we don’t know the size while coding, you can just empty the array and ask the user to set the size at run time.

#include <iostream>
using namespace std;
int main()
{
    int size=0;
    cout<>size;
    cout<<endl;
   int myarr[size];
   for (int i = 0; i < size; i++)
   {
      cout << "Enter Value at index " << i <> myarr[i];
   }
   cout << "\n You Entered\n";
   for (int i = 0; i < size; i++)
   {
      cout << myarr[i] << " \t";
   }
   return 0;
}

As you see in this example, after the protocols of the code, we start our main body and initialize a variable with the data type of integer. After taking the value from the user, we store this variable. Then we assign this value as the size of the array. After that, we start the loop to get values of the array from the user and store them at their indexes. Quickly after that, we use another loop to display our value, and we use “\t” to enter a tab between the value and them separate from others.

Print 2D array:

We discuss now the liner or 1D, which is a one-dimension array. Here we discuss the other and main type of array that is called a 2D array or two-dimension array. This array is just like a matrix, and we enter our values at its indexes. That is how it has to index: one is from left to right or in a row; the second is from up to down or in the column.

The syntax of the 2D array in C++ is data type variable name [rang] [range] = {{element, element}, {element, element}}. Now let’s go to the example.

#include <iostream>
using namespace std;
int main()
{
    int two_D_arr[2][2]={{2,4},{6,8}};
    cout<<"value at 0,0 = "<<two_D_arr[0][0]<<endl;
    cout<<"value at 0,1 = "<<two_D_arr[0][1]<<endl;
    cout<<"value at 1,0 = "<<two_D_arr[1][0]<<endl;
    cout<<"value at 1,1 = "<<two_D_arr[1][1]<<endl;
    return 0;

Here we can see there is no difficult thing in this code; we just simply initialized an integer 2D array. You can say we take a matrix of 2×2. Then assign values to this array. After that, we just print these arrays, and you can see the values on their respective indexes.

Conclusion:

This article defines the array and briefly discusses all its basic features. Also, we study how many ways we can read and write arrays in the code. Then we describe the main type of array, a 2D array, and then we explain how we can display it in multiple ways with the help of different examples.

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.