how to declare 1-D array in C/C++

To declare a 1-D array in C/C++, you can follow the steps below:

  1. Choose a data type: Decide on the data type that you want your array to hold. This can be any valid data type in C/C++, such as int, float, char, etc.

  2. Specify the array name: Choose a name for your array. This name will be used to access and manipulate the elements of the array.

  3. Define the array size: Determine the number of elements that the array will store. This will define the size of the array.

  4. Declare the array: Use the chosen data type, array name, and array size to declare the array. The general syntax for declaring a 1-D array is:

c++ data_type array_name[array_size];

For example, to declare an integer array named "myArray" with a size of 5, you would write:

c++ int myArray[5];

This declares an integer array named "myArray" that can hold 5 elements.

  1. Accessing array elements: You can access individual elements of the array using their index. Array indices start from 0 and go up to (array_size - 1). For example, to access the first element of "myArray", you would write:

c++ myArray[0] = 10;

This assigns the value 10 to the first element of "myArray".

Similarly, you can access and manipulate other elements of the array using their respective indices.

That's it! You have successfully declared a 1-D array in C/C++ and accessed its elements. You can now use this array for storing and retrieving data as needed.