C++ Single Dimensional Array

Hi everyone, inside this article we will see about C++ Single Dimensional Array.

In C++, an array is a collection of similar data types that are stored together in a contiguous block of memory. A single dimensional array is an array that consists of a single row of elements.

A Basic Example

Here is an example program that demonstrates how to declare and initialize a single dimensional array in C++:

#include <iostream>

using namespace std;

int main() {
  // Declare an array of 5 integers
  int numbers[5];

  // Initialize the elements of the array
  for (int i = 0; i < 5; i++) {
    numbers[i] = i + 1;
  }

  // Print the elements of the array
  for (int i = 0; i < 5; i++) {
    cout << "numbers[" << i << "] = " << numbers[i] << endl;
  }

  return 0;
}

In this program, we declare an array of 5 integers called numbers. We then initialize the elements of the array using a for loop. Finally, we print the elements of the array using another for loop.

The output of this program will be:

numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5

In this example, we initialized the elements of the array to sequential numbers starting from 1. However, you can initialize the elements to any values you want, such as user input or values calculated by your program.

In C++, a single dimensional array is a collection of similar data types that are stored together in a contiguous block of memory.

Properties of C++ Single Dimensional Array

Here are some of the key properties of a single dimensional array in C++:

Homogeneous: A single dimensional array is homogeneous, meaning all elements of the array must be of the same data type.

Fixed Size: The size of a single dimensional array is fixed at the time of declaration, meaning the number of elements in the array cannot be changed during the execution of the program.

Contiguous Memory Allocation: The elements of a single dimensional array are stored in contiguous memory locations, meaning each element is adjacent to the previous and next elements in memory.

Indexed Access: The elements of a single dimensional array are accessed using an index, starting from 0 for the first element and incrementing by 1 for each subsequent element.

Easy Traversal: The elements of a single dimensional array can be traversed easily using a for loop or a while loop.

Time Complexity: The time complexity for accessing an element in a single dimensional array is constant, O(1), because the index can be used to access the element directly.

Direct Memory Access: In C++, a single dimensional array provides direct access to the memory location of its elements, which allows for fast and efficient memory access.

These properties make single dimensional arrays a useful data structure for storing and manipulating collections of related data in C++.

We hope this article helped you to understand about C++ Single Dimensional Array in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.