Arrays help us store many values of the same type together in one place. This makes it easy to organize and use data efficiently.
Why arrays are needed in C++
class ArrayExample { public: int elements[5]; // array of 5 integers void setValues() { elements[0] = 10; elements[1] = 20; elements[2] = 30; elements[3] = 40; elements[4] = 50; } void printValues() { for (int i = 0; i < 5; i++) { std::cout << elements[i] << " "; } std::cout << std::endl; } };
Arrays store multiple values of the same type in a fixed size.
Each value is accessed by its position, starting from 0.
int numbers[3]; // array with 3 integers numbers[0] = 5; numbers[1] = 10; numbers[2] = 15;
int emptyArray[0]; // array with zero size (not useful but valid in some compilers)
int singleElement[1] = {100}; // array with one element
int lastElement = numbers[2]; // access last element in a 3-element array
This program creates an array of 5 integers inside a class, sets values, and prints them.
#include <iostream> class ArrayExample { public: int elements[5]; void setValues() { elements[0] = 10; elements[1] = 20; elements[2] = 30; elements[3] = 40; elements[4] = 50; } void printValues() { std::cout << "Array elements: "; for (int i = 0; i < 5; i++) { std::cout << elements[i] << " "; } std::cout << std::endl; } }; int main() { ArrayExample myArray; myArray.setValues(); myArray.printValues(); return 0; }
Accessing array elements by index is very fast (constant time).
Arrays have fixed size; you cannot change their size after creation.
Common mistake: accessing outside the array size causes errors (out of bounds).
Use arrays when you know the number of items in advance and want fast access.
Arrays store multiple values of the same type together.
They allow easy access to each value by position.
Arrays have fixed size and are useful for organizing related data.
Practice
Solution
Step 1: Understand the purpose of arrays
Arrays group many values of the same type in one place, making data management easier.Step 2: Compare with other options
The other options do not describe the main purpose of arrays.Final Answer:
To store multiple values of the same type together -> Option CQuick Check:
Arrays store many values = A [OK]
- Thinking arrays store different types
- Confusing arrays with single variables
- Believing arrays speed up math automatically
Solution
Step 1: Recall C++ array declaration syntax
Arrays are declared by specifying type, name, and size in square brackets, like int arr[5];Step 2: Check each option
int arr[5]; matches correct syntax. int arr = {5}; attempts to initialize a single int, int arr; declares a single int, array int arr(5); is invalid syntax.Final Answer:
int arr[5]; -> Option AQuick Check:
Array declaration uses brackets = D [OK]
- Missing brackets for size
- Using wrong keywords
- Trying to assign size like a value
int arr[3] = {10, 20, 30};
std::cout << arr[1];Solution
Step 1: Understand array indexing
Arrays start at index 0, so arr[0] = 10, arr[1] = 20, arr[2] = 30.Step 2: Identify the printed element
The code prints arr[1], which is 20.Final Answer:
20 -> Option AQuick Check:
arr[1] = 20 [OK]
- Assuming index starts at 1
- Confusing values at indexes
- Expecting syntax error
int arr[3]; arr[3] = 50;
Solution
Step 1: Check array size and indexing
Array arr has size 3, valid indexes are 0, 1, 2.Step 2: Identify invalid index usage
arr[3] accesses the fourth element, which is out of bounds and causes error.Final Answer:
Array index out of bounds error -> Option DQuick Check:
Index 3 invalid for size 3 array [OK]
- Using index equal to size
- Ignoring out of bounds errors
- Confusing syntax errors with runtime errors
Solution
Step 1: Understand the problem requirements
We need to store multiple temperature values for each day and access them easily.Step 2: Evaluate array features
Arrays store multiple values of the same type and allow access by index, perfect for daily temperatures.Step 3: Eliminate incorrect options
Arrays do not calculate averages automatically, cannot change size dynamically, and store only one type.Final Answer:
Because arrays allow storing multiple related values with easy access by position -> Option BQuick Check:
Arrays store many values accessibly = A [OK]
- Thinking arrays auto-calculate
- Believing arrays resize automatically
- Assuming arrays hold mixed types
Ready to prove it?
Pass the test and this pattern moves unknown
