Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

The Islamic University of Gaza

Engineering Faculty

Department of Computer Engineering

Fall 2021

ECOM 2003

Eng. Baraa Alastal

Computer Programming: C++


Lab#8: Single-Dimensional Arrays

Contents
❖ Introduction
❖ Array Basics
❖ Passing Arrays to Functions
❖ Searching Arrays

Introduction
If you need to read 100 numbers, compute their average, and determine how many
numbers are above the average.
To accomplish this, the numbers must all be stored in variables. You have to declare
100 variables and repeatedly write almost identical code 100 times. Writing a
program this way would be impractical. So, how do you solve this problem?
C++ provides a data structure, the array, which stores a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of
data, but it is often more useful to think of an array as a collection of variables of the
same type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables as follows:

2
Lab#8: Single-Dimensional Arrays

Array Basics
An array is used to store multiple values of the same type. An element in an array
can be accessed using an index.

❖ Declaring Arrays
To declare an array, you need to specify its element type and size using the
following syntax:
elementType arrayName[SIZE];

3
Lab#8: Single-Dimensional Arrays

The elementType can be any data type, and all elements in the array will have the
same data type. The SIZE, known as array size declarator, must be an expression
that evaluates to a constant integer greater than zero. For example, the following
statement declares an array of 5 double elements:
double myList[5];

The compiler allocates the space for 5 double elements for array myList. When an
array is declared, its elements are assigned arbitrary values. To assign values we use
the following syntax:
arrayName[index] = value;
For example, the following code initializes the array:
myList[0] = 5.6;
myList[1] = 4.5;
myList[2] = 3.3;
myList[3] = 13.2;
myList[4] = 4.0;

Note:
The array size used to declare an array must be a constant expression in standard
C++.
For example, the following code is illegal:
int size = 4;
double myList[size]; // Wrong
But it is all right if SIZE is a constant as follows:
const int SIZE = 4;
double myList[SIZE]; // Correct

❖ Accessing Array Elements


The array elements are accessed through the integer index. Array indices are 0-
based; that is, they run from 0 to arraySize-1. The first element is assigned the index
0, the second element is assigned 1, and so on.

4
Lab#8: Single-Dimensional Arrays

For example:
- myList[4] represents the last element in the array myList.
- The following code increments myList[0] by 1:
myList[0]++;

❖ Initializing Arrays
You can initialize C++ array elements either one by one or using a single statement
as follow:

The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].

C++ allows you to omit the array size when declaring and creating an array using
an initializer. For example, the following declaration is fine

❖ Ex1: Write a program to find the first and the second largest elements in
an array of n elements.

5
Lab#8: Single-Dimensional Arrays

Passing Arrays to Functions

At some point, we may need to pass an array to a function as a parameter. In C++,


it is not possible to pass the entire block of memory represented by an array to a
function directly as an argument. But what can be passed instead is its address. In
practice, this has almost the same effect, and it is a much faster and more efficient
operation.
To accept an array as parameter for a function, the parameters can be declared as
the array type, but with empty brackets, omitting the actual size of the array. For
example:
void procedure (int arg[])
This function accepts a parameter of type "array of int" called arg. In order to pass
to this function an array declared as:
int myarray [40];

6
Lab#8: Single-Dimensional Arrays

it would be enough to write a call like this:


procedure (myarray);

❖ Ex2: Passing One-dimensional Array to a Function

Searching Arrays
Searching is the process of looking for a specific element in an array.

❖ The Linear Search Approach


The linear search approach compares the key element key sequentially with each
element in the array. The function continues to do so until the key matches an
element in the array or the array is exhausted.

7
Lab#8: Single-Dimensional Arrays

❖ Lab work:

1. Write a program that asks the user to type 10 integers of an array. The
program must compute and write how many integers are greater than or
equal to 10.

2. Write a program that asks the user to type 10 integers of an array. The
program must output the largest element in the array, and the index at
which that element was found.

You might also like