IMD238
Programming for Information Professional
CHAPTER 7:
Introduction to Arrays
2 ARRAYS
[LEARNING OUTCOMES]
At the end this class, students should able:
What is array?
Array declaration
Array initialization
Accessing individual elements of an array
Character Arrays
Passing array to FUNCTION
3 WHAT IS ARRAY?
• Array: a collection of a fixed number of components wherein all of
the components have the same data type
• In a one-dimensional array, the components are arranged in a list form
• Syntax for declaring a one-dimensional array:
***intExp evaluates to a positive integer
4 ARRAYS (CONTINUED)
• Example:
int num[5];
dataType arrayName intExpr
5 C++ ARRAYS START AT 0 !!!!!!!
• The first element is the 0th element!
• If you declare an array of n elements, the last one is number
n-1.
• If you try to access element number n it is an error!
6 ACCESSING ARRAY
COMPONENTS
7 ACCESSING ARRAY
COMPONENTS (CONTINUED)
8 ACCESSING ARRAY
COMPONENTS (CONTINUED)
9 ARRAY DECLARATION
int list[100]; //array of size 100
const int NO_OF_EMPLOYEES = 6;
int hours[NO_OF_EMPLOYEES];
element_type array_name[number_of_elements];
The contents of each element are of the same type.
Could be an array of int, double, char, …
PROCESSING ONE-DIMENSIONAL ARRAYS
10
Consider the declaration
int list[100]; //array of size 100
int i;
Using for loops to access array elements:
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
Example:
for (i = 0; i < 100; i++) //Line 1
cin >> list[i]; //Line 2
11 ARRAY INITIALIZATION
• You can initialize an array when you declare it (just like with
variables):
• int foo[5] = { 1,8,3,6,12};
• double d[2] = { 0.707, 0.707};
• char s[] = { 'R', 'P', 'I' };
You don’t need to specify a size when initializing, the
compiler will count for you.
12 ARRAY INITIALIZATION DURING
DECLARATION
• Arrays can be initialized during declaration
• In this case, it is not necessary to specify the size of the array
• Size determined by the number of initial values in the braces
• Example:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
PARTIAL INITIALIZATION OF ARRAYS DURING
DECLARATION
13
• 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
14
PARTIAL INITIALIZATION OF ARRAYS DURING
DECLARATION (CONTINUED)
• 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
15 ACCESSING ARRAY
COMPONENTS (CONTINUED)
16 EXAMPLE
17 EXAMPLE OF ARRAYS
void main()
{
int facs[10];
for (int i=0;i<10;i++)
facs[i] = factorial(i);
for (int i=0;i<10;i++)
cout << "factorial(" << i << ") is “<<facs[i]
<< endl;
}
18 ARRAYS - EXERCISE 01
const int SIZE = 10;
double gpa[SIZE]; // holds up to SIZE gpas
int i; // index into array
cout << "Enter " << SIZE << " gpas: ";
for(i = 0; i < SIZE; i++)
cin >> gpa[i];
gpa[4] = 3.7;
Based on the program above, illustrate how gpa look like in a memory.
19 EXERCISE 01 - ANSWER
• Assume the input is 4.0 for everyone. Below is a pictorial
representation of what gpa would look like.
20 ARRAYS - EXERCISE 02
Given the following declaration:
const int N = 50;
int A[N]; // assume declaration
• Assign 7 to the first element and -25 to the last element
in array A
21 EXERCISE 02 - ANSWER
Given the following declaration:
const int N = 50;
int A[N]; // assume declaration
• Assign 7 to the first element and -25 to the last element in array A
A[0] = 7;
A[N-1] = -25; // better than A[49] = -25
22 ARRAYS - EXERCISE 03
• Print out the 4th through 9th elements, position 3 through
position 8
Answer:
for(i = 3; i < 9; i++)
cout << A[i] << " ";
cout << endl;
23 ARRAYS - EXERCISE 04
• Read in array elements with odd indexes
Answer:
for(i = 1; i < N; i += 2)
cin >> A[i];
24 ARRAYS - EXERCISE 05
• Print out all positive values one per line:
Answer:
for(i = 0; i < N; i++)
if(A[i] > 0)
cout << A[i] << endl;
25 INITIALIZING CHARACTER
ARRAYS
char city[] = {'D', 'a', 'l', 'l', 'a', 's'};
char city[] = "Dallas";
This statement is equivalent to the preceding statement, except that
C++ adds the character '\0', called the null terminator, to indicate
the end of the string. Recall that a character that begins with the
back slash symbol (\) is an escape character.
'D' 'a' 'l' 'l' 'a' 's' '\0'
city[0] city[1] city[2] city[3] city[4] city[5] city[6]
26 PRINTING CHARACTER ARRAY
For a character array, it can be printed using one
print statement. For example, the following code
displays Dallas:
char city[] = "Dallas";
cout << city;
27 PASSING ARRAYS TO
FUNCTIONS
• Just as you can pass single values to a function, you can also pass an entire array to a function. Below example
used to demonstrate how to declare and invoke this type of functions.
• #include <iostream.h>
void printArray(int list[], int arraySize); // function prototype
void main()
int numbers[5] = {1, 4, 3, 6, 8};
printArray(numbers, 5);
void printArray(int list[], int arraySize)
for (int i = 0; i < arraySize; i++)
cout << list[i] << " ";
}
28 PASSING SIZE ALONG WITH
ARRAY
• Normally when you pass an array to a function,
you should also pass its size in another argument.
So the function knows how many elements are in
the array.
• Otherwise, you will have to hard code this into
the function or declare it in a global variable.
Neither is flexible or robust.
29 Two-Dimensional Arrays
• A two-dimensional array, also known as a 2D array, is a
collection of data elements arranged in a grid-like structure
with rows and columns.
• Each element in the array is referred to as a cell and can be
accessed by its row and column indices/indexes.
• The syntax for declaring two-dimensional array is :
datatype arrayName[intExp1] [intExp2];
double sales [10][5];
CONT..
30 datatype arrayName[intExp1] [intExp2];
double sales [10][5];
This statement declares a two-dimensional array sales of 10 rows and 5 columns, in
which every component is of type double. Rows are numbered 0..9, and colums 0..4
[0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
31 Iniatializing 2D arrays
int arr[4][2] = {
{1234, 56},
{1212, 33},
{1434, 80},
{1312, 78}
};
Initializing 2D array arr with 4 rows and 2 columns
Equivalent to this initialization :
int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
32 Printing a 2D Arrays i C++
#include<iostream>
using namespace std;
main( )
{
int arr[4][2] = {
{ 10, 11 },
{ 20, 21 },
{ 30, 31 },
{ 40, 41 }
};
int i,j;
cout<<"Printing a 2D Array:\n";
for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
cout<<"\t"<<arr[i][j];
}
cout<<endl;
}
}
….as user input
#include<iostream>
33 using namespace std;
main( )
{
int s[2][2];
int i, j;
cout<<"\n2D Array Input:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"\
ns["<<i<<"]["<<j<<"]= ";
cin>>s[i][j];
}
}
cout<<"\nThe 2-D Array is:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"\t"<<s[i]
[j];
}