An array is a collection of variables of the same type stored contiguously in memory.

  • One-dimensional array is like a list of elements.

  • All elements have the same data type.

  • Arrays help to store multiple values in a single variable, instead of creating many separate variables.

Analogy: Think of an array as a row of boxes, where each box has a number (index) and stores a value.

Declaration of One-Dimensional Arrays

Syntax:

 
data_type array_name[size];
  • data_type → type of elements (int, float, char, etc.)

  • array_name → name of the array

  • size → number of elements

Examples:

 
int marks[5]; // Array of 5 integers
float temperatures[7]; // Array of 7 floats
char letters[4]; // Array of 4 characters

Important Notes:

  • Array index starts from 0 and goes to size-1.

  • Size must be a positive integer.

  • All elements are of the same data type.

Accessing Elements of an Array

  • Access elements using index number in square brackets [ ].

Syntax:

 
array_name[index]

Example:

 
#include <iostream>
using namespace std;
int main() {
int marks[5] = {80, 85, 90, 75, 95};
cout << marks[0] << endl; // Output: 80
cout << marks[4] << endl; // Output: 95
return 0;
}

Explanation:

  • marks[0] → first element

  • marks[4] → fifth element

  • Arrays are zero-indexed

Modifying Elements

 
marks[2] = 100; // Change third element

cout << marks[2]; // Output: 100

Examples of One-Dimensional Arrays

Example 1: Initialize and Print All Elements

 
int numbers[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++)
cout << numbers[i] << " ";

Output: 1 2 3 4 5

Example 2: Input Elements from User

 
int arr[3];
for(int i = 0; i < 3; i++) {
cout << "Enter value: ";
cin >> arr[i];
}

Example 3: Sum of Array Elements

 
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++)
sum += arr[i];
cout << "Sum = " << sum; // Output: Sum = 100

Example 4: Find Maximum Element

 
int arr[5] = {10, 50, 20, 70, 40};
int max = arr[0];
for(int i = 1; i < 5; i++)
if(arr[i] > max)
max = arr[i];
cout << "Maximum = " << max; // Output: Maximum = 70

Example 5: Find Minimum Element

 
int arr[5] = {10, 50, 20, 70, 40};
int min = arr[0];
for(int i = 1; i < 5; i++)
if(arr[i] < min)
min = arr[i];
cout << "Minimum = " << min; // Output: Minimum = 10

Example 6: Average of Array Elements

 
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < 5; i++)
sum += arr[i];
float avg = sum / 5.0;
cout << "Average = " << avg; // Output: Average = 30

Example 7: Reverse an Array

 
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 4; i >= 0; i--)
cout << arr[i] << " ";

Output: 5 4 3 2 1

Example 8: Count Positive Numbers

 
int arr[6] = {-1, 2, -3, 4, 5, -6};
int count = 0;
for(int i = 0; i < 6; i++)
if(arr[i] > 0)
count++;
cout << "Positive numbers = " << count; // Output: 3

Example 9: Check if Array Contains a Value

 
int arr[5] = {10, 20, 30, 40, 50};
int value = 30;
bool found = false;
for(int i = 0; i < 5; i++)
if(arr[i] == value)
found = true;
cout << (found ? "Found" : "Not Found"); // Output: Found

Example 10: Copy Array to Another Array

 
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5];
for(int i = 0; i < 5; i++)
arr2[i] = arr1[i];

Example 11: Multiply Each Element by 2

 
int arr[4] = {1, 2, 3, 4};
for(int i = 0; i < 4; i++)
arr[i] *= 2;

Example 12: Print Even Elements

 
int arr[6] = {1,2,3,4,5,6};
for(int i = 0; i < 6; i++)
if(arr[i] % 2 == 0)
cout << arr[i] << " ";

Output: 2 4 6

Example 13: Print Odd Elements

 
for(int i = 0; i < 6; i++)
if(arr[i] % 2 != 0)
cout << arr[i] << " ";

Output: 1 3 5

Example 14: Sum of Even and Odd Separately

 
int arr[5] = {1,2,3,4,5};
int sumEven = 0, sumOdd = 0;
for(int i = 0; i < 5; i++) {
if(arr[i] % 2 == 0)
sumEven += arr[i];
else
sumOdd += arr[i];
}
cout << "Even sum = " << sumEven << ", Odd sum = " << sumOdd;

Output: Even sum = 6, Odd sum = 9

Example 15: Shift Elements Right

 
int arr[5] = {1,2,3,4,5};
int last = arr[4];
for(int i = 4; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = last;

Output: Array becomes 5,1,2,3,4

Example 16: Shift Elements Left

 
int first = arr[0];
for(int i = 0; i < 4; i++)
arr[i] = arr[i+1];
arr[4] = first;

Output: Array becomes 2,3,4,5,1

Example 17: Merge Two Arrays

 
int arr1[3] = {1,2,3};
int arr2[2] = {4,5};
int arr3[5];
for(int i=0;i<3;i++) arr3[i]=arr1[i];
for(int i=0;i<2;i++) arr3[i+3]=arr2[i];

Example 18: Find Second Largest Element

 
int arr[5] = {10,20,30,40,50};
int max1 = arr[0], max2 = arr[0];
for(int i = 1;i<5;i++){
if(arr[i]>max1){
max2=max1;
max1=arr[i];
}
else if(arr[i]>max2)
max2=arr[i];
}
cout << "Second largest = " << max2; // Output: 40

Example 19: Count Frequency of a Value

 
int arr[6] = {1,2,2,3,2,4};
int value=2, count=0;
for(int i=0;i<6;i++)
if(arr[i]==value)
count++;
cout << "Frequency of 2 = " << count; // Output: 3

Example 20: Remove Duplicate Elements

 
int arr[5]={1,2,2,3,1};
int size=5;
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++){
if(arr[i]==arr[j]){
for(int k=j;k<size-1;k++)
arr[k]=arr[k+1];
size--; j--;
}
}
}

Array becomes: 1,2,3

Extra Details About One-Dimensional Arrays

  • Arrays are zero-indexed

  • Size must be known at compile time in traditional C++ arrays

  • Use loops to process arrays efficiently

  • Arrays can be passed to functions (by value or reference)

  • Arrays can store integers, floats, doubles, chars, strings

  • For dynamic size, use vectors instead of arrays

Best Practices

  • Always initialize arrays

  • Use loops for processing, not manual access

  • Avoid out-of-bounds access

  • Prefer constants for array size to make code maintainable

  • Use functions for repetitive array tasks (sum, max, min, search)