Chapter 03 - Arrays
Chapter 03 - Arrays
Topics to cover:
1
Arrays Data Types
• Data types are of two kinds:
- simple data types (e.g. int, float, double, char)
- Structured data type: (e.g. arrays)
• An array is a collection of two or more adjacent
memory cells, called array elements, that are
associated with a particular symbolic name.
2
One-Dimensional Arrays
• Declaration of one-dimension array
Syntax: atype aname [ size ] ; // uninitialized array
atype aname [ size ] = { initialization list } ;
where
atype is any data type;
aname is the name given to the array;
size represents the number of elements in the array.
initialization list is the list of initial values given to
the array.
3
One-Dimensional Arrays Examples
• EX1: int x [ 3 ];
- This tells the compiler to associate 3 memory cells
with name x.
- These cells will be adjacent to each other in memory.
- Each element of array x contains a value of integer
type
• EX2: int Y [ 4 ] = { 2, 4, 6, 8 } ;
This initializes array Y to have 4 elements which
contain 2, 4, 6, 8.
4
Accessing One-Dimensional Array
int x [3] ;
Array x in memory:
24 20 10
Array x in memory:
Index 0 1 2
Values
24 20 10
7
Examples on One-Dimensional Arrays
Example2: ReadListOfIntegers
#include <iostream>
using namespace std;
void main ()
{
const int ListSize = 10;
int L [ListSize];
0 1 2 3 4
B 7 6 10 13 23
0 1 2 3 4
12 16 25 33 48
C
10
Example 4
Example 4:
Write a C++ program to read 10 integers and store them
in array A. Then it finds the even numbers to store them
in array B and the odd numbers to store them in array C.
11
Example 4 (Cont.)
// File arrays.cpp
#include <iostream.h>
void main ( )
{ int i;
int A [10], B[10], C[10] ;
cout << “ Enter 10 integers : “ ;
for (i = 0 ; i <10; i++)
{ cin >> A[i] ;
if (A[i] % 2 == 0)
B[i] = A[i] ;
else
C[i] = A[i];
}
cout << “B element = “ << “ C element = “ << endl;
for (i = 0; i < 10; i++)
{ cout << B[i] << “ “ << C[i] << endl ;
}
}
12
Examples on One-Dimensional Arrays
Example 5:
The resultant arrays B and C in example 2 contain data
stored in non consecutive locations in the arrays.
Modify the program of example 4 so that the data are
stored in consecutive locations.
13
Example 5 (Cont.)
/ File arrays.cpp
#include <iostream.h>
void main ( )
{ int i, j = 0 , k = 0 ;
int A [10], B[10], C[10] ;
for ( i= 0 ; i <10; i++)
{ cin >> A[i] ;
if (A[i] % 2 == 0)
{ B[j]=A[i];
j ++ ; }
else
{ C [k] = A [ i ];
k ++ ; }
}
cout << “B element = “ << “ C element = “ << endl ;
for (i=0; i<10; i++)
{ cout << “B[i] << “ “ << “ “ << C[i] << endl ; }
}
14
Example 6
The problem:
Write C++ program that searches for an integer in array of 10
integers. A proper message should be printed out if the number is
found or not.
The Analysis:
A given array of integer numbers is going to be searched in order
to find a given number.
Requirements:
Input: an integer number n, an array A of 10 integers
Output: a message “yes found” or “no not found” according to
weather the number is found or not.
15
The C++ Program for Example 6
//File search.cpp
#include <iostream.h>
void main ( )
{ int n, i ; int A [ 10 ] ;
bool flag = false;
cout << “ Enter the number that you ant to search for: “ ;
cin >> n ;
cout << “ Enter 10 integers: “ ;
for ( i = 0 ; i < 10 ; i++ )
{ cin >> A[i] ;
if ( A[i] == n )
{ flag = true ; break ; } }
if ( flag == true )
cout << “ Yes, the number is found \n” ;
else cout << “ No, the number is not found \n” ;
16
}
Arrays of Two-Dimensions
• Syntax ( in C++ ):
atype aname [nrows] [ncolumns] ;
• Example:
int B [2] [3] ; // declaring array B of 2 rows and 3 columns
int A [3][3] = {1,2,3,4,5,6,7,8,9}; //initializing array A
Array B in diagram:
0 1 2
0
1
//File diagonal.cpp
#include <iostream>
using namespace std;
void main ( )
{ int i, j , product = 1 ;
int B[3][3];
cout << "Enter the 9 array elements: " ;
for ( i = 0 ; i <3 ; i++ )
for ( j = 0 ; j < 3 ; j++)
cin >> B[i][j] ;
// Process the array now
for ( i = 0 ; i < 3 ; i++)
for ( j = 0 ; j < 3 ; j++ )
if ( i == j )
product = product * B[i][j] ;
cout << " The product of the diagonal elements = " << product << endl;
}
20
Example3 of Two-Dimensional Array
•Anther way to solve Example 3
//File diagonal.cpp
#include <iostream>
using namespace std;
void main ( )
{ int i, j , product = 1 ;
int B[3][3];
cout << "Enter the 9 array elements: " ;
for ( i = 0 ; i <3 ; i++ )
for ( j = 0 ; j < 3 ; j++)
cin >> B[i][j] ;
// Process the array now
cout << " The product of the diagonal elements = " << product << endl;
21}
Example4 of Two-Dimensional Array
• Write a C++ program that reads an array of size (3 x 3) and finds the product of the right diagonal
elements.
//File diagonal.cpp
#include <iostream>
using namespace std;
void main ( )
{ int i, j , product = 1 ;
int B[3][3];
cout << "Enter the 9 array elements: " ;
for ( i = 0 ; i <3 ; i++ )
for ( j = 0 ; j < 3 ; j++)
cin >> B[i][j] ;
// Process the array now
for ( i = 0 ; i < 3 ; i++)
for ( j = 0 ; j < 3 ; j++ )
if (j == (-1 * i) + 2 )
product = product * B[i][j] ;
cout << " The product of the right diagonal elements = " << product << endl;
}
22
Example4 of Two-Dimensional Array
•Another way to solve Example4.
//File diagonal.cpp
#include <iostream>
using namespace std;
void main ( )
{ int i, j , product = 1 ;
int B[3][3];
cout << "Enter the 9 array elements: " ;
for ( i = 0 ; i <3 ; i++ )
for ( j = 0 ; j < 3 ; j++)
cin >> B[i][j] ;
// Process the array now
j=2, product=1;
for(int i=0; i<3; i++)
{
product*=B[i][j];
j--;
}
23cout << " The product of the right diagonal elements = " << product << endl; }
Example5 of Two-Dimensional Array
•Write a C++ program that initializes a 4 X 4 array A, and transpose it into array B.
#include <iostream>
using namespace std;
void main ( )
{ int A[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16};
int B[4][4];
int i, j;
for (i=0; i<=3;i++)
{ for(j=0;j<=3;j++)
cout<<" "<<A[i][j]<<" ";
cout<<endl; }
cout<<endl<<endl;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
B[j][i]=A[i][j];
for (i=0; i<=3;i++)
{ for(j=0;j<=3;j++)
cout<<" "<<B[i][j]<<" ";
cout<<endl;}
cout<<endl<<endl; }
24
Declaring an array size with an identifier
Or a constant:
To define a dynamic array (i.e. its size is decided during the program
execution), you will need to use pointers. This will be discussed
later.
25
Array as a parameter in a function
• Array name is pointer (call by reference)
• When an array is sent to a function as a parameter, it is always a call by reference.
#include <iostream.h>
void Increment (int a[]) {
for (int i=0; i<5; i++)
a[i] += 10; }
void main() {
int b[5] = {10,20,30,40,50};
Increment(b);
for(int i=0; i<5; i++)
cout<<b[i]<<'\t';
cout<<endl;
}
26
Array as a parameter in a function
• Modify the previous example, so that the function Increment can receive any array
of any size.
#include <iostream.h>
void Increment (int a[], int arr_size) {
for (int i=0; i<arr_size; i++)
a[i] += 10; }
void main() {
int b[5] = {10,20,30,40,50};
Increment(b, 5);
for(int i=0; i<5; i++)
cout<<b[i]<<'\t';
cout<<endl;
}
27
Array element as a parameter in a
function
- Array element as a parameter (call by value).
#include <iostream.h>
void Inc_element(int a) { ++a; }
void main() {
int b[5] = {10,20,30,40,50};
Inc_element(b[1]);
cout<<b[1]<<endl;
}
28
Array element as a parameter in a
function
- Array element as a parameter (call by reference).
#include <iostream.h>
void Inc_element(int &a) { ++a; }
void main() {
int b[5] = {10,20,30,40,50};
Inc_element(b[1]);
cout<<b[1]<<endl;
}
29
Swapping two arrays using a
function
#include <iostream> void main ()
using namespace std; { int x[5] = {10,20,30,40,50};
int y[5] = {60,70,80,90,100};
void swaparray (int a[], int b[], int arr_size) cout << "Before swap "<<endl<<endl<<"Array x"<<endl;
for (int i=0; i<5; i++)
{ int temp;
cout << x[i]<< " ";
for (int i=0; i<arr_size; i++)
cout<< endl<<endl<<"Array y"<<endl;
{ temp = a[i];
for (int j=0; j<5; j++)
a[i] = b[i]; cout << y[j]<< " ";
b[i] = temp;} cout<< endl<<endl;
} swaparray(x, y, 5);
cout << "After swap "<<endl<<endl<<"Array x"<<endl;
for (int i=0; i<5; i++)
cout << x[i]<< " ";
cout<< endl<<endl<<"Array y"<<endl;
for (int j=0; j<5; j++)
cout << y[j]<< " ";
cout<< endl<<endl;
}
30
Pointer Arithmetic
• Increment / decrement pointers ( ++ or -- )
• Add / subtract an integer to/from a pointer
( + or += , - or -= )
• Pointers may be subtracted from each other
• Pointer arithmetic is meaningless unless
performed on an array
31
Pointer Arithmetic (Cont.)
• Example
Consider an integer array V of 5 elements, and an integer pointer vPtr.
• Subtracting pointers
- Returns the number of elements between two
addresses
33
Relations Between Pointers and Arrays
34
Relations Between Pointers and Arrays (Cont.)
35
Example1: Pointers as arrays
#include <iostream>
using namespace std;
void main ()
{
int A[5] = {10,20,30,40,50};
int *p;
p= A;
cout <<"First element of the array "<<*p<<endl;
cout <<"Fourth element of the array "<<*(p+3)<<endl;
}
36
Example2: Pointers as arrays
#include <iostream>
using namespace std;
void main ()
{
int A[5] = {10,20,30,40,50};
int *p;
p= &A[0];
for (int i=0; i<5; i++)
cout << *(p+i) << " ";
cout << endl;}
}
37
Example3: Pointers as arrays
#include <iostream>
using namespace std;
void main ()
{
int A[5] = {10,20,30,40,50};
int *p;
p = A;
cout << p++ << endl; //this will print the current address of the
//pointer, then moves to the next location
cout << ++p << endl; //this will move the pointer to the next location
//and then prints the new address
cout << (*p)++<<endl; //this will print the value that this pointer points
//to, and then increment this value
}
38
Dynamic Array using Pointers
Example1
#include <iostream>
using namespace std; err_size
void main ()
{ int err_size;
Reading
cin >> err_size; into array
int *d = new int[err_size];
Output
for (int i=0; i<err_size; i++)
cin >> d[i];
cout<< endl;
The same
for (int i=0; i<err_size; i++) Output
cin >> *(d+i);
cout<< endl;
41