0% found this document useful (0 votes)
40 views27 pages

Chapter One

#include <iostream> using namespace std; int main() { int rows, cols; cout << "Enter number of rows: "; cin >> rows; cout << "Enter number of columns: "; cin >> cols; int arr[rows][cols]; cout << "Enter elements of 2D array: " << endl; for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { cin >> arr[i][j]; } } return 0; }

Uploaded by

Abel Ateme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views27 pages

Chapter One

#include <iostream> using namespace std; int main() { int rows, cols; cout << "Enter number of rows: "; cin >> rows; cout << "Enter number of columns: "; cin >> cols; int arr[rows][cols]; cout << "Enter elements of 2D array: " << endl; for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { cin >> arr[i][j]; } } return 0; }

Uploaded by

Abel Ateme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Fundamentals of Programming II (ITec2041)

Chapter One: Array and String

Abey B. (M.Sc.)

Debre Berhan University


Faculty of Computing
Department of Information Technology

December 2, 2022

DBU Chapter One: Array and String December 2, 2022 1 / 53


4.2. Arrays

What is array
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
Array is a data structure which allows a collective name to be given
to a group of elements which all have the same type.
In this type of array, it stores elements in a single dimension.
In this array, a single specification is required to describe elements of
the array.
An individual element of an array is identified by its own unique
index (or subscript).

DBU Chapter One: Array and String December 2, 2022 2 / 53


4.2. Arrays

What is array
An array can be thought of as a collection of numbered boxes each
containing one data item.
The number associated with the box is the index of the item.
To access a particular item the index of the box associated with the
item is used to access the appropriate box.
The index must be an integer and indicates the position of the
element in the array.

DBU Chapter One: Array and String December 2, 2022 3 / 53


4.2. Arrays

To declare an array in C++, you need to define:


I The data type of elements in the array
I The name of the array
I The size of the array
Syntax:

DBU Chapter One: Array and String December 2, 2022 4 / 53


4.2. Arrays

Example: int club[10];


I int is the data type of the array elements
I club is the name of the array
I ten is the size of the array, which means ten blocks of memory space
are created for the array club.The number of elements must be an
integer.
It is best to make the array size a constant and then, if required,
the program can be changed to handle a different size of array by
changing the value of the constant,

DBU Chapter One: Array and String December 2, 2022 5 / 53


4.2. Arrays

To initialize an array in C++, there are two ways.


I 1. Initializing the array during the declaration:
F You can initialize an array while declaring it, by assigning elements to
the array during the declaration using curly braces around the elements
separated by commas.

DBU Chapter One: Array and String December 2, 2022 6 / 53


4.2. Arrays

To initialize an array in C++, there are two ways.


I 2. Assigning the Value After the Declaration
F After the declaration of the array, you can assign elements to the array
at each index.

DBU Chapter One: Array and String December 2, 2022 7 / 53


4.2. Arrays

Types of Arrays in C++


1. One-Dimensional Array:
I In this type of array, it stores elements in a single dimension.
I In this array, a single specification is required to describe elements of
the array.

DBU Chapter One: Array and String December 2, 2022 8 / 53


4.2. Arrays

1. One-Dimensional Array:
4.3.2. Accessing Array Elements

Given the declaration above of a 100-element array the compiler


reserves space for 100 consecutive floating point values
I Accesses these values using an index/subscript that takes values from 0
to 99.
The first element in an array in C++ always has the index 0, and if
the array has n elements the last element will have the index n-1.

DBU Chapter One: Array and String December 2, 2022 9 / 53


4.2. Arrays

1. One-Dimensional Array:
4.3.2. Accessing Array Elements
The values of any of the elements in an array can be accessed just
like the value of a regular variable of the same type.
The syntax is:

DBU Chapter One: Array and String December 2, 2022 10 / 53


4.2. Arrays

If declared with less, the remaining elements are set to their default
values (which for fundamental types, means they are filled with
zeroes).
for example:

DBU Chapter One: Array and String December 2, 2022 11 / 53


4.2. Arrays

The initializer can even have no values, just the braces:


for example:

This creates an array of five int values, each initialized with a value of
zero:

DBU Chapter One: Array and String December 2, 2022 12 / 53


4.2. Arrays

In C++ allows the possibility of leaving the square brackets empty [].
I In this case, the compiler will assume automatically a size for the array
that matches the number of values included between the braces :

DBU Chapter One: Array and String December 2, 2022 13 / 53


4.2. Arrays

Example 1

DBU Chapter One: Array and String December 2, 2022 14 / 53


4.2. Arrays

Example 2

DBU Chapter One: Array and String December 2, 2022 15 / 53


4.2. Arrays

Example 3

DBU Chapter One: Array and String December 2, 2022 16 / 53


4.2. Arrays

Example 4

DBU Chapter One: Array and String December 2, 2022 17 / 53


4.2. Arrays

Example 5

DBU Chapter One: Array and String December 2, 2022 18 / 53


4.2. Arrays

Example 5

DBU Chapter One: Array and String December 2, 2022 19 / 53


4.2. Arrays

Example 5

DBU Chapter One: Array and String December 2, 2022 20 / 53


4.4. Multidimensional arrays

An array may have more than one dimension.


Each dimension is represented as a subscript in the array.
Therefore a two dimensional array has two subscripts, three
dimensionaa l array has three subscripts, and so on.
A multi-dimensional array is an array of arrays.
Multi-Dimensional Arrays in C++ arrays are used to store the data
in the form of a table of rows and columns.

DBU Chapter One: Array and String December 2, 2022 21 / 53


4.4. Multidimensional arrays

The simplest form of the multidimensional array is the


two-dimensional array.
To declare a two-dimensional integer array of size x,y, you would write
something as follows:
datatype arrayName [ x ][ y ];
A 2-dimensional array a, which contains three rows and four columns
can be shown as below :

DBU Chapter One: Array and String December 2, 2022 22 / 53


4.4 Multidimensional arrays

4.4.1.Initializing Multidimensional Arrays


There are two ways in which a Two-Dimensional array can be
initialized.
First Method:

Seocnd and Better Method:

DBU Chapter One: Array and String December 2, 2022 23 / 53


4.4 Multidimensional arrays

Multidimensional Array
Example 1:

DBU Chapter One: Array and String December 2, 2022 24 / 53


4.4 Multidimensional arrays

Multidimensional Array
Example 2:

DBU Chapter One: Array and String December 2, 2022 25 / 53


4.4 Multidimensional arrays

Multidimensional Array
Example 3:

DBU Chapter One: Array and String December 2, 2022 26 / 53


4.4 Multidimensional arrays

Multidimensional Array
Example 4: Write the c++ program taking Input from user for Two
Dimensional Array?

DBU Chapter One: Array and String December 2, 2022 27 / 53

You might also like