Arrays are one of the most fundamental data structures in C++ programming. An array allows you to store multiple values of the same data type under a single variable name. Instead of creating separate variables for each value, arrays organize data efficiently using indexes.
For example, instead of writing:
int marks1 = 85;
int marks2 = 90;
int marks3 = 78;
int marks4 = 88;
int marks5 = 95;
You can simply write:
int marks[5] = {85, 90, 78, 88, 95};
Each element in an array has a unique index starting from 0.
Arrays are widely used in:
- Competitive Programming
- Data Structures & Algorithms (DSA)
- Sorting Algorithms
- Searching Algorithms
- Matrix Operations
- Game Development
- Software Development
In this chapter, you’ll solve beginner-friendly and interview-oriented array problems to strengthen your understanding of array traversal, searching, calculations, and basic manipulation. C++ Arrays practice questions with solutions help to build concepts.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Input
- Sample Output
- Explanation
- Concepts Covered
Let’s begin with the first five array practice questions.
1. C++ Program to Read and Print Array Elements
Problem Statement
Write a C++ program to read 5 integers from the user and display them using an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
cout << "\nArray Elements:\n";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
return 0;
}
Sample Input
Enter 5 numbers:
12
25
38
41
59
Sample Output
Array Elements:
12 25 38 41 59
Explanation
The first loop stores the values in the array, while the second loop displays all elements one by one.
Concepts Covered
- One-Dimensional Array
- Array Input
- Array Output
- for Loop
2. C++ Program to Find the Sum of Array Elements
Problem Statement
Write a C++ program to calculate the sum of all elements in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int sum = 0;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum;
return 0;
}
Sample Input
Enter 5 numbers:
10
20
30
40
50
Sample Output
Sum = 150
Explanation
Each array element is added to the variable sum during input.
Concepts Covered
- Array Traversal
- Accumulator Variable
- for Loop
3. C++ Program to Find the Average of Array Elements
Problem Statement
Write a C++ program to calculate the average of elements stored in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int sum = 0;
float average;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
sum += numbers[i];
}
average = (float)sum / 5;
cout << "Average = "
<< average;
return 0;
}
Sample Input
Enter 5 numbers:
10
20
30
40
50
Sample Output
Average = 30
Explanation
The average is calculated by dividing the total sum by the number of elements.
Concepts Covered
- Array Traversal
- Average Calculation
- Type Casting
4. C++ Program to Find the Largest Element in an Array
Problem Statement
Write a C++ program to find the largest element in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
int largest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
cout << "Largest Element = "
<< largest;
return 0;
}
Sample Input
Enter 5 numbers:
12
98
45
67
81
Sample Output
Largest Element = 98
Explanation
The program assumes the first element is the largest and updates it whenever a bigger element is found.
Concepts Covered
- Maximum Element
- Array Traversal
- Comparison Operators
5. C++ Program to Find the Smallest Element in an Array
Problem Statement
Write a C++ program to find the smallest element in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
int smallest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] < smallest)
{
smallest = numbers[i];
}
}
cout << "Smallest Element = "
<< smallest;
return 0;
}
Sample Input
Enter 5 numbers:
45
12
78
23
91
Sample Output
Smallest Element = 12
Explanation
The program compares each element with the current smallest value and updates it whenever a smaller element is found.
Concepts Covered
- Minimum Element
- Array Traversal
- Comparison Operators
6. C++ Program to Search an Element in an Array
Problem Statement
Write a C++ program to search for a given element in an array using Linear Search.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int search;
bool found = false;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
cout << "Enter element to search: ";
cin >> search;
for (int i = 0; i < 5; i++)
{
if (numbers[i] == search)
{
found = true;
break;
}
}
if (found)
cout << "Element Found";
else
cout << "Element Not Found";
return 0;
}
Sample Input
Enter 5 numbers:
15
28
36
42
50
Enter element to search:
36
Sample Output
Element Found
Explanation
The program checks each element one by one until the required element is found.
Concepts Covered
- Linear Search
- Boolean Variable
- Array Traversal
- for Loop
7. C++ Program to Count Even and Odd Numbers in an Array
Problem Statement
Write a C++ program to count the total number of even and odd elements in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int even = 0;
int odd = 0;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
if (numbers[i] % 2 == 0)
even++;
else
odd++;
}
cout << "Even Numbers = "
<< even << endl;
cout << "Odd Numbers = "
<< odd;
return 0;
}
Sample Input
Enter 5 numbers:
10
15
20
25
30
Sample Output
Even Numbers = 3
Odd Numbers = 2
Explanation
Each array element is checked using the modulus (%) operator to determine whether it is even or odd.
Concepts Covered
- Array Traversal
- Even/Odd Logic
- Counter Variables
8. C++ Program to Reverse an Array
Problem Statement
Write a C++ program to display the elements of an array in reverse order.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
cout << "Reversed Array:\n";
for (int i = 4; i >= 0; i--)
{
cout << numbers[i] << " ";
}
return 0;
}
Sample Input
Enter 5 numbers:
10
20
30
40
50
Sample Output
Reversed Array:
50 40 30 20 10
Explanation
The array is traversed from the last index to the first index.
Concepts Covered
- Reverse Traversal
- Array Indexing
- for Loop
9. C++ Program to Copy One Array into Another
Problem Statement
Write a C++ program to copy all elements from one array into another array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int source[5];
int destination[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> source[i];
}
for (int i = 0; i < 5; i++)
{
destination[i] = source[i];
}
cout << "\nCopied Array:\n";
for (int i = 0; i < 5; i++)
{
cout << destination[i] << " ";
}
return 0;
}
Sample Input
Enter 5 numbers:
2
4
6
8
10
Sample Output
Copied Array:
2 4 6 8 10
Explanation
Each element from the source array is assigned to the corresponding index in the destination array.
Concepts Covered
- Array Copy
- Assignment Operator
- Array Traversal
10. C++ Program to Find the Second Largest Element in an Array
Problem Statement
Write a C++ program to find the second largest element in an array.
C++ Solution
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int numbers[5];
int largest = INT_MIN;
int secondLargest = INT_MIN;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
if (numbers[i] > largest)
{
secondLargest = largest;
largest = numbers[i];
}
else if (numbers[i] > secondLargest && numbers[i] != largest)
{
secondLargest = numbers[i];
}
}
cout << "Second Largest Element = "
<< secondLargest;
return 0;
}
Sample Input
Enter 5 numbers:
12
98
45
76
89
Sample Output
Second Largest Element = 89
Explanation
The program keeps track of both the largest and second-largest values while traversing the array only once.
Concepts Covered
- Maximum Value Tracking
- Array Traversal
- Comparison Operators
INT_MIN
11. C++ Program to Find the Second Smallest Element in an Array
Problem Statement
Write a C++ program to find the second smallest element in an array.
C++ Solution
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int numbers[5];
int smallest = INT_MAX;
int secondSmallest = INT_MAX;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
if (numbers[i] < smallest)
{
secondSmallest = smallest;
smallest = numbers[i];
}
else if (numbers[i] < secondSmallest && numbers[i] != smallest)
{
secondSmallest = numbers[i];
}
}
cout << "Second Smallest Element = "
<< secondSmallest;
return 0;
}
Sample Input
Enter 5 numbers:
25
10
45
5
30
Sample Output
Second Smallest Element = 10
Explanation
The program maintains both the smallest and second smallest values while traversing the array only once.
Concepts Covered
- Array Traversal
- Minimum Element
- Comparison Operators
INT_MAX
12. C++ Program to Count Positive, Negative and Zero Elements in an Array
Problem Statement
Write a C++ program to count the number of positive, negative, and zero values in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int positive = 0;
int negative = 0;
int zero = 0;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
if (numbers[i] > 0)
positive++;
else if (numbers[i] < 0)
negative++;
else
zero++;
}
cout << "Positive Numbers = "
<< positive << endl;
cout << "Negative Numbers = "
<< negative << endl;
cout << "Zero Values = "
<< zero;
return 0;
}
Sample Input
Enter 5 numbers:
10
-5
0
20
-8
Sample Output
Positive Numbers = 2
Negative Numbers = 2
Zero Values = 1
Explanation
Each element is checked individually and counted according to its sign.
Concepts Covered
- Arrays
- Conditional Statements
- Counter Variables
13. C++ Program to Merge Two Arrays
Problem Statement
Write a C++ program to merge two arrays into a single array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int first[5], second[5], merged[10];
cout << "Enter 5 elements of first array:\n";
for (int i = 0; i < 5; i++)
{
cin >> first[i];
merged[i] = first[i];
}
cout << "Enter 5 elements of second array:\n";
for (int i = 0; i < 5; i++)
{
cin >> second[i];
merged[i + 5] = second[i];
}
cout << "\nMerged Array:\n";
for (int i = 0; i < 10; i++)
{
cout << merged[i] << " ";
}
return 0;
}
Sample Input
First Array:
1 2 3 4 5
Second Array:
6 7 8 9 10
Sample Output
Merged Array:
1 2 3 4 5 6 7 8 9 10
Explanation
The elements of both arrays are copied sequentially into a third array.
Concepts Covered
- Array Copy
- Merging Arrays
- Array Traversal
14. C++ Program to Find Duplicate Elements in an Array
Problem Statement
Write a C++ program to display duplicate elements present in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[6];
cout << "Enter 6 numbers:\n";
for (int i = 0; i < 6; i++)
{
cin >> numbers[i];
}
cout << "Duplicate Elements:\n";
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 6; j++)
{
if (numbers[i] == numbers[j])
{
cout << numbers[i] << " ";
break;
}
}
}
return 0;
}
Sample Input
Enter 6 numbers:
5
8
3
5
2
8
Sample Output
Duplicate Elements:
5 8
Explanation
The program compares every element with the remaining elements to identify duplicates.
Concepts Covered
- Nested Loops
- Array Comparison
- Duplicate Detection
15. C++ Program to Sort an Array in Ascending Order
Problem Statement
Write a C++ program to sort an array in ascending order.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int temp;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++)
{
if (numbers[i] > numbers[j])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
cout << "Sorted Array:\n";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
return 0;
}
Sample Input
Enter 5 numbers:
45
12
78
5
30
Sample Output
Sorted Array:
5 12 30 45 78
Explanation
The program uses a simple comparison-based sorting approach to arrange the elements in ascending order.
Concepts Covered
- Sorting
- Nested Loops
- Swapping
- Array Traversal
Chapter Summary
In this chapter, you learned how to work with one-dimensional arrays in C++. You practiced storing and displaying array elements, calculating sums and averages, finding the largest and smallest values, searching for elements, reversing arrays, copying arrays, merging arrays, detecting duplicates, and sorting data. Arrays are the foundation of many advanced data structures and algorithms, making them an essential topic for every C++ programmer.
Key Takeaways
- Arrays store multiple values of the same data type in contiguous memory.
- Array indexing starts from 0 in C++.
- Loops are commonly used to traverse arrays.
- Arrays simplify handling collections of data.
- Searching and sorting are fundamental array operations.
- Nested loops are useful for comparing array elements.
- Arrays are widely used in DSA, competitive programming, and software development.
- Understanding arrays is essential before learning pointers, vectors, and dynamic memory allocation.
- Efficient array manipulation improves programming skills.
- Arrays form the basis for many advanced algorithms.
Frequently Asked Questions (FAQs)
1. What is an array in C++?
An array is a collection of elements of the same data type stored in contiguous memory locations.
2. Why do array indexes start from 0?
C++ uses zero-based indexing, meaning the first element is stored at index 0, the second at 1, and so on.
3. Can an array store different data types?
No. A standard array can only store elements of the same data type.
4. What is array traversal?
Array traversal means visiting each element of an array, usually with a loop.
5. Which loop is commonly used with arrays?
The for loop is the most commonly used loop for processing arrays because the number of elements is usually known.
6. What is the difference between searching and sorting?
Searching locates a specific element, while sorting arranges elements in a specific order such as ascending or descending.
7. What are multidimensional arrays?
Multidimensional arrays are arrays that contain other arrays, such as two-dimensional arrays used for matrices.
8. Why are arrays important in Data Structures and Algorithms (DSA)?
Arrays provide fast indexed access to data and are the foundation for many advanced data structures like stacks, queues, heaps, hash tables, and dynamic arrays.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
