lecture10
lecture10
Array Examples
Character Arrays
Quick Review
2
Fill in the blanks with appropriate words
➢ When arguments are passed by ______, a copy of the argument’s value is made and passed to the called
function. Changes to the copy do not affect an original variable’s value in the caller.
value
➢ To use rand() function, we should import <_____.h> header.
stdlib
➢ We use ______ function to set a seed for the random number generation.
srand
➢ To randomize without entering a seed each time, use a statement like _______.
srand(time(NULL))
➢ A ________ function is a function that calls itself either directly or indirectly through another function.
recursive
➢ A ________ is the condition to stop the recursion.
base case
3
What is the
output?
4
Lecture 10.1
Arrays Introduction
5
6.1 Introduction
• The definition
• int b[100], x[27];
reserves 100 elements for integer array b and 27 elements for integer
array x.
• These arrays have indices in the ranges 0–99 and 0–26, respectively.
• Arrays may contain other data types.
• Character strings and their similarity to arrays are discussed in
Chapter 8. The relationship between pointers and arrays is discussed
in Chapter 7.
Array Examples
16
6.4 Array Examples
6.4.1 Defining an Array and Using a Loop to Set the Array’s Element
Values
• Like any other variables, uninitialized array elements contain garbage
values.
• Figure 6.3 uses for statements to initialize the elements of a five
integer array n to zeros and print the array in tabular format.
• The first printf statement displays the column heads for the two
columns printed in the subsequent for statement.
• If there are fewer initializers than elements in the array, the remaining
elements are initialized to zero.
• For example, the elements of the array n in Fig. 6.3 could have been
initialized to zero as follows:
// initializes entire array to zeros
int n[10] = {0};
• This explicitly initializes the first element to zero and initializes the
remaining nine elements to zero because there are fewer initializers
than there are elements in the array.
• If the array size is omitted from a definition with an initializer list, the
number of elements in the array will be the number of elements in
the initializer list.
• For example,
• int n[] = {1, 2, 3, 4, 5};
would create a five-element array initialized with the indicated values.
• The for loop takes the responses one at a time from the array
responses and increments one of the 10 counters (frequency[1]
to frequency[10]) in the frequency array.
• The key statement in the loop is
• ++frequency[responses[answer]];
which increments the appropriate frequency counter depending on
the value of responses[answer].
Character Arrays
58
6.5 Using Character Arrays to Store and Manipulate
Strings
• We now discuss storing strings in character arrays.
• So far, the only string-processing capability we have is outputting a
string with printf.
• A string such as "hello" is really an array of individual characters in C.
• A character array can be initialized using a string literal.
• For example,
• char string1[] = "first";
initializes the elements of array string1 to the individual characters in
the string literal "first".