2023 Slide Deck - ITPPA1-22 - Week - 5
2023 Slide Deck - ITPPA1-22 - Week - 5
ITPPA1-22
Eduvos (Pty) Ltd (formerly Pearson Institute of Higher Education) is registered with the Department of Higher Education and Training as a private higher education institution under the
Higher Education Act, 101, of 1997. Registration Certificate number: 2001/HE07/008 1-1
What will be covered
in today’s lesson?
In this chapter, you will learn the
Introduction of arrayy.
Array Declaration
Examples
1-2
Introduction
• What are arrays and why do we need them?
• Arrays are used to store multiple values of the same
type in a single variable.
• Arrays make it easier to calculate the position of
each element by simply adding an offset to a base
value.
• Arrays provide fast access to any element by using
its index.
• Arrays can be used to implement other data
structures such as hashtables, stacks, queues, etc.
1-3
Array Declaration
How to declare an array in C++?
• To declare an array, we need to specify the data type,
the name of the array, and the number of elements it
can hold inside square brackets.
• For example, int x[6]; declares an array of 6 integers
named x.
• The size and type of arrays cannot be changed after
declaration
1-4
Array Initialization
1-5
Array Access
• To access an array element, we need to use
the name of the array and the index of the
element inside square brackets.
• For example, x[0] accesses the first element
of the array x, which is 19.
• The index of an array starts from 0 and ends
at n-1, where n is the size of the array
1-6
• Array Modification
1-7
Example 1…
#include <iostream>
using namespace std;
int main() {
// declare and initialize an array of 5 integers
int numbers[5] = {7, 5, 6, 12, 35};
1-8
Example 1 cont…
cout << endl;
// change the third element to 10
numbers[2] = 10;
// print all elements of the modified array
cout << "The modified numbers are: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
1-9
Example 1 Output…
1-10
Array with Empty Members
What happens if we initialize an array with less than its size?
In such cases, the compiler assigns random values (usually zero)
to the remaining places2.
For example,
// declare and initialize an array with less than its size
int x[6] = {19, 10, 8};
1-12
• Passing Array to a Function
• To pass an array to a function, we need to specify the name
of the array and its size as parameters3.
• For example,
• // function prototype
void printArray(int arr[], int size);
1-13
• Passing Array to a Function Cont
// function definition
void printArray(int arr[], int size) {
// print all elements of the array
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
1-14
• Passing Array to a Function Cont
// main function
int main() {
// declare and initialize an array
int numbers[] = {7, 5, 6, 12, 35};
return 0;
}
1-15
Class Examples
• Write a C++ program that declares an array of 10 integers
and asks the user to enter 10 numbers. The program then
prints the sum and the average of the numbers entered by
the user.
• Write a C++ program that declares an array of 10 integers and
asks the user to enter 10 numbers. The program then prints
the largest and the smallest number in the array and their
indices.
1-16
• Summary and Questions
1-17
What Happens Next?
Prepare Chapter 5: File structures for next week.
Prepare the work on myLMS under week 5: File Structures.
1-18