C++

How to Declare an Array with double Data Type in C++?

In C++, an array seems like a collection of similar data types stored in the contiguous memory locations. We could access the entries of an array randomly by using array indices. We can also use an array to hold primitive data types for example int, float, etc., as well as derived data types like structures, pointers, etc. In this article, we will discuss how to declare an array with the double data type in C++

How to Declare an Array with double Data Type in C++

There are two ways to declare an array according to memory allocation:

How to Declare a Static Array with Double Data Type

Static arrays are kept in the memory stack of the program. As a result, we must decide the array size at compile time. In this type of array, the size is decided during the compile-time, which remains fixed, however its memory is allocated during the run-time.

The following is the syntax to declare a static array with double data type in C++:

datatype array_name[size]={elements};

In the above syntax, you must define the array datatype as double, name the array, set the array size, and add the elements inside the array.

The given piece of code describes the static declaration of an array with double data type in C++.

#include <iostream>

using namespace std;

int main()

{

  double my_array[7] = {1.25, 3.50, 4.75, 5.91234, 10.101010, 0.0001, 9.151515};

  for (int i; i<=6; i++)

  {

cout<< my_array[i]<< "\n";

  }

}

In the above program, double my_array[7] are the double type arrays having static memory allocation with fixed size during compilation. Here we used for loop to print every element of array in C++.

Output

How to Declare a Dynamic Array with Double Data Type

A dynamic array in C++ is an array whose size can be determined at runtime rather than at compile time. It is implemented using pointers and memory allocation functions like new and delete.

The following is the syntax to declare a dynamic array in C++:

datatype *array_name{ new datatype[size]{elements} };

Here, datatype refers to the data type of the elements that will be stored in the array. array_name is the name you choose for the array. size specifies the number of elements that the array will contain. The elements is an optional list of initial values for the array elements, and can be omitted if you don’t need to initialize the array at the time of allocation.

The new keyword allocates memory on the heap for the array, and returns a pointer to the first element of the array. This pointer is assigned to the array_name variable.

The given program indicates the declaration of a dynamically allocated array in C++.

#include <iostream>

using namespace std;

int main()

{

  double *my_array{ new double[10]{ 1.01, 0.0011, 1.53, 30.78, 11.99, 4.68, 3.33333, 7.845, 6.834, 0.00001 } };

  cout << "Elements in the dynamic array: " << endl;

  for (int i = 0; i < 10; i++)

  {

  cout << my_array[i] << endl;

  }

  return 0;

}

The above code declares a dynamic array of type double with 10 elements initialized with specific values and prints them using a for loop.

Output

Conclusion

Declaring an array with double data type in C++ can be achieved using both static and dynamic allocation methods. Static allocation may offer better performance and simpler code, but it may not be suitable for situations where the size of the array needs to be determined at runtime. Dynamic allocation, on the other hand, provides greater flexibility in terms of size and can be useful in situations where the size of the array is not known at compile time.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.