Mada za sehemu hiiImplementing arrays in a programMada 2
- Demonstrating declaration and initialization of single dimensional array
- Demonstrating accessing and printing of elements from an array
Accessing and Printing Elements from an Array
When you store data in an array, you often need to retrieve and display specific values. Accessing means getting a value from a particular position in the array, while printing means showing those values on the screen. This lesson demonstrates how to perform both operations using C++.
In C++, array indices always start from 0, not 1. This is called zero-based indexing. If you have an array with 5 elements, the valid indices are 0, 1, 2, 3, and 4.
For example, consider an array called Age with 5 elements:
| Index | [0] | [1] | [2] | [3] | [4] |
|---|---|---|---|---|---|
| Age | 12 | 10 | 9 | 16 | 5 |
- The first element (12) is at index 0
- The second element (10) is at index 1
- The third element (9) is at index 2
- The fourth element (16) is at index 3
- The fifth element (5) is at index 4
The general formula for finding an element's position is: index = n - 1, where n is the position counting from 1.

To access a specific element in an array, you use the array name followed by the index in square brackets. The syntax is:
arrayName[index];
Examples of Accessing Elements
int mark[5] = {19, 10, 8, 17, 9};
// Access and print first element
cout << mark[0]; // outputs: 19
// Access and print third element
cout << mark[2]; // outputs: 8
// Access fourth element and assign to variable
int x = mark[3]; // x now holds 17
You can also change an element's value using the assignment operator:
mark[3] = 9; // changes fourth element from 17 to 9
There are two main methods to print array elements in C++.
Method 1: Manual Printing (Element by Element)
You can print each element individually by specifying its index:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
cout << numbers[0] << " ";
cout << numbers[1] << " ";
cout << numbers[2] << " ";
cout << numbers[3] << " ";
cout << numbers[4] << " ";
return 0;
}
Output:
The numbers are: 7 5 6 12 35
This method becomes tedious when working with large arrays.
Method 2: Using a For Loop
The preferred method uses a loop to access and print all elements efficiently:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
Output:
The numbers are: 7 5 6 12 35
The loop variable i starts at 0 and increases to 4, accessing each element in sequence.
You can also accept user input, store it in an array, and then display it:
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// Store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// Print array elements
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
Sample Input:
7
3
9
12
5
Sample Output:
The numbers are: 7 3 9 12 5
- Array indices always start from 0 in C++
- The last valid index is always
arraySize - 1 - Use
arrayName[index]to access a single element - Use a for loop to print all elements efficiently
- Always ensure your loop does not exceed the array bounds
- Index out of bounds: Accessing index 5 in an array of size 5 is invalid (valid indices are 0 to 4)
- Using wrong index: Remember that the first element is at index 0, not index 1
In Tanzania, shop owners can use arrays to manage inventory. For example, a kiosk owner in Dar es Salaam can store the prices of 10 different items in an array and write a program to display each item's price when needed. This makes it easy to check and update prices without flipping through paper records, saving time during busy market days at Kariakoo or along the beaches of Zanzibar.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Access array elements correctly using indices.
- Traverse arrays using loops (e.g., for and while loops).
Tathmini ya matokeo
Array elements are accessed and displayed (printed) correctly as per the task requirements.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Use indices to retrieve specific elements from an array.
- Iterate through arrays to display all elements or subsets.
- Debug errors related to incorrect index access.
Swali
What is the index of the first element in any C++ array?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado