Array and String
Array and String
Fundamental programming II 1
Overview
• Introduction to Arrays
• Initializing Arrays
• Programming with Arrays
• Sorting an array using selection
sort
• Array and loop
• Multidimensional Arrays
• C-style strings
Fundamental programming II 2
Introduction to Arrays
• An array is an aggregate data type that
lets you access multiple variables
through a single name by use of an index.
In C++, all of these variables must have
the same type.
• An array is used to process a collection
of data of the same type
Examples: A list of names
A list of temperatures
Fundamental programming II 3
Introduction to Arrays
(continued)
Why do we need arrays?
Imagine keeping track of 30 test scores,
or 100, or 1000 in memory
How would you name all the variables?
How would you process each of the
variables?
Fundamental programming II 4
Declaring an Array
• Consider the case where you want to record the
test scores for 30 students in a class. To do
so, you would have to allocate 30 variables!
int nTestScoreStudent1;
int nTestScoreStudent2;
int nTestScoreStudent3;
// ...
int nTestScoreStudent30;
• Arrays give us a much easier way to do this:
– int anTestScores[30]; // allocate 30 integers
Fundamental programming II 5
Declaring an Array
Fundamental programming II 10
Indexed Variable
Assignment
• To assign a value to an indexed
variable, use
the assignment operator:
int n = 2;
score[n + 1] = 99;
– In this example, variable score[3] is
assigned 99
Example of Array
Fundamental programming II 12
Arrays - Introduction
0 69
1 61
index
2 70
3 89 values
4 23
5 10
6 9
13
Loops And Arrays
• for-loops are commonly used to step through
arrays
First index is 0 Last index is (size – 1)
– Example:for (i = 0; i < 5; i++)
{
cout << score[i] << " off by "
• Omitted Size
– If you are initializing an array of
elements using an initializer list, the
compiler can figure out the size of the
array for you, and you can omit
explicitly declaring the size of the
array:
– int anArray[] = { 0, 1, 2, 3, 4 }; //
declare array of 5 elements
Fundamental programming II 31
Default Values
• If too few values are listed in an
initialization
statement
– The listed values are used to initialize
the first of the indexed variables
– The remaining indexed variables are
initialized to a zero of the base type
– Example: int a[10] = {5, 5};
initializes a[0] and a[1] to 5 and
a[2] through a[9] to
0
Un-initialized Arrays
• Can you
– Describe the difference between a[4]
and int a[5]?
Fundamental programming II 35
One-Dimensional Arrays
(continued)
• All the temperatures in the list are
double-precision numbers and must be
declared as such. However, each item in
the list doesn’t have to be declared
separately.
• The items in the list can be declared as a
single unit and stored under a common
variable name called the array name. For
example, temp is used as the name for this
list, and the declaration statement double
temp[5];
Fundamental programming II 36
One-Dimensional Arrays
(continued)
• Good programming practice requires
defining number-of-items in the array as a
constant before declaring the array. So in
practice, the previous array declaration
for temp would be declared with two
statements, as in these examples:
– const int NUMELS = 5; // define a
constant for the number of items
– double temp[NUMELS]; // declare the
array
Fundamental programming II 37
One-Dimensional Arrays
(continued)
• The following are other examples of array
declarations using this two-line syntax:
const int NUMELS = 6;
int volts[NUMELS];
const int ARRAYSIZE = 4;
char code[ARRAYSIZE];
const int SIZE = 100;
double amount[SIZE];
• In these declaration statements, each array is
allocated enough memory to hold the number of
data items specified in the declaration
statement.
Fundamental programming II 38
Arrays and Enums
Fundamental programming II 39
One-Dimensional Arrays
(continued) Example
enum StudentNames
{
KENNY, // 0
KYLE, // 1
STAN, // 2
BUTTERS, // 3
CARTMAN, // 4
WENDY, // 5
MAX_STUDENTS // 6
};
int anTestScores[MAX_STUDENTS]; // allocate 6
anTestScores[STAN] = 76;
Fundamental programming II 40
Exercise
Fundamental programming II 41
Multi-Dimensional Arrays
Fundamental programming II 48
Sorting an array using
selection sort (continued)
Fundamental programming II 49
Exercise
Fundamental programming II 50
C-Strings
Fundamental programming II 51
C-Strings
Fundamental programming II 52
Strings (continued)
Fundamental programming II 53
Strings (continued)
Fundamental programming II 54
Strings (continued)
Some examples of string constants in C++ are:
"hello there"
"I like C++."
"#$%§@@+*"
"\""
"\"\""
"\\"
""
The null string, ““, only contains the null
terminator and represents
the empty string.
Fundamental programming II 55
Reading a String from
the Keyboard
• How to read a string entered from the keyboard?
• Make an array, that will receive the string, the
target of a cin stream.
• The following program reads (part of) a string
entered by the user:
#include <stdio.h>
int main() {
char str[80];
cout << “Enter a string: “;
cin >> str; // read string from keyboard
cout << “Here is your string: “;
cout << str;
return(0);}
Fundamental programming II 56
Strings (continued)
Fundamental programming II 57
Strings (continued)
Fundamental programming II 58
Some C++ Library Functions
for Strings (continued)
Fundamental programming II 59
Some C++ Library Functions
for Strings (continued)
Fundamental programming II 62
Fundamental programming II 63
Some C++ Library Functions
for Strings (continued)
Fundamental programming II 64
Fundamental programming II 65
Using the Null
Terminator
Fundamental programming II 66