0% found this document useful (0 votes)
45 views20 pages

Chapter 2 - Array

The document provides an introduction to arrays in C++. It defines arrays as a series of elements of the same type in contiguous memory locations. Arrays have properties such as being zero-indexed and holding only one data type. The document discusses declaring, initializing, and accessing one-dimensional and two-dimensional arrays. It provides examples of declaring arrays, initializing arrays with individual or list values, and using loops and indexes to access elements. Homework problems are included to practice arrays.

Uploaded by

Adisu Biyana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
45 views20 pages

Chapter 2 - Array

The document provides an introduction to arrays in C++. It defines arrays as a series of elements of the same type in contiguous memory locations. Arrays have properties such as being zero-indexed and holding only one data type. The document discusses declaring, initializing, and accessing one-dimensional and two-dimensional arrays. It provides examples of declaring arrays, initializing arrays with individual or list values, and using loops and indexes to access elements. Homework problems are included to practice arrays.

Uploaded by

Adisu Biyana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Chapter Two

Introduction to Array in C++


At the end of this session you will be able to understand:
• The definitions and uses of array
• Declarations of 1D & 2D array
• Initializations of 1D & 2D array
• Accessing elements of an array using one dimensional
and two-dimensional arrays
• How to write a programs using array in C++
What is an Array?
• An array is a series of elements of the same type
placed in a contiguous memory locations
• An individual element of an array is identified by its
own unique index (or subscript)
• Instead of declaring individual variables to record all
the student’s mark, such as int mark0, int mark1, ...,
up to int mark7, you can declare one array variable for
all given data that are in the same type like:-
float mark[7];
• This declaration will cause the compiler to allocate
space for 7 consecutive float variables in memory
Properties of Arrays
• Arrays in C++ are zero-bounded; that is the index of
the first element in the array is 0 and the last element is
N-1, where N is the size of the array.
• Array can only hold values of one type.
• All arrays consist of contiguous memory locations.
The lowest address corresponds to the first element
and the highest address to the last element.
• An individual element of an array is identified by its
own unique index (or subscript).
• The index must be an integer and indicates the
position of the element in the array. Thus the elements
of an array are ordered by the index.
Cont..
Name of array (Note
• Array that all elements of
– Group of consecutive (sequential) this array have the
memory locations same name, c
– Same name and type

• To refer to an element, specify c[0] -45


c[1] 6
– Array name c[2] 0
– Position number c[3] 72
c[4] 1543
• Format: c[5] -89
Array_name[ position number ] c[6] 0
c[7] 62
– First element at position 0 c[8] -3
– n element array named c: c[9] 1
c[10] 6453
• c[ 0 ], c[ 1 ].......c[ n – 1 ] c[11] 78
• Int c[12]={-45, 6, 0, 72,1543, -89
0, 62, -3, 1, 6453, 78};
Position number of the
5
element within array c
Declaring Arrays
• Declaring the name and type of an array and setting the
number of elements in an array is called dimensioning the
array.
• The array must be declared before one uses it like other
variables.
• In the array declaration one must define:
1. The type of the array (i.e. integer, floating-point, char, etc.)
2. Name of the array,
3. The total number of memory locations to be allocated or the
maximum value of each subscript.

• So the general syntax for the declaration is:


DataTypename arrayname [array size];

6
Initializing Array

• You can initialize C++ array elements either one by one


or using a single statement as follows:
• int num[5] = {20,55,43,34,68}; the initial values are given
as a list enclosed in curly brackets.
• Indexed representation will be as:
– num[0]=20;
– num[1]=55;
– num[2]=43;
– num[3]=34;
– num[4]=68;
Cont..
Example: int day[5];
• Every elements of array day will be set initially to 0:

• But additionally, when we declare an Array, we have the


possibility to assign initial values to each one of its
elements using curly brackets { } .
• For example:
Int day[5] = {16, 2, 77, 40, 12071};

• The above declaration would have created an array like


the following one:
Cont..
• Note: The number of values between braces { } cannot be
larger than the number of elements that we declare for the
array between square brackets [ ].
• If you omit the size of the array, an array will be big enough to
hold the elements that are inserted into it.
• You will create exactly the same array as you did before if you
specify an array as:
int num[] = {20,55,43,34,68};
• Note: when initializing an array, we can provide fewer values
than the array elements.
• E.g. int num[5] = {20, 55}; in this case the compiler sets the
remaining elements to “zero”.
Accessing Array Elements
• The accessing function for one dimensional array is
array-name [index]; e.g. myarray [3];

#include<iostream>
using namespace std;
int main()
{
int myarray[3]={2,5,6};
cout<< myarray[index];
//index will be the required output
return 0;
}
Cont..
• To display the entire elements of array we need to loop
through the sequence.
#include<iostream.h>
int main()
{
int mark[3]={2,5,7};

for(int index=0;index<3;index++)
{
cout<<mark[index];
}
return 0;
}
Two Dimensional (2D) Array
• In a first (left) subscript as being the row, and two-
dimensional array, it is convenient to think of the second
(right) subscript as being the column.

• To access the elements of a two-dimensional array,


simply use two subscripts:

• array[row][col]
Initializing a 2D Array
1. use nested braces, with each set of numbers representing a
row:
int array[3][5] ={{ 1, 2, 3, 4, 5 }, // row 0
{6, 7, 8, 9, 10 }, // row 1
{11, 12, 13, 14, 15 } // row 2
};

2. We can also initializes this array as:


int array[3][5]={1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15};
Omitting the Size of two 2D Array
• Two-dimensional arrays with initializer lists can omit (only) the
leftmost length specification:
int array[][5] ={{ 1, 2, 3, 4, 5 },{ 6, 7, 8, 9, 10},
{ 11, 12, 13,14, 15 }};

• The compiler can do the math to figure out what the array length is.
• However, the following is not allowed:
int array[ ][ ] = { 1, 2, 3, 4, 5, 6, 7, 8 };

• Because the inner parenthesis are ignored, the compiler can not tell
whether you intend to declare a 1×8, 2×4, 4×2, or 8×1 array in this
case.

• If a one-dimensional array is initialized, the size can be omitted as it


can be found from the number of initializing elements:
int x[] = { 1, 2, 3, 4}; This initialization creates an array of
four elements
Accessing Elements in a 2D Array
• Accessing all of the elements of a two-dimensional array requires two
loops: one for the row, and one for the column.

• Since two-dimensional arrays are typically accessed row by row, the


row index is typically used as the outer loop.

for (int row=0;row<index1;row++) //loop for rows


{
for( into col=0; col<index2,col++) //columns
{
cout<< array[row][col]<<“ “;
}
cout<<endl;// to make space between matrixes
}
Example 2D Array
#include<iostream.h>
int main(){
int mark[2][6]= {{34,12,56,78,11,89},
{23,45,78,90,56,97}
};
for(int row=0;row<2;row++)// outer loop for rows
{
for(int col=0;col<6; col++)//inner loop for cols
{
cout<<mark[row][col]<<" ";
}
cout<<endl;
}
return 0;
}
Home work (4%)
1. Write a C++ program to calculates and prints a multiplication
table for all values between 1 and 9 (inclusive). Note that when
printing the table, the for loops start from 1 instead of 0. This is
to omit printing the 0 column and 0 row.

The output of the program should be as follows:-


1 2 3 4 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
Quiz(6%)
1. Write a C++ program that can store 5 numbers
entered by user and finding the sum of numbers
entered using one dimensional array?

2. Write a C++ Program to store 5 numbers entered by


user in an array and display first and last number
only.
Question 1.
#include <iostream.h>
int main(){
int numbers[5];
int sum = 0;
cout << "Enter 5 numbers: ";
/* Storing 5 number entered by user in an array
Finding the sum of numbers entered */
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum = sum + numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Question 2
#include <iostream.h>
int main() {
int n[5];
cout<<"Enter 5 numbers: ";
/* Storing 5 number entered by user in an
array using for loop. */
for (int i = 0; i < 5; ++i) {
cin>>n[i];
}
cout<<"First number: "<<n[0]<<endl;
cout<<"Last number: "<<n[4
return 0;
}

You might also like