Lecture 6 - Arrays & Strings
Lecture 6 - Arrays & Strings
Programming 2
MEK 3100
Lecture 6
Introduction
• A data type is called simple if variables of that type can store only
one value at a time
int list[10];
Accessing Arrays Components
(cont’d)
Accessing Arrays Components
(cont’d)
Accessing Arrays Components
(cont’d)
Processing One-Dimensional
Arrays
• Some basic operations performed on a one-dimensional array are:
✓ Initializing
✓ Inputting data
✓ Outputting data stored in an array
✓ Finding the largest and/or smallest element
• Example:
for (i = 0; i < 100; i++) //Line 1
cin >> list[i]; //Line 2
Processing One-Dimensional
Arrays (cont’d)
Processing One-Dimensional
Arrays (cont’d)
Array Index Out of Bounds
• If we have the statements:
double num[10];
int i;
• Example:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
Partial Initialization of Arrays
During Declaration
• The statement:
int list[10] = {0};
declares list to be an array of 10 components and initializes all
of them 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
Partial Initialization of Arrays
During Declaration (cont’d)
• 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
• The statement:
int list[25]= {4, 7};
declares an array of 25 components; initializes list[0] to 4
and list[1] to 7; all other components are initialized to 0
Some Restrictions on Array
Processing
• Consider the following statements:
• Solution:
Some Restrictions on Array
Processing (cont’d)
• The following is illegal too:
• Solution:
• The following statements are legal, but do not give the desired
results:
Arrays as Parameters to
Functions
• Arrays are passed by reference only
• The symbol & is not used when declaring an array as a formal
parameter
• The size of the array is usually omitted
• If provided, it is ignored by the compiler
Constant Arrays as Formal
Parameters
Base Address of an Array and
Array in Computer Memory
• The base address of an array is the address, or memory location of
the first array component
• C++ does not allow functions to return a value of the type array
Searching an Array for a
Specific Item
• Sequential search or linear search
✓Searching a list for a given item
✓Continue the search until either you find the item or no more data
is left in the list to compare with searchItem
Searching an Array for a
Specific Item (cont’d)
Sorting
• Selection sort: rearrange the list by selecting an element in the list
and moving it to its proper position.
• The first time, we locate the smallest item in the entire list. The
second time, we locate the smallest item in the list starting from the
second element in the list, and so on.
Sorting (cont’d)
• Example:
✓'A' is the character A
✓"A" is the C-string A
o "A" represents two characters, 'A' and '\0‘
C-Strings (Character Arrays)
(cont’d)
• Consider the statement
char name[16];
• The statement
char name[] = "John";
✓Stores the next m characters into str but the newline character is
not stored in str
✓If the input string has fewer than m characters, the reading stops
at the newline character
String Output
• cout << name; outputs the content of name on the screen
✓<< continues to write the contents of name until it finds the null
character
✓If name does not contain the null character, then we will see
strange output
o << continues to output data from memory adjacent to name until '\0'
is found
Two- and Multidimensional
Arrays
• Two-dimensional array: collection of a fixed number of
components (of the same type) arranged in two dimensions
• Sometimes called matrices or tables
• Declaration syntax: