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
Arrays in Programming
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. Instead of declaring five separate variables, you can declare one array that holds all five values. All values are stored in contiguous memory locations and can be accessed using the same identifier with the appropriate index.

A one-dimensional array is an array with a single row and more than one column. You can imagine it as a row where elements are stored one after another.
Consider an array named Age with five elements:
| Index | [0] | [1] | [2] | [3] | [4] |
|---|---|---|---|---|---|
| Age | 12 | 10 | 9 | 16 | 5 |
Remember: array indices always start from 0, not 1. The first element is at index 0, the second at index 1, and so on.
To declare a single-dimensional array, use this syntax:
data_type array_name[array_size];
Where:
- data_type — is the type of data for each array element (int, double, char, etc.)
- array_name — is the name of the array used to refer to it
- array_size — is the number of elements the array will hold
Declaration Examples
int age[7]; // declares an array named age that can hold 7 integers
double salary[15]; // declares an array named salary that can hold 15 decimal numbers
char gender[10]; // declares an array named gender that can hold 10 characters
You can initialize array elements in two ways: using a single statement or assigning values one by one.
Method 1: Initialize with a Single Statement
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
When initializing this way, you do not need to specify the array size because the compiler determines it from the number of elements in the braces:
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0}; // size is 5
For character arrays (strings):
char name[7] = {'J', 'o', 'h', 'n', 'n', 'y', '\0'};
char name[] = "Johnny"; // simpler form
Method 2: Assign Elements One by One
You can assign values to specific positions using the index:
array_name[index] = value;
Given the array double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
- First element:
balance[0] = 1000.0; - Second element:
balance[1] = 2.0; - Third element:
balance[2] = 3.4; - Fourth element:
balance[3] = 17.0; - Fifth element:
balance[4] = 50.0;
Example: Declaring and Initializing an Integer Array
#include <iostream>
using namespace std;
int main() {
// Declare an array of 5 integers
int marks[5];
// Initialize each element
marks[0] = 19;
marks[1] = 10;
marks[2] = 8;
marks[3] = 17;
marks[4] = 9;
// Display the array elements
cout << "Student marks are: ";
for(int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
return 0;
}
Output:
Student marks are: 19 10 8 17 9
Example: Initialize Array During Declaration
#include <iostream>
using namespace std;
int main() {
// Declare and initialize in one statement
int age[] = {12, 10, 9, 16, 5};
// Print all elements
cout << "Age array elements: ";
for(int i = 0; i < 5; i++) {
cout << age[i] << " ";
}
return 0;
}
- Array indices always start at 0, not 1
- The last valid index is always (array_size - 1)
- All elements in an array must be of the same data type
- Always ensure you do not access an index outside the valid range
- The null character '\0' marks the end of a character string array
In Tanzania, shop owners can use single-dimensional arrays in a program to manage daily sales records. For example, a shopkeeper at Maskana Market in Dar es Salaam can store the prices of 10 different items in an array and use the program to quickly calculate the total bill for a customer, making the checkout process faster and reducing calculation errors when handling multiple items.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Declare and initialize arrays using correct syntax.
- Access, modify, and iterate over elements in the array.
- Debug and resolve errors related to arrayboundaries or initialization
- Use arrays to solve problems efficiently.
Tathmini ya matokeo
Arrays are correctly declared and initialized with valid data to meet specified requirements.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Declare and initialize arrays with fixed sizes and dynamic data.
- Access and manipulate array elements using indices.
- Iterate through arrays using loops for various operations.
Principles: The student should explain the principle of:
- Memory allocation for arrays and its impact on program efficiency.
- Logical indexing and boundary conditions in array operations.
- Data organization using arrays for sequential storage.
Theories: The trainee should explain: The trainee should explain:
- How arrays simplify data management and retrieval in programming.
- The relationship between arrays and iterative processing.
Detailed knowledge about:
- Common errors in array declaration and how to resolve them (e.g., outof-bounds errors).
- Real-world use cases of arrays in data storage and processing.
Swali
What is the correct syntax to declare an integer array named marks with 10 elements?
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