ARRCAYS 
One-dimensional Array
One-dimensional array 
A one-dimensional array is a list related values. (Lab. Manual, Page 92) 
You are knowledgeable & experience of the following: 
1. Declare one-dimensional array 
2. Initialize one-dimensional array 
3. Understand the initialization and declaration of a one-dimensional array 
4. Perform fundamental operations of a one-dimensional array 
An array is a series of elements of the same type. 
It means that, we can store 10 values of type INT in an array without having to declare 10 different 
values of the same type INT. 
Example: 
int a1; 
int a2; 
int a3; 
and so on… 
Instead this way  int a[10];
Declaration of an array 
Like regular variable, an array must be declared before it is used. 
Type name[elements] 
Where type is a valid type (int, float,) 
Where name is a valid identifier 
Where elements specifies the size of an array 
Example: 
int testArray[10]; 
An individual element of an array is accessed through index. 
An index describes the position of an element within an array. 
Arrays have zero as the index of the first element. 
In the example. 
How many elements? 
What is the first element? 
What is the last element?
Initializing array 
To assign initial values to each one of its elements by 
enclosing the values in curly braces { }. 
Example: 
int ArraySample[5] = {10,20,30,40,50}; 
The compiler will identify or assume the size of an array 
that will matches the number of values that 
Is included between the curly braces. 
After declaration the above example, 
array sample would be 5 int long, 
because we initialized 5 values.
Accessing the values of an array 
We can access the value of any elements individually like a normal variable. 
Format/Syntax: 
Name[index]; 
Example: 
ArraySample[2]; 
To store a value 
ArraySample[2]=70; 
To pass a value 
a = ArraySample[2]; 
Previous example: 
int ArraySample[5] = {10,20,30,40,50}; 
What is the last element? 
What is the value of second element? 
If we write/access ArraySample[5]. You are accessing 
out-of-range elements. 
Where you experience the error? compilation or runtime?
Example program (Problem) 
Create a program that will allow a user to enter four scores of 
the quizzes and display the average. 
See lab manual page 95 
Output: 
Display the average of the four quizzes on the screen using for loop statement.
•A multi-dimensional arrays 
described as “arrays of arrays” 
Two dimensional Arrays
At the end of this topic, you should be able to: 
• Declare a two-dimensional array 
• Perform fundamentals operations on a two-dimensional array 
• Use parameters for a two-dimensional arrays
The simplest form of the multidimensional array is a two –dimensional array. 
A two-dimensional array is a list of one-dimensional arrays. 
A two-dimensional array can be understood as a table containing rows and columns. 
Format/Syntax: 
type name[rows][columns]; 
Example: to declare an array 
int arraySample[a][b]; 
Where type can be any valid c++ data type 
Where arraySample can be a valid identifier 
Where [a] and [b] is the rows and columns 
Two-dimensional arrays are declared by specifying the number of rows and columns.
A two-dimensional array can be think as a table which will have an X number of rows 
and Y number of columns. 
Example below is a two-dimensional array which contains three rows and four columns. 
myArray[rows][columns]; 
Column 0 Column 1 Column 2 Column 3 
Row 0 myArray [0][0] myArray [0][1] myArray [0][2] myArray [0][3] 
Row 1 myArray [1][0] myArray [1][1] myArray [1][2] myArray [1][3] 
Row 2 myArray [2][0] myArray [2][1] myArray [2][2] myArray [2][3] 
Every element in array myArray is identified by an element name of the 
form myArray[rows][columns] 
Where myArray is the name of the array. 
Where [rows] and [columns] are the subscript that uniquely identified 
each element in myArray.
Initializing two-dimensional arrays 
You can specify initial values by enclosing each row in curly braces { }. 
Example 1: 
char board[3][3] = {{‘x’, ‘x’, ‘o’}, 
{‘o’, ‘o’, ‘x’}, 
{‘x’, ‘o’, ‘ ‘}}; 
If some elements are omitted in the 
initialization list, they are set to zero. 
Example 2: 
int a[3][4]= {{0,1,2,3}, initialize for row indexed by 0 
{4,5,6,7}, initialize for row indexed by 1 
{8,9,10,11}}; initialize for row indexed by 2 
The nested braces, which indicate the 
ntended row are optional. 
You can write as: 
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Subscripting two-dimensional array elements 
Note: Passing over all elements of a two-dimensional array is usually done with nested loops. 
Write subscript as x[row][col]. (Lab Manual page 100) 
Accessing two-dimensional array elements 
An element in two-dimensional array is accessed by using the subscript, 
ROW Index and COLUMN index of the array. 
EXAMPLE: 
int val = a[2][3] // this will take 4th element from the 3rd row of the array. To verify page 99.
Example program (Problem) Page 101 
#include <iostream> 
using namespace std; 
int main() 
{ 
int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}}; 
for(int n=0;n<5;n++) 
for(int m=0;m<2;m++) 
{ 
cout<<"a["<<n<<"]["<<m<<"]"; 
cout<<a[n][m]<<endl; 
} 
system("pause"); 
return 0; 
} 
OUTPUT 
|name| rows|columns|values| 
a[0][0]0 
a[0][1]0 
a[1][0]1 
a[1][1]2 
a[2][0]2 
a[2][1]4 
a[3][0]3 
a[3][1]6 
a[4][0]4 
a[4][1]8 
R|C 
{0,0} 
{1,2} 
{2,4} 
{3,6} 
{4,8}
Assignment Lab Manual page 103

