0% found this document useful (0 votes)
2 views

Topic 2 Array Part2

The document covers key concepts related to arrays in programming, including initialization, summation, and usage of arrays as counters. It discusses passing arrays to functions, searching with linear search, and sorting with insertion sort, along with an introduction to multidimensional arrays. Additionally, it highlights the importance of static and automatic local arrays for performance optimization.

Uploaded by

paullaguerder0
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)
2 views

Topic 2 Array Part2

The document covers key concepts related to arrays in programming, including initialization, summation, and usage of arrays as counters. It discusses passing arrays to functions, searching with linear search, and sorting with insertion sort, along with an introduction to multidimensional arrays. Additionally, it highlights the importance of static and automatic local arrays for performance optimization.

Uploaded by

paullaguerder0
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/ 34

Arrays Constant,

Search and Sorting


Objectives

• Recall how to initialize an array


• Specify an array using constant variable
• Summing up the value of an Array
• Use elements of an array as counter
• Static Local Arrays and Automatic Local Arrays
Objectives

• Passing Arrays to Function


• Searching Arrays with Linear Search
• Sorting Arrays with Insertion Sort
Initialize and Array

//initialize elements of
array n to 0
int n[10] = { };
//array size is omitted
array n[ ] = {0,1,2,3,4}
Initialize and Array

int n[5] = {32,27,64,18,95,14};

*array declaration causes compilation error


Specifying Array’s Size with a Constant Variable and
Setting Array Elements with Calculations

Constant variable also call


named constants or read –
only variables
Specifying Array’s Size with a Constant Variable and
Setting Array Elements with Calculations

Not assigning variable when it


is declared is compilation error
Assigning a value to a constant
variable in an executable
statement is a compilation
error
Summing the Elements of an Array
Using Elements of an Array as Counters
Using Array to Summarize Survey Results

Twenty students were asked to rate on a scale of 1


to 5 the quality of the food in the student cafeteria,
with 1 being “awful”, and 5 being “excellent”. Place
the 20 responses in an integer array and determine
the frequency of each rating
Using Array to Summarize Survey Results

Twenty students were asked to rate on a scale of 1


to 5 the quality of the food in the student cafeteria,
with 1 being “awful”, and 5 being “excellent”. Place
the 20 responses in an integer array and determine
the frequency of each rating
Using Array to Summarize Survey Results
Using Array to Summarize Survey Results
Referring to an element
outside the array bounds is
an execution time logic
error. It isn’t a syntax error.
Static Local Arrays and Automatic Local Arrays

• Static local variable in a function definition


exists for the program’s duration but is visible
only in the function body
We can apply static to a local array declaration so that it not
created and initialized each time the program calls the function and
is not destroyed each time the function terminates. This can improve
performance , especially when using large array
Static Local Arrays and Automatic Local Arrays
Static Local Arrays and
Automatic Local Arrays
Static Local Arrays and
Automatic Local Arrays
Passing Arrays to Functions

• To pass an array argument to a function, specify


the name of the array without any bracket
int hourlyTemperatures [24];
• The function call :
modifyArray(hourlyTemperatures, 24);
Passing Arrays to Functions

• C++ passes arrays functions by reference


• The value of the name of the array is the
address in the computer’s memory of the first
element of the array
Passing Arrays to Functions

• Passing arrays by reference makes send for


performance reason
• Passing by value would require copying each
element.
• For large, frequently passed arrays, this would be
time consuming and would require considerable
storage for the copies of the array elements.
Passing Arrays to Functions

• Observation:
It’s possible to pass an array by values by
embedding it as a data member of a class
and passing an object of the class, which
defaults to pass – by – value.
Searching Arrays with Linear Search

• The process of finding a particular element of an


array is called searching
• Linear search, compares each element of an array
with a search key
• Linear searching method works well for small
arrays or for unsorted arrays(arrays whose
elements are in no particular order)
Sorting Arrays with Insertion Sort

• Sorting data (placing the data into some


particular order such as ascending or
descending)
• Insertion sort – it is a simple but inefficient,
sorting algorithm
Sorting Arrays with Insertion Sort

• The first iteration of this algorithm takes the


second element and ,if it’s less than the first
element, swaps it with the first element
• The second iteration looks at the third element and
inserts it into the correct position with respect to
the first two elements, so all three elements are in
order
• At the ith iteration of this algorithm, the first i
elements in the original array will be sorted
1. {34, 56, 4, 10, 77, 51, 93, 30, 5, 52}
2. {4 ,34, 56,10, 77, 51, 93, 30, 5, 52}
3. {4 ,10, 34, 56,77, 51, 93, 30, 5, 52}….
Multidimensional Arrays

• Arrays with two dimensions (i.e subscript) often


represent tables of values consisting of
information arranged in rows and columns
– 1st subscripts identifies the elements row
–2nd subscripts identifies the elements’ column
Multidimensional Arrays

• Arrays that require two subscripts to identify


particular element are called two – dimensional
arrays or 2 – D arrays
• Arrays with two or more dimension are known
as multidimensional arrays and can have more
than two dimensions
Multidimensional Arrays

Column 0 Column 1 Column 2 Column 3


Row 0 a[0] [0] a[0] [1] a[0] [2] a[0] [3]
Row 1 a[1] [0] a[1] [1] a[1] [2] a[1] [3]
Row 2 a[2] [0] a[2] [1] a[2] [2] a[2] [3]
Column Subscript
Row Subscript
Array Name

• It is a 3-by – 4 array
• An array with m rows and n columns is called an m-by-n array
Multidimensional Arrays

• A multidimensional array can be initialized in its


declaration b[0][0] b[1][0]

– int b[2][2] = { {1, 2}, {3, 4} }; b[1][1]


b[0][1]
• If there are not enough initialized for a given row, the
remaining elements of that row are initialized to 0 thus the
declaration
– Int b[2][2] = { { 1 }, { 3, 4} };
Multidimensional Arrays
for (int column = 0; column < 4; ++column)
a[2] [column] = 0;

Equivalent to:

a[ 2 ] [ 0 ] = 0;
a[ 2 ] [ 1 ] = 0;
a[ 2 ] [ 2 ] = 0;
a[ 2 ] [ 3 ] = 0;
• To get the total elements in array a:
total = 0;
for(int row = 0; row <3; ++row)
for (int column = 0; column < 4; ++column)
total += a[ row] [column];

You might also like