0% found this document useful (0 votes)
11 views12 pages

Week 4 - Arrays

The document provides an overview of arrays in programming, including their definition, declaration, initialization, and how to access and manipulate elements. It covers one-dimensional, two-dimensional, and multi-dimensional arrays, along with examples and problems to solve using arrays. The content emphasizes the fixed size of arrays and the importance of proper initialization and element access.

Uploaded by

lujainhesham04
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)
11 views12 pages

Week 4 - Arrays

The document provides an overview of arrays in programming, including their definition, declaration, initialization, and how to access and manipulate elements. It covers one-dimensional, two-dimensional, and multi-dimensional arrays, along with examples and problems to solve using arrays. The content emphasizes the fixed size of arrays and the importance of proper initialization and element access.

Uploaded by

lujainhesham04
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/ 12

Arrays

Agenda
1. Introduction
2. Arrays
3. Accessing Elements
4. Array Initialization
5. Dealing With Array Elements
6. 2-Dimensional Arrays
7. Multi-dimensional Arrays
8. To Solve
Introduction
Problem:

● You are given 4 numbers, and you are asked to print them in reverse.

● Input:

1582

● Output:

2851

int a, b, c, d;

cin >> a >> b >> c >> d;

cout << d << ' ' << c << ' ' << b << ' ' << a;

What if you were given 1 million integers and you were asked to print them in
reverse? Should we declare 1 million variables manually?

Arrays
● An array is a data structure that allows the programmer to store multiple
values of the same data type under a single variable name. This means that
instead of creating individual variables for each value, the programmer can
create an array and store all the values in it. The array is a contiguous block
of memory locations, each of which can hold a value of the specified data
type.

● The size of the array should be mentioned while declaring it.

● How to declare an array:

dataType arrayName[arraySize];

● For example:
int x[100];

● Here, array x stores 100 values all of the same datatype int.

● When an array is declared, it is allocated a fixed amount of memory to store


a specific number of elements of a particular data type. Once the array is
declared, the size of the array cannot be changed. This means that the
programmer cannot add or remove elements from the array beyond the size
that was specified during the declaration. Therefore, it is important to
carefully consider the size of the array during its declaration to ensure that it
can accommodate the maximum number of elements that may be required
by the program. In summary, the size of an array is fixed once it is declared
and cannot be changed during the program's execution

Accessing Elements

● Each element in an array is identified by a unique number called an index.


The index represents the position of the element within the array and is used
to access or modify the value of that element. The index is an integer value
starting from 0 for the first element, and it increases sequentially to represent
subsequent elements in the array. Therefore, by using the index, the
programmer can identify and manipulate individual elements within the
array in a predictable and organized manner.

● The syntax to access array elements:

arrayName[arrayIndex];

● For an array named x:


● The array indices start from 0. Meaning x[0] is the first element of the array.

● If the size of the array is N, the last element of the array is stored at index N
- 1.

Array Initialization

● An array can be initialized during its declaration. This means that the
programmer can specify a set of predefined values for the array elements at
the time of its declaration. The initialization can be done by enclosing the set
of values within curly braces {} and separating them with commas. The
number of values specified in the initialization determines the size of the
array. Therefore, by initializing an array during declaration, the programmer
can create an array with predefined values and size, which can save time and
make the code more concise.

● For example:

int x[6] = {19, 10, 8, 17, 9, 15};

● Another method of initialization:

int x[] = {19, 10, 8, 17, 9, 15};

● Here, we have not mentioned the size of the array. The compiler
automatically computes the size.
● It is possible to initialize some elements of an array during its declaration
while leaving the remaining elements uninitialized. When this is done, the
programmer specifies the values for the initialized elements and leaves the
rest of the elements empty. In this case, the compiler automatically assigns a
default value of 0 to the uninitialized elements. This means that the
uninitialized elements will have a value of 0 until they are assigned a
different value by the program. By initializing only some elements of an
array, the programmer can create an array with a combination of predefined
and default values, providing greater flexibility and control over the data
stored in the array.

● For example:

int x[6] = {19, 10, 8};

● Another example:

int x[6] = {};

● Here, all 6 elements of the array are initialized with 0.


Dealing With Array Elements

● Once you have accessed an element of the array, you treat it as a normal
variable.

int x[5] = {19, 10, 8, 17, 9};

// Change 4th element to 1


x[3] = 1;

// Take a value from the user and store it at the 2nd element
cin >> x[1];

/*Assign the sum of the 2nd element


and the 4th element to the last element*/
x[4] = x[1] + x[3];

// Print the last element of the array


cout << x[4];

● How to print the whole array?

int x[5] = {19, 10, 8, 17, 9};

for (int i = 0; i < 5; i++)


cout << x[i] << endl;

● Here, we print each element of the array in order, starting from index 0 (1st
element) till we reach index 4 (last element).

● How to take the whole array as input from the user?

int x[5];
for (int i = 0; i < 5; i++)
cin >> x[i];
● Program to take an array of size N as input, and print it:

int main() {
int n;
cin >> n;

int x[n];
for (int i = 0; i < n; i++) {
cin >> x[i];
}

for (int i = 0; i < n; i++) {


cout << x[i];
}
}

Problem:

● You are given N numbers, and you are asked to print them in reverse.

● Input:

1582

● Output:

2851

Solution:

int main() {
int n;
cin >> n;

int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
cout << a[i] << " ";
}
}

Problem:

● You are given N numbers, and you are asked to print their average.

● Input:

1582

● Output:

Solution:

int main() {
int n;
cin >> n;
int a[n];

double sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}

cout << sum / n;


}
2-Dimensional Arrays
● What if we want to create an array of arrays? This is where 2D arrays come
in handy.

● Example:

int x[3][4];

● Here, x is a 2D array, holding 12 elements.

● We can think of this array as a table with 3 rows (arrays) and each row has 4
columns (elements) as shown.

● Initializing a 2D array:

int x[3][4] = {{3, 1, 4, 2}, {0, 3, 5, 9}, {8, 2, 4, 6}};

● Program to fill and print a 2D array of N rows and M columns:

int main() {
int n, m;
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j] << ' ';
}
cout << endl;
}
}

Problem:

● Given an array of N rows and N columns, print the elements on its main
diagonal.

● Input:

5635

1278

0233

6231

● Output:

5231

Solution:

int main() {
int n;
cin >> n;
int arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < n; i++) {
cout << arr[i][i] << " ";
}
}

Multi-Dimensional Arrays
● There are no inherent limits on the number of dimensions that an array can
have, other than the amount of available memory. This means that the
programmer can create arrays with any number of dimensions to suit the
needs of the program, as long as there is enough memory available to store
all the elements in the array. The dimensions of an array determine the
number of indices required to access an individual element within the array.
Therefore, by allowing arrays to have any number of dimensions, C++
provides a flexible and adaptable data structure that can be used to store and
manage complex sets of data in an organized and efficient manner.

● We can create a 3D array, as well as 4D and 5D.

● One can imagine a 3D array as a Rubik’s cube.

● Example:

int main() {
int n;
cin >> n;

int arr[n][n][n];

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
cin >> arr[i][j][k];
}
}
}
}

To Solve
● Print Sub-Array

● Maximum in Table

● Two Arrays And Swaps

You might also like