Arrays

  • 1.
  • 2.
    One-dimensional array Aone-dimensional array is a list related values. (Lab. Manual, Page 92) You are knowledgeable & experience of the following: 1. Declare one-dimensional array 2. Initialize one-dimensional array 3. Understand the initialization and declaration of a one-dimensional array 4. Perform fundamental operations of a one-dimensional array An array is a series of elements of the same type. It means that, we can store 10 values of type INT in an array without having to declare 10 different values of the same type INT. Example: int a1; int a2; int a3; and so on… Instead this way  int a[10];
  • 3.
    Declaration of anarray Like regular variable, an array must be declared before it is used. Type name[elements] Where type is a valid type (int, float,) Where name is a valid identifier Where elements specifies the size of an array Example: int testArray[10]; An individual element of an array is accessed through index. An index describes the position of an element within an array. Arrays have zero as the index of the first element. In the example. How many elements? What is the first element? What is the last element?
  • 4.
    Initializing array Toassign initial values to each one of its elements by enclosing the values in curly braces { }. Example: int ArraySample[5] = {10,20,30,40,50}; The compiler will identify or assume the size of an array that will matches the number of values that Is included between the curly braces. After declaration the above example, array sample would be 5 int long, because we initialized 5 values.
  • 5.
    Accessing the valuesof an array We can access the value of any elements individually like a normal variable. Format/Syntax: Name[index]; Example: ArraySample[2]; To store a value ArraySample[2]=70; To pass a value a = ArraySample[2]; Previous example: int ArraySample[5] = {10,20,30,40,50}; What is the last element? What is the value of second element? If we write/access ArraySample[5]. You are accessing out-of-range elements. Where you experience the error? compilation or runtime?
  • 6.
    Example program (Problem) Create a program that will allow a user to enter four scores of the quizzes and display the average. See lab manual page 95 Output: Display the average of the four quizzes on the screen using for loop statement.
  • 7.
    •A multi-dimensional arrays described as “arrays of arrays” Two dimensional Arrays
  • 8.
    At the endof this topic, you should be able to: • Declare a two-dimensional array • Perform fundamentals operations on a two-dimensional array • Use parameters for a two-dimensional arrays
  • 9.
    The simplest formof the multidimensional array is a two –dimensional array. A two-dimensional array is a list of one-dimensional arrays. A two-dimensional array can be understood as a table containing rows and columns. Format/Syntax: type name[rows][columns]; Example: to declare an array int arraySample[a][b]; Where type can be any valid c++ data type Where arraySample can be a valid identifier Where [a] and [b] is the rows and columns Two-dimensional arrays are declared by specifying the number of rows and columns.
  • 10.
    A two-dimensional arraycan be think as a table which will have an X number of rows and Y number of columns. Example below is a two-dimensional array which contains three rows and four columns. myArray[rows][columns]; Column 0 Column 1 Column 2 Column 3 Row 0 myArray [0][0] myArray [0][1] myArray [0][2] myArray [0][3] Row 1 myArray [1][0] myArray [1][1] myArray [1][2] myArray [1][3] Row 2 myArray [2][0] myArray [2][1] myArray [2][2] myArray [2][3] Every element in array myArray is identified by an element name of the form myArray[rows][columns] Where myArray is the name of the array. Where [rows] and [columns] are the subscript that uniquely identified each element in myArray.
  • 11.
    Initializing two-dimensional arrays You can specify initial values by enclosing each row in curly braces { }. Example 1: char board[3][3] = {{‘x’, ‘x’, ‘o’}, {‘o’, ‘o’, ‘x’}, {‘x’, ‘o’, ‘ ‘}}; If some elements are omitted in the initialization list, they are set to zero. Example 2: int a[3][4]= {{0,1,2,3}, initialize for row indexed by 0 {4,5,6,7}, initialize for row indexed by 1 {8,9,10,11}}; initialize for row indexed by 2 The nested braces, which indicate the ntended row are optional. You can write as: int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 12.
    Subscripting two-dimensional arrayelements Note: Passing over all elements of a two-dimensional array is usually done with nested loops. Write subscript as x[row][col]. (Lab Manual page 100) Accessing two-dimensional array elements An element in two-dimensional array is accessed by using the subscript, ROW Index and COLUMN index of the array. EXAMPLE: int val = a[2][3] // this will take 4th element from the 3rd row of the array. To verify page 99.
  • 13.
    Example program (Problem)Page 101 #include <iostream> using namespace std; int main() { int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}}; for(int n=0;n<5;n++) for(int m=0;m<2;m++) { cout<<"a["<<n<<"]["<<m<<"]"; cout<<a[n][m]<<endl; } system("pause"); return 0; } OUTPUT |name| rows|columns|values| a[0][0]0 a[0][1]0 a[1][0]1 a[1][1]2 a[2][0]2 a[2][1]4 a[3][0]3 a[3][1]6 a[4][0]4 a[4][1]8 R|C {0,0} {1,2} {2,4} {3,6} {4,8}
  • 14.