CSC404 CHAPTER3 1darray
CSC404 CHAPTER3 1darray
ONE-DIMENSIONAL ARRAYS
Learning Outcomes
Atthe end of this chapter, student
should be able to:
learn about the use of arrays to store data
declare and initialize an array
manipulate data in the arrays
use string functions to process C-strings
pass an array as a parameter to a function
Introduction
The
variable that we have used so far
have all had common characteristic:
Each variable could be used to store only a
single value at a time – called simple data
type
Example:
char grade;
int count;
float marks;
Introduction (cont.)
Frequently
we may have set of values, all of
the same data type, that form a logical
group.
Example:
Marks Codes Prices
98 x 10.96
87 a 6.43
92 m 2.58
79 n 0.86
Array
One- Two-
dimensional dimensional
array array
One-Dimensional Array
Asimple list containing individual items of
the same data type that is stored in a single
group name (array name).
Also referred as single – dimensional array.
Aone-dimensional array is an array in which
the components are arranged in a list form.
Can be visualized as a column of variables.
marks[0] 98
marks[1] 87 [0] [1] [2] [3]
marks[2] 92 marks 98 87 92 79
marks[3] 79
Declaring Arrays
Arrays occupy space in memory.
The
general syntax of declaring a one-
dimensional array is:
dataType arrayName [arraySize];
The
arraySize must be an integer constant
greater than zero.
Example 1
For
example, to tell the compiler to reserve 5
elements for integer array num, use the
declaration
int num[5];//num is an array of 5 integers
Or Index or subscript
Array name
const int SIZE = 5;
int num[SIZE];
num[0] 4
num[1] 2
num[2] 7
num[3] 3
num[4] 6
Initializing Arrays
Likeany other simple variable, arrays can
also be initialized while they are being
declared.
When initializing arrays while declaring
them, it is not necessary to specify the size
of the array.
The
size of the array is determined by the
number of initial values in the braces.
Example 2
The statement
int list[10] = {0};
Declares list to be an array of 10 components and
initializes all components to zero.
The statement
int list[10] = {8, 5, 12};
Declares list to be an array of 10 components,
initializes list[0] to 8, list[1] to 5, list[2] to 12
and all other components are initialized to 0.
The statement
int list[] = {5, 6, 3};
Declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to
3.
Example 3
The statement
int list[5];
num[0] = -4;
num[1] = 6;
num[2] = 10;
num[3] = 4;
num[4] = 15;
Declares num to be an array of 5
components, and initializes num[0] to -4,
num[1] to 6, num[2] to 10, num[3] to 4,
and num [4] to 15.
Accessing Array Component
The
general syntax of accessing an array
component is:
arrayName[index]
Index
value specifies the position of the
component in the array.
The[ ] operator is called the array
subscripting operator.
Example 4
Consider the following declaration:
int list[5] = {0};
This
statement declares an array list of 5
components.
Thecomponents are list[0], list[1], …,
list[4]. In other words, we have declared
5 variables.
list[0]
list[1]
list[2]
list[3]
list[4]
Example 5
The assignment statement:
list[4] = 34;
stores
34 in list[4], which is the fifth
component of the array list.
list[0] 0 list[0] 0
list[1] 0 list[1] 0
list[2] 0 list[2] 0
list[3] 0 list[3] 0
list[4] 0 list[4] 34
Before After
Example 6
Suppose
i is an int variable. Then the
assignment statement:
list[4] = 34;
is equivalent to the assignment statements:
i = 4;
list[i] = 34;
Example 7
Next consider the following statements:
list[0] = 10; list[0] 10
list[1] 35
list[1] = 35;
list[2]
list[3] = list[0] + list[1]; list[3] 45
list[4] 34
Input data
Output data stored in array
Find the sum and average
Find the largest and/or smallest element
Eachof these operations requires the ability to
step through the elements of the array.
Stepping-through
the elements of an array is
easily accomplished by a loop – for loop
Example 8a
This
example shows how loops are used to
process arrays.
Thefollowing declaration is used
throughout this example:
double sale[10];
int i;
double sum, average, largestSale;
Example 8b
Initializingan array: The following loop
initialized every component of the array
sale to 0.0
sum = 0.0;
for(i = 0; i < 10; i++)
sum = sum + sale[i];
average = sum / 10;
Example 8f
Largest element in the array:
The following statement is to find the largest
element in the array. Assume that the value
element of the sale as follow:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
sale 12.50 8.35 19.00 25.00 14.00 39.43 35.90 98.23 66.65 35.64
Example 8f (cont.)
Method 1: Use the index (location of the largest element
in the array) of the first occurrence of the largest element
to find the largest sale.
19.60<25.00 is true;
3 2 19.60 25.00
maxIndex = 3
4 3 25.00 14.00 25.00<14.00 is false
25.00<39.43 is true;
5 3 25.00 39.43
maxIndex = 5
6 5 39.43 35.90 39.43<35.90 is false
39.43<98.23 is true;
7 5 39.43 98.23
maxIndex = 7
8 7 98.23 66.65 98.23<66.65 is false
9 7 98.23 35.64 98.23<35.64 is false
Example 8f (cont.)
Method 2
NUMBER[]
[0] [1] [2] [3] [4]
Exercise 3 (cont.)
b) temp = NUMBER[0];
for(int i=0; i<3; i++)
NUMBER[i] = NUMBER[i+1];
NUMBER[4] = temp;
NUMBER[]
[0] [1] [2] [3] [4]
Exercise 4
numis an integer array of ten elements.
Write C++ statements to do the following:
Declare num.
Read all elements of num.
Display all positive integers in num.
Exercise 5
Write a program that accepts a list of integers
and places them into an array. When the enter
button is pressed, the list of integers in the array
is displayed in a reverse order. Below is the
sample output of the program.
Exercise 6
Given declaration as follows:
const int NUMARRAY = 5;
float LIST[NUMARRAY];
How many elements are in LIST array?
Using a for statement, write a program to assign the
value of 1.2, 3.2, 5.5, 7.1, 5.5 from user input to the
LIST array.
Based on the above question, what is the value of
x?
x = int (LIST[l]) + LIST[4] / LIST[3] * pow (LIST[2] ,3);
Exercise 7
Write
a complete C++ program using a one-
dimensional array to perform the following:
declare an array named voltage to store 50
floating point voltages.
input values for voltages into the array named
voltage.
calculate and display the total and average of
voltages.
find and display the highest and lowest voltage.
count and display the values of voltages that
exceed 100 volts.
Exercise 8
Create an array of double of size 30 to store
daily temperatures in Celsius for one month.
Write a C++ program to do the following
operations on the array:
prompt the user to input the daily temperatures
and stores it into the array.
determine and display the hottest and the coldest
temperature of the month.
determine and display the difference between
the hottest and the coldest temperature of the
month.
determine and display the average temperature
of the month.
Searching an Array for a Specific Item
Searching
a list for a given item is one of the
most common operations performed on a list.
Sequential search (or linear search):
Searching a list for a given item, starting from the
first array element.
Compare each element in the array with value
being searched for.
Continue the search until item is found or no more
data is left in the list.
Searching an Array for a Specific Item
(cont.)
Example:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
numList 35 12 65 78 45 34 17 30 98 22
temp = grade[1];
grade[1] = grade[2];
grade[2] = temp;
This
process successfully swaps the values
without the loss of any values.
Bubble Sort
As elements are sorted, they gradually “bubble” or
rise to their proper location in the array.
Repeatedly compares adjacent/next elements of
an array.
84 69 76 86 94 91
Array at
84 69 76 86 94 91
beginning
After Pass 1 84 76 86 94 91 69
After Pass 2 84 86 94 91 76 69
After Pass 3 86 94 91 84 76 69
After Pass 4 94 91 86 84 76 69
After Pass 5
94 91 86 84 76 69
(done)
Bubble Sort (cont.)
Whenthe first pass through the array is
complete, the bubble sort returns to
elements one and two and start the
process all over again.
The
bubble sort knows that it is finished
when it examines the entire array and no
“swaps” are needed.
Example 9a
const int array_size = 4;
int x[array_size] = {10, 40, 13, 8};
int hold;
10 40 13 8
In the first pass, the value i=0 and the inner loop came into action,
it will perform 3 iterations and check the condition of the following
block of statements.
0 1 2 3
10 40 13 8
Unsorted list
10 40 13 8
At second iteration, the value j=1 and the value j+1=2.
Therefore it will compare the values of first index and the
second index of an array.
As we can see that the value at second index is smaller
than the first index, therefore the elements of first and
second will be swap, the new array looks like:
0 1 2 3
10 13 40 8
Explanation (cont.)
Iteration no. 3:
0 1 2 3
10 13 40 8
10 13 8 40
As you can see that the end of the first pass, the largest
value is placed at the last index.
Explanation (cont.)
Second Pass:
Iteration no. 1:
0 1 2 3
10 13 8 40
10 13 8 40
0 1 2 3
10 8 13 40
Explanation (cont.)
Iteration no. 3:
0 1 2 3
10 8 13 40
10 8 13 40
At first iteration, the value j=0 and the value j+1=1. Therefore
it will compare the values of zero index and the first index of
an array.
As we can see that the value at first index is smaller than the
zero index, therefore the elements of zero and first will be
swap, the new array looks like:
0 1 2 3
8 10 13 40
At this point the loop is completed. The outer loop will also
terminate and the array is sorted in ascending order
Exercise 9
Sort the list into alphabetical order:
G, C, N, A, P, C