Learning Outcomes
At the end of this chapter, you should be able to:
Understand the concept of array.
Perform the array declaration and initialization.
Apply on how to access elements in an array.
Perform operations (summation, maximum and minimum) in an array.
Perform passing array to function.
Introduction
In C++ programming, an array is a data structure. It is process of group the same categories of data.
Array is a collection of data of the same data type. An array allows us to group and store same variables which have a same data type.
Usually a variable can hold one value at a time, but by using an array, we can store more than one value in a single variable.
Figure 6.1 shows the difference between variable and array.
Figure 6.2 shows the arrays for variablessalary, name and staff ID.
An array can store many values, and each of the values is known as an element and has its own array index that can be used to access it.
A variable can be easily retrieved by referring to the index.
An array index is also known as subscripts.
A subscript is a number that indicates the position of a particular array element that is being used.
Figure 6.4 illustrates the array elements and array indexing of a variable named temperature.
The figure shows four elements stored in the array.
The subscripts must start with 0, which is why the last subscript ends with three.
An array will start with the 0th element and end with array size -1.
The figure also shows the arrangement of the array in a portrait orientation (a) and landscape orientation (b).
Array Declaration
As in normal programming steps, we have to declare the entire variable before we use it. Like normal variables, arrays also have to be declared.
An array can be declared as:
datatype arrayName[size];
For array declarations, firstly, type any simple data type, continue with arrayName, with any legal identifier, and the size (in square bracket).
The size represents the number of elements the array contains.
Example of array declaration statement:
int listNumber [100];
the array named listNumber which reserve 100 elements of integer values.
char codeId [ 6];
the array reserves 6 element of characters named codeId.
Array Initialization
In C++ programming, we can assign or initialize values to the array. Initialization means assigning values to an array.
The format to write the array initialization is:
datatype arrayName[size]={ elements};
An array’s initial values are written using curly brackets
{ } and commas (,) to differentiate each element in the array as shown in the following example.
We can also initialize the values to an array without declaring the size of the array.
The number of elements in an array can define the size of an array as shown in Example 6.3.
This means we can declare an array with or without size, but we must initialize the values to it.
There can also be a situation whereby we have declared the size of an array with 6 and initialized the values for 3 elements only—the compiler will assume the next three elements are zero as shown in Example 6.4.
Restrictions in Array Operation
There are some restrictions in array operation that we should consider in a few situations.
In the first situation, an error message will appear once we want to copy one array to another array as shown in Example 6.5.
We will also have a problem once we want to insert values into an array. We cannot insert or read data to an array using the normal way.
For the solution, we have to insert data to an array using the looping method as shown in Example 6.6.
In another situation, we have to specify the index number once to retrieve the value from an array or it will display an error message. Example 6.7 shows the solution for the situation.
Accessing Array Elements
The for loop is commonly used to manipulate all the operations in arrays. Usually, with the iteration or loop process, it will make the process easier because it allows us to access the elements by referring to the subscript in the sequence.
Besides using for loop to perform the accessing process, we also can access and initialize the elements using the various methods as shown in Example 6.8.
Besides all the methods shown, we can use other ways such as the for loop to initialize or access arrays. Example 6.9 shows the general form of how to read elements into an array.
As in a normal iteration process, we have to define the for loop argument based on the array size.
As usual, we have to start the iteration with zero (0), and stop the loop once we reach the arraysize-1.
The counter for the loop should be increased by one with each iteration.
Example 6.10 shows the general form to display the array’s contents. Here, we will also use the for loop method to display all the elements of an array by sequence.
Example 6.11 shows a complete C++ program that creates an array and display its contents. This program will have an array with 10 elements that will be initialized with values and then display them.
Example 6.12 shows a complete C++ program that creates an array and displays its contents.
The program will have two arrays, named StudentNo and Student_Marks. Both arrays will store 3 values each.
The program will prompt the user to insert the values for the arrays.
Then, the program will display the contents of the arrays in table form as shown in the output.
The program in Example 6.13 will declare an array named EvenList [ ] with 10 elements in it.
The array will store 10 values consisting of even numbers that are less than 20, then display them.
Then, the program will identify the even numbers from the list of numbers entered by the user.
Array Operations
Traversing
Traversing is an operation whereby each element in an array is accessed for a specific purpose.
Actually, in the traversal operation, we will use the values in many programs because it is a basic operation that we have discussed before.
The process of printing the elements, and calculations between the elements are all examples of the traversal operation.
Inserting
Inserting is a process of adding a new element into an existing array.
We can add the element in the beginning, middle, or at the end of the array.
The inserting process can be applicable only if the size that has been declared is large enough. For example, an array with size 5 has only 3 elements initialized to it. In this case, we can apply the insertion process.
However, if there is not enough space (size) available, then we cannot proceed with the operation.
Searching
Searching means the process of finding an element in the array.
If the element is in the array, the location or the subscript of the element will be displayed and if the element is not available in the array, an appropriate message will be displayed.
There are two types of searching methods available—linear search and binary search.
Summation
Summation is a process of adding two or more values together.
So, in arrays, summation can be performed to find the total of the values in an array.
Besides that, summation can also be done between any elements in an array as will be discussed in Example 6.15.
Example 6.16 shows a program segment for summation operation to find the total of an array price[ ].
Here, for calculating the total of the values in an array, we have declared a variable to accumulate the total.
The variable should be initialized to zero (0) before we start to accumulate the value.
Finding the Maximum
For finding the largest value in an array, we can use the maximum operation, which will compare each element in an array to find the largest value.
Example 6.18 shows the general form to find the largest value. We will use for loops and if methods to check and compare each of the values in the array to find the maximum value.
In the operation, firstly, we have to define a variable named MaxIndext and assign zero (0) to it.
Then, we will start to compare the values in the array using the sequence until we fulfil the objective to find the largest value.
Example 6.18 shows the general form to find the largest value.
Finding the Minimum
We can find the lowest value in an array using the same steps for finding the maximum value. Here also, the operation will compare each element in an array to find the lowest value.
Example 6.20 shows the general form to find the lowest value in an array. We will use for loops and if methods to check and compare each of the values in the array to find the minimum value.
Passing Array to Function
Arrays can also apply in functions in C++ program.
An array can be passed as a parameter in a function. Example 6.22 shows the array usage in a function.
The program shows that a function named calculateTotal ( ) receives a parameter of type array.
The function will receive the values in the array, then calculate the total. It returns the total to the main function.
Conclusion
An array is a collection of similar data type value.
In C++ programming, array is also known as a simple data structure.
Array will store the group data for a temporary basis in memory.
Each data in the array is stored in a sequence and has its own address that known as index. Index is also called as subscript.
Summation, and finding the maximum and minimum values, are some common operations of an array method.
Comments
Post a Comment