Introduction to Computer
Programming
CSE 021 Fall 2021
LECTURE 08: Arrays
Dr. Basma Hassan
[email protected]Logical Operators
• Logical operators are operators that can be used to create
complex boolean expressions
• Compound conditions
• && operator (logical AND) and || operator (logical OR):
• binary operators, connect two boolean expressions into a
compound boolean expression
• ! Operator (Negation):
• unary operator, reverses the truth of its boolean operand
2
The Logical AND(&&)Operator
• Takes two boolean expressions as operands
• Creates compound boolean expression that is true only when both
sub expressions are true
• Can be used to simplify nested decision structures
• Truth table for the && operator Expression Value of the
Expression
false and false false
false and true false
true and false false
true and true true
3
The Logical OR (||) Operator
• Takes two boolean expressions as operands
• Creates compound boolean expression that is true when either of
the sub expressions is true
• Can be used to simplify nested decision structures
• Truth table for the || operator Expression Value of the
Expression
false and false false
false and true true
true and false true
true and true true
4
The not Operator
• Takes one boolean expressions as operand and reverses its
logical value
• Sometimes it may be necessary to place parentheses around an
expression to clarify to what you are applying the not operator
• Truth table for the ! operator
Expression Value of the Expression
true false
false true
5
Short-Circuit Evaluation
• Deciding the value of a compound boolean expression after
evaluating only one sub expression
• Performed by the || and && operators
• For || operator:
• If left operand is true, compound expression is true. Otherwise,
evaluate right operand
• For && operator:
• If left operand is false, compound expression is false. Otherwise,
evaluate right operand
6
break and continue Statements
• The break and continue statements are used to alter the
flow of control
• break executed in a loop statement, immediately exits
from that statement
• Common uses of break are to escape early from a loop or skip the
remainder of a switch
• continue executed in a loop, skips the remaining
statements in that control statement’s body and performs
the next iteration of the loop
7
Repetition Structures Summary
while Iteration do…while Iteration for Iteration
8
Lecture Topics
• Introduction
• Arrays
• Defining Arrays
• Initializing Array Elements
• Examples Using Arrays
• Searching Arrays
• Sorting Arrays
9
Arrays
• Arrays are data structures consisting of related data items of
the same type
• An array is a group of elements of the same type stored
contiguously in memory
integer array called c, containing five elements
Array Elements
• To refer to a particular element, we specify:
• the array’s name followed by
• the element’s position number in square brackets ([ ])
• Example: c[3] refers to the 4th element in the array named c
• First element located at position number 0 (zero)
• Position number is called the element’s subscript (or index)
• Must be a non-negative integer or integer expression.
• A subscripted array name can be used on the left side of an
assignment.
11
Defining Arrays
• Specify its element type and number of elements so the
compiler may reserve the appropriate amount of memory
• Define five elements for integer array c, which has
subscripts in the range 0–4
• int c[5];
• Arrays are “static” entities
• they remain the same size throughout program execution.
• A char array can store a character string
Setting the Array’s Element Values with
Loops
• Example: set five-element integer array elements to zeros
int main(void) {
int n[5]; // n is an array of five integers
// set elements of array n to 0
for (int i = 0; i < 5; i++) {
n[i] = 0; // set element at location i to 0 Element Value
} 0 0
1 0
printf("Element\tValue\n"); 2 0
3 0
// output contents of array n in tabular format 4 0
for (int i = 0; i < 5; ++i) {
printf("%d\t%d\n", i, n[i]);
}
}
13
Array Initialization
• Uninitialized array elements contain garbage values
• Array’s elements can be initialized when defining the array by
providing a comma-separated list of array initializers in braces, { }
• If there are fewer initializers than elements in the array, the
remaining elements (rightmost) are initialized to zero
• If too many a syntax error is produced
• If size omitted, initializers determine size
• int n[] = {1,2,3,4,5};
• 5 initializers, therefore 5 element array
14
Initializing Array Elements with Initializer
List
• Example: initializes five-element array with the values 32, 27, 64, 18 and 95
int main(void) {
// use initializer list to initialize array n
int n[5] = { 32, 27, 64, 18, 95 };
printf("Element\tValue\n");
Element Value
0 32
// output contents of array n in tabular format
1 27
for (int i = 0; i < 5; ++i) {
2 64
printf("%d\t%d\n", i, n[i]);
3 18
}
4 95
}
15
Initializing Array Elements with Initializer
List (Fewer Elements)
• Example: initializes five-element array with the values 32, 27, 64, 18 and 95
int main(void) {
// use initializer list to initialize array n
int n[5] = { 32, 27, 64 };
printf("Element\tValue\n");
Element Value
0 32
// output contents of array n in tabular format
1 27
for (int i = 0; i < 5; ++i) {
2 64
printf("%d\t%d\n", i, n[i]);
3 0
}
4 0
}
16
Initializing Array Elements with Initializer
List (More Elements)
• Example: initializes five-element array with the values 32, 27, 64, 18 and 95
int main(void) {
// use initializer list to initialize array n
int n[5] = { 32, 27, 64, 18, 95, 55 };
printf("Element\tValue\n");
ERROR!
Too many initializers
// output contents of array n in tabular format
for (int i = 0; i < 5; ++i) {
printf("%d\t%d\n", i, n[i]);
}
}
17
Initializing Array Elements with Initializer
List (All Zeros)
• Example: initializes entire array elements with Zeros
• Explicitly initializes n[0] to 0 and implicitly initializes the remaining elements to 0
int main(void) {
// initializes entire array to zeros
int n[5] = { 0 };
Element Value
0 0
printf("Element\tValue\n");
1 0
2 0
// output contents of array n in tabular format
3 0
for (int i = 0; i < 5; ++i) {
4 0
printf("%d\t%d\n", i, n[i]);
}
}
18
Initializing Array Elements with Calculations
• Example: initializes five-element array with the values 2, 4, 6, 8 and 10
int main(void) {
int n[5]; // array n has five elements
for (int i = 0; i < 5; ++i) { // set the values
n[i] = 2 + 2 * i;
Element Value
}
0 2
1 4
printf("Element\tValue\n");
2 6
3 8
// output contents of array s in tabular format
4 10
for (int i = 0; i < 5; ++i) {
printf("%d\t%d\n", i, n[i]);
}
}
19
Summing the Elements of an Array
• Example: Computing the sum of the elements of an array
int main(void) {
// use an initializer list to initialize the array
int a[5] = { 1, 2, 3, 4, 5 };
int total = 0; // sum of array The total of a's
values is 15
// sum contents of array a
for (int i = 0; i < 5; ++i) {
total += a[i];
}
printf("The total of a's values is %d\n", total);
}
20
Graphing Array Element Values with Bar
Charts
• Example: Read numbers from an array and graphs the information in a bar chart (*)
// use initializer list to initialize array n
int n[5] = { 4, 2, 3, 1, 5 };
printf("Elm\tVal\tBar Chart\n");
// for each element of array n, output a bar of the bar chart
for (int i = 0; i < 5; ++i) {
printf("%d\t%d\t", i, n[i]); Elm Val Bar Chart
0 4 ****
for (int j = 1; j <= n[i]; ++j) { // print one bar 1 2 **
printf("%c", '*'); 2 3 ***
} 3 1 *
4 5 *****
puts(""); // end a bar with a newline
}
21
Searching Arrays
• The process of finding a key value in an array is called
searching
• A linear search algorithm compares each array element with
the search key.
• The array is not sorted, so it’s just as likely the value will be found
in the first element as in the last
• On average, the program will have to compare the search
key with half the array elements
• If the key value is found, the element’s subscript is
returned.
Searching Arrays: Linear Search
• Example: Finding a key value in an array
// Assume array a = {22, 87, 45, 36, 90, 12}
printf("Enter integer search key: ");
int searchKey = 0; // value to locate in array a
scanf("%d", &searchKey);
// attempt to locate searchKey in array a
Enter integer search
// loop through array
for (int n = 0; n < SIZE; ++n) { key: 36
if (a[n] == searchKey) { Found value at
// display location of key subscript 4
printf("Found value at subscript %d\n", n);
break;
}
}
23
• Sorting Arrays with Bubble Sort
24
End of Lecture!
Thanks for your Attention!
25