You are on page 1of 33

Chapter 8

Arrays and Strings


C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
Arrays as Parameters to Functions
• Arrays are passed by reference only
• Do not use symbol & when declaring an array
as a formal parameter
• Size of the array is usually omitted
– If provided, it is ignored by the compiler
• Example:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3


Arrays are passed by reference only

• Reference parameters
• Value parameters

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4


Constant Arrays
as Formal Parameters
• Can prevent a function from changing the
actual parameter when passed by reference
– Use const in the declaration of the formal
parameter
• Example:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5


C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10
Function call

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11


Base Address of an Array and Array in
Computer Memory
• Base address of an array: address (memory
location) of the first array component
• Example:
– If list is a one-dimensional array, its base
address is the address of list[0]
• When an array is passed as a parameter, the
base address of the actual array is passed to
the formal parameter

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12


C++ Programming: From Problem Analysis to Program Design, Seventh Edition 13
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14
Functions Cannot Return a Value of
the Type Array
• C++ does not allow functions to return a value
of type array

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15


Example

double distanceTraveled[SIZE];
double averageSpeed[SIZE];

const int SIZE = 6;

void getData(double list[], int length);


void averageSpeedOverTimeInterval(double list[], int length,
double avgSpeed[]);
double maxAvgSpeed(double avgSpeed[], int length);
double minAvgSpeed(double avgSpeed[], int length);
void print(double list[], int length, double avgSpeed[]);

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16


Integral Data Type
and Array Indices
• C++ allows any integral type to be used as an
array index
– Improves code readability
• Example:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17


Other Ways to Declare Arrays
• Examples:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18


Searching an Array
for a Specific Item
• 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

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19


Find 27

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20


C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24
Sorting
• Selection sort: rearrange the list by selecting
an element and moving it to its proper
position
• Steps:
– Find the smallest element in the unsorted portion
of the list
– Move it to the top of the unsorted portion by
swapping with the element currently there
– Start again with the rest of the list
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
Selection Sort (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26


C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
Auto Declaration and Range-Based
For Loops
• C++11 allows auto declaration of variables
– Data type does not need to be specified
auto num = 15; // num is assumed int
• Range-based for loop
sum = 0;
for (double num : list) // For each num
sum = sum + num; // in list

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 29


Inclass 1/19

readData(ifstream& fin, int list[], int SIZE)

printResult(const int list[], int SIZE)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30


hint
const int SIZE = 8;
for(int i=0; i<SIZE; i++){
if(I != Size)
Cout << i*25 << “-“ << (i+1)*25-1 << endl;
else
Cout << i*25 << “-“ << (i+1)*25 << endl;
}

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31


Int score = 35;
Cout << Score/25;

Int score = 200;


Cout << score/25 - 1

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32


Read file:
#include <fstream>
ifsteam fin;
fin.open(“filename”);
while(fin){
fin >> score;
int index = score/25;
list[index]++;
}
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33

You might also like