Chapter 3: Array and String:- Array
Computer Science
Informatics college
University of Gondar
Getaneh T.
February 14, 2025
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 1 / 22
Learning Objectives
At the end of this lesson students able to answer
What is an Array?
Types of Arrays
1. One-Dimensional Array
How to Declare
Initialize
How to Access
2. Two-Dimensional Array
How to Declare
Initialize
How to Access
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 2 / 22
why it need array?
Consider the following problem. Assume you need a program that a
ccepts 15 floating point numbers from the user calculates their
average print out the numbers that are above the average.
What would you do?
Declare 15 variables
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 3 / 22
What is an Array?
To solve the above problem it need to use array.
Array is a sequence of variables of the same data type, sharing the
same name.
The variables in the sequence are referenced using an index.
Useful for storing a large number of values of the same type.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 4 / 22
Characteristics of Arrays
Homogeneous: All elements are of the same data type.
Consecutive memory locations for all elements.
Individual variables in an array are called elements.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 5 / 22
Types of Arrays
Single-dimensional arrays: Sequence data in a row or column.
Multi-dimensional arrays: Sequence data in table form (e.g., 2D
arrays).
This course focuses on single-dimensional and 2D arrays.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 6 / 22
Declaring 1D Arrays
Syntax: DataType arrayName[arraySize];
Example:
int balance[10];
float numbers[80];
Array size must be a constant integer greater than zero.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 7 / 22
cont...
Once an array is declared, its size (number of elements) will remain
the same.
It is better to declare a constant SIZE variable and use it for the size
of the array, as shown in the example below:
const int size = 80;
float num[size];
It is very common to use a named constant to set the size of an array
because:
It can be used to control loops throughout the program.
It will be easy to change if the size of the array needs to be modified.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 8 / 22
Initializing 1D Arrays
Arrays can be initialized during declaration.
Example:
int odd[] = {1, 3, 5, 7, 9}; (size is 5)
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
Size is optional during initialization but mandatory during declaration.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 9 / 22
Initializing 1D Arrays cont..
you can assign value to individual elements of array.
int odd[2] = 3;
You can insert (read) value from the keyboard for
individual element of an array.
A value can be read into an array element directly using cin.
syntax:- cin>>mar[4]; .
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 10 / 22
Initializing 1D Arrays cont..
You can insert all the array data.
Reading from the keyboard is performed using loops.
Example: to read 10 elements and store it in an array whose name is
odd.
for( int i=0;i<=10;i++)
cin>>odd[i]; .
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 11 / 22
Accessing Array Elements
The 1st 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 of (n-1)
i.e. 0 → n-1 index number.
An array element is accessed by writing the identifier of the array
followed by the subscript/ index in square brackets ([]).
This is done by placing the index of the element within square
brackets after the name of the array.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 12 / 22
Accessing Array Elements cont...
You access an individual element of a one-dimensional array.
Syntax: name[index];
Example:
double salary = balance[4];
You can access all elements of an array( 1D) using looping statements.
Example: to display all elements of an array whose name is odd and
which has 8 elements are:-
for (int i = 0; i < n; i++)
{ cout << odd[i]; }
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 13 / 22
cont...
what will be the output of the following program
#include <iostream>
using namespace std;
int main() {
int x[3]={3,4,5};
cout<<x[2]<<endl;
return 0;
}
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 14 / 22
Exercises
Write a C++ program that stores or initializes the marks of five
students and displays them?
Write a C++ program that reads 10 integer values and displays them
using an array.
Write a C++ program that reads 10 integer values and display
elements that are greater than 55.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 15 / 22
2D Arrays
2D array has multiple row and column.
You can declare a 2D array in C++.
Syntax: DataType arrayName[rows index][columns index];
Example:
int matrix[3][2];
float table[5][4];
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 16 / 22
cont...
The total number of elements in 2D array is calculated by
row index * colmun index
Example:-
int matrix[3][2];
The total element of this array is 3*2=6.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 17 / 22
Initialize 2D
You can assign in 2D array
During declaration by using (=) but It must give the size of array.
Example
int the Array [5][3] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
By using cin>> statement
it need nested looping statement.
the outer loop iterate based on number of row and the inner loop
iterate based on number of columns in array.
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 18 / 22
cot...
Example:-
int x[5][2];
for ( int i=0; i<5; i++){
for (int j = 0; j<2;j++)
{
cin>>x[i][j];
}}
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 19 / 22
Accessing 2D arrays
Individual elements of a 2D array can be accessed in C++.
Syntax:- name[row index][colmun index].
Example:- cout<<x[2][1]
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 20 / 22
Accessing 2D Arrays cont...
When we needs to access all element at time it needs to a nested
looping statement.
The first loop iterate on row and the second or inter loop iterate
based on the column.
Syntax:
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
cout << matrix[i][j];
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 21 / 22
Exercises
1 Write a program to summing up data in each row of a 2d array the
data it may read from the keyboard?
2 Write a program to summing up the all data in 2D array the data it
may read from the keyboard?
Next:- String.....
Getaneh T. Chapter 3: Array and String:- Array February 14, 2025 22 / 22