C++ Arrays in Data Structures:

 data structures

Arrays are a fundamental concept in programming, especially in C++. They allow us to store multiple values of the same type in a single variable, making data management more efficient. This guide will walk you through the basics of arrays in C++, including their syntax, usage, and common operations.


📦 What Is an Array?

An array is a collection of elements, all of the same type, stored in contiguous memory locations. Think of it as a row of boxes, each holding a value, and each box is identified by an index. In C++, arrays are zero-indexed, meaning the first element is at index 0.


🛠️ Declaring and Initializing Arrays

Declaration

To declare an array in C++, specify the data type, array name, and size:


data_type array_name[array_size];

For example, to store marks for 5 students:


int marks[5];

Initialization

You can initialize an array at the time of declaration:


int marks[5] = {85, 90, 78, 92, 88};

If you omit the size, the compiler determines it based on the initializer:W3Schools.com+8Programiz+8GeeksforGeeks+8


int marks[] = {85, 90, 78, 92, 88};

Each element can be accessed using its index:


cout << marks[0]; // Outputs 85


🔄 Iterating Through Arrays

Loops are commonly used to process arrays. Here's how to print all elements:


for (int i = 0; i < 5; i++) { cout << marks[i] << endl; }

In C++11 and later, you can use range-based for loops:


for (int mark : marks) { cout << mark << endl; }


🔍 Finding Minimum and Maximum Values

To find the smallest and largest elements:W3Schools.com+3Programiz+3YouTube+3


int smallest = marks[0]; int largest = marks[0]; for (int i = 1; i < 5; i++) { if (marks[i] < smallest) { smallest = marks[i]; } if (marks[i] > largest) { largest = marks[i]; } }


🔁 Reversing an Array

To reverse the elements of an array:


int start = 0; int end = 4; // For an array of size 5 while (start < end) { int temp = marks[start]; marks[start] = marks[end]; marks[end] = temp; start++; end--; }


🔎 Linear Search

To search for a specific value:


int target = 90; bool found = false; for (int i = 0; i < 5; i++) { if (marks[i] == target) { found = true; break; } }


📤 Passing Arrays to Functions

Arrays are passed by reference to functions, allowing modifications to affect the original array:


void doubleMarks(int arr[], int size) { for (int i = 0; i < size; i++) { arr[i] *= 2; } }

Calling doubleMarks(marks, 5); will double each element in the marks array.


🧠 Summary

  • Declaration: Define the type, name, and size.

  • Initialization: Assign values at declaration or later.

  • Access: Use indices starting from 0.

  • Iteration: Use loops to process elements.

  • Operations: Search, find min/max, reverse, and modify elements.

  • Functions: Arrays are passed by reference.W3Schools.com+3W3Schools.com+3Cplusplus.com+3


📝 Practice Exercise

Create an array of 10 integers. Implement functions to:Cplusplus.com+3Microsoft Learn+3Learn C+++3

  1. Find the sum of all elements.

  2. Find the average value.

  3. Identify and print all even numbers.

  4. Reverse the array.

This exercise will reinforce your understanding of arrays and their manipulation in C++.

Comments