0% found this document useful (0 votes)
17 views18 pages

2023 Slide Deck - ITPPA1-22 - Week - 5

This document provides an introduction to procedural programming in C++ and covers arrays. It defines what arrays are, how to declare and initialize them, access and modify elements, pass arrays to functions, and provides examples. The key points covered are: - Arrays store multiple values of the same type and provide fast access using indices - Arrays are declared by specifying the type, name, and size - Elements can be accessed and modified using indices in brackets - Loops can iterate over arrays to perform operations on elements - Arrays can be passed to functions along with their size - Examples demonstrate initializing, accessing, modifying, and passing arrays

Uploaded by

Nevin Tom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

2023 Slide Deck - ITPPA1-22 - Week - 5

This document provides an introduction to procedural programming in C++ and covers arrays. It defines what arrays are, how to declare and initialize them, access and modify elements, pass arrays to functions, and provides examples. The key points covered are: - Arrays store multiple values of the same type and provide fast access using indices - Arrays are declared by specifying the type, name, and size - Elements can be accessed and modified using indices in brackets - Loops can iterate over arrays to perform operations on elements - Arrays can be passed to functions along with their size - Examples demonstrate initializing, accessing, modifying, and passing arrays

Uploaded by

Nevin Tom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Procedural Programming

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.

Week 5 Introduction What are arrays

Array Declaration

Array access and modification

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

How to initialize an array in C++?


• To initialize an array, we can use an array literal that contains
the values of the elements inside curly braces12.
• For example, int x[6] = {19, 10, 8, 17, 9, 15}; initializes the
array x with 6 values.
• We can also omit the size of the array and let the compiler
infer it from the number of values.
• For example, int x[] = {19, 10, 8, 17, 9, 15}; is equivalent to
the previous example

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

• To modify an array element, we need to assign a new value to


it using the same syntax as accessing it.
• For example, x[3] = 9; changes the fourth element of the array
x from 17 to 9.
• We can also use loops to iterate over the array elements and
perform operations on them.

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};

// print all elements of the array


cout << "The numbers are: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}

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…

The output of the previous example is:


The numbers are: 7 5 6 12 35
The modified numbers are: 7 5 10 12 35

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};

// print all elements of the array


for (int i = 0; i < 6; i++) {
cout << x[i] << " ";
}
1-11
• Output of Array with Empty Members

• The output of the previous example is:


• 19 10 8 0 0 0 .

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};

// pass the array and its size to the function


printArray(numbers, 5);

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

• In this topic, we learned about C++ arrays and how to use


them.
• We learned how to declare, initialize, access, modify, pass
and return arrays in C++.
• We also learned about some advantages and disadvantages
of arrays.
• Do you have any questions or comments?.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy