Chapter 4 (Part I) - Array and Strings
Chapter 4 (Part I) - Array and Strings
Computer Programming
Chapter 4
Array and Strings
Chere L. (M.Tech)
Lecturer, SWEG, AASTU
Outline
Basic concepts of Array
Types of Array
One Dimensional Arrays
Multi-dimensional Arrays
Array declaration and initialization
Accessing and processing Array Elements
Basics of String
String declaration and initialization
String manipulation and operation
Input/output, Copying, Comparing, concatenation, etc.
String library functions and operators 2
Chapter 4
Part I
Array
3
Chapter 4
1. Basic concepts of Array
What is an array?
An array is a group of consecutive memory locations with same
name and data type.
So far we were dealing with scalar and atomic variable is a single
memory location with unique name and a type.
Unlike scalar and atomic variable an array is collection of different
adjacent memory locations.
Can be also referred as
Series of elements (variables) of the same type placed consecutively
in memory.
Memory collection to hold more than one value of the same types at
a time (it’s like of list of items).
Array is derived data structure which built up on primitive data types
4
Chapter 4
1. Basic concepts of Array (Cont’d)
In general array concept comprises the following
Array is a consecutive group of memory locations
All these memory locations referred by a collective name and
have the same data type (either primitive/non primitive).
Elements of array
– the memory locations in the array/value stored.
Length of array
– define the total number of elements in the array
– Must be constant natural number (most probably >=2)
The elements of array is accessed with reference to its position in
array, that is call index or subscript.
Both the length of array and index/subscript of an array are
specified within square bracket []
5
Chapter 4
1. Basic concepts of Array (Cont’d)
Array index range – starts from 0 and extend till the size of the array
minus one.
i.e. [0 length – 1 ]
6
Chapter 4
1. Basic concepts of Array (Cont’d)
Need of array - Consider the scenario given below…
Write a program that reads five numbers, and performs some
manipulations on these numbers, such as find their sum, and print the
numbers in reverse order, find the their mean and compute standard
deviation etc.
Solution:
We could use five individual variables of type int, but five variables
are hard to keep track of.
We could make program more readable by giving the variables
related names such as item1, item2, item3, and so forth (see next
slide)
However, this solution becomes absurd/challenging if the number
of items is very large (if we will have hundred list of numbers)
Therefore, the use of array is the best solution for such scenario
7
Chapter 4
1. Basic concepts of Array (Cont’d)
Array mainly applicable in large data set sort and search operations
The values stored in an array can be sorted easily.
The search process can be applied on arrays easily
9
Chapter 4
2. Types of Array
Mainly there are two types of array namely One dimensional and
Multi dimensional
10
Chapter 4
2. Types of Array (cont’d)
Two Dimensional Array
Simplest form of Multi-Dimensional array
It is a row and column based data structure
Referred to as a matrix or table
A matrix has two subscripts/indices, one denote the row and
another denotes the column.
In other words, 2D array is an array of 1D arrays
NOTE:
The elements field within brackets [] when declaring an array must be a
constant value, since arrays are blocks of static memory of a given size and
the compiler must be able to determine exactly how much memory it must
assign to the array before any instruction is considered. 14
Chapter 4
3.2) Initialization of 1D Array
Alternative 1: initializing array elements during array declaration
Syntax:
data_type identifier[length] = {V1, V2, …… Vn};
where n <= length and
V1, V2, . . Vn is list of initial values
Example 1:
int marks[5] = {34, 21, 2, 66, 567};
15
Chapter 4
3.2) Initialization of 1D Array
Example 2:
float arr[5] = {10, 50, 30}; //less initial value
Example 3:
float salary[3] = {0}; //initialize all array
float salary[3] = {}; //elements to zero
Example:
float salary[ ]={25, 35, 45, 53, 25, 7}; //array length = 6
17
Chapter 4
3.2) Initialization of 1D Array (Cont’d)
Alternative 3: initializing after array declaration
After declaration array elements can be initialized individually by
specifying their index.
Most probably it is a kind of run-time initialization
NOTE:
The row size and column size specified with brackets [] when declaring an array
must be a constant value, since arrays are blocks of static memory of a given size
and the compiler must be able to determine exactly how much memory it must
assign to the array before any instruction is considered
20
Chapter 4
3.4) Initialization of 2D Array
Alternative 1: initializing array elements during array declaration
Syntax (option 1): just list array elements within curly brace
- data_type identifier[rowSize][colSize] = {V1, V2, V3, … Vn};
where n <= array Length, and i, k refers to row indices
NOTE:
Grouping elements are advantages to make partial initialization.
22
Chapter 4
3.4) Initialization of 2D Array (Cont’d)
Alternative 2: omitting array size while initializing array elements
Syntax : data_type identifier[][colSize] = {V1, V2, … Vn};
- also referred as Unsized array
Example:
int theArray[ ][3] ={ 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11,
rowSize =
12, 13, 14, 15, 16}; 16/3 = 5.11 = 6
int myArray[ ][3] ={ 4, 5, 6, 7, 8, 9, 10, 11, 12}; // rSize = 9/3 = 3
int stud_Array[ ][3] = { {1,2}, {3,4}, {5} } ; // rSize = #group = 3
int item[][] = { {1,2}, {3,4} } ; // invalid.
NOTE:
In all multidimensional array it must have bounds for all dimensions
except the first.
23
Chapter 4
3.4) Initialization of 2D Array (Cont’d)
Alternative 3: initializing after array declaration
After declaration array elements can be initialized individually
by specifying their index.
Most probably it is a kind of run-time initialization
Example:
Design a program that read and stores the mark of SWEG
2012 batch of all courses they are taking semester wise.
Solution
Dimension 1 can be refer to Student list, like student ID
Dimension 2 can be refer to academic semester
Dimension 3 can be refer to list of courses the students take
in a semester
25
Chapter 2
3.5) Accessing/processing Array Elements
The values of an array can be access using array index only.
arrayName[index]; //1D array
arrayName [rowIndex][colIndex] //2D array
30
Chapter 4
Example program 1
A Program to demonstrate 1D array Declaration, initialization, manipulation
and processing
31
Chapter 4
Example program 1 (cont’d)
32
Chapter 4
Example program 2
Program to read five numbers, find their sum, and print the numbers in reverse
order
33
Chapter 4
Example program 3
A program to demonstrate Array size declaration as constant
34
Chapter 4
Exercises 1
Design a program that read two matrix and perform the following
Print each input matrix in tabular format
Find the sum of the two matrix and print the resulting matrix
in tabular format
35
Chapter 4
Exercise 1 – Solution
36
Chapter 4
Exercise 1 – Solution (cont’d)
37
Chapter 4
Practical Exercises 1 - Array
1. Write array declarations, including initializers, for the following:
a) A list of 10 integer voltages: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82.
b) A list of 100 double-precision distances; the first six distances are
6.29, 6.95, 7.25, 7.35, 7.40, and 7.42.
c) A list of 64 double-precision temperatures; the first 10 temperatures
are 78.2, 69.6, 68.5, 83.9, 55.4, 67.0, 49.8, 58.3, 62.5, and 71.6.
d) A list of 15 character codes; the first seven codes are f, j, m, q, t, w,
and z.
2) Write a program to declare a 4-by-5 array of integers and initialize
with the data 16, 22, 99, 4, 18, -258, 4, 101, 5, 98, 105, 6, 15, 2,
45, 33, 88, 72, 16, and 3.
3) Write a program to input eight integer numbers into an array
named temp. As each number is input, add the number into a
total. After all numbers are input, display the number and their
average.
38
Chapter 2
Reading Resources/Materials
Chapter 11 & 12:
Diane Zak; An Introduction to Programming with C++ (8th
Edition), 2016 Cengage Learning
Chapter 7:
Walter Savitch; Problem Solving With C++ [10th
edition, University of California, San Diego, 2018
Link:
https://www.w3schools.in/category/cplusplus-
tutorial/
39
Thank You
For Your Attention!!
40