0% found this document useful (0 votes)
13 views

Week 3 Arrays

Uploaded by

ramezbasel
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)
13 views

Week 3 Arrays

Uploaded by

ramezbasel
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/ 20

ICPC O6U Community

Week 3

Arrays
Arrays
● If we want to store data we use variables. But if we want
to store a lot of data, we will need to create a lot
variables to store it and this is not always effective.

● For example, suppose we have 1,000 students and we


want to store each student's degree. Now, if we create a
variable for each student, we’ll need 1,000 variables,
and that's hard to do, we can use a container which is
called an array.
Arrays
● What is an array?
Array is a collection of similar data stored at contiguous
memory locations and elements can be accessed randomly
using indices of an array.
Arrays
● An array is a container used to store multiple data in a
single variable, instead of declaring separate variables
for each data.

● Declaring Arrays:

DataType ArrayName[Number of elements] ;


ex: int x[5];
Here we create an array called x with size 5 of type int.
Arrays
● Initializing Arrays:
1. int x[5] = {1, 2, 3, 4, 5};
2. int x[] = {1, 2, 3, 4, 5}; // size is number of elements

● When we declare an array, it contains garbage values until we


initialize it.
int x[5] = {3};
Here, the first element in the array will be set to 3 and the rest
will be 0 by default.
int x[5] = {};
Here, all elements in the array will be set to 0.
Arrays

Arrays
● Accessing the values of an array:
We can access elements in an array by calling its name then
[Index] while the index refers to the location you want to access.
Array_Name[Index];
Arrays
● Accessing the elements of an array using loops:
Problems
1. Write a program to take an array of size N from user and number X
then check if X exists in the array or not.

2. Write a program to take an array of size N from user then if the


number is even change its value to 0 otherwise change it to 1 then
print the array.

3. Write a program to take an array of characters of size N from user,


reverse it and print it.

4. Write a program to take an array of characters of size N from user


then convert every character that is uppercase to lowercase and
vice versa.
Multidimensional Arrays
● Now If we want to represent a chess board we would
have to create 8 arrays of size 8.

● But if we want to make a 250 * 250 board, it would be


difficult to be done with a 1D array, so it will be easy if
we create a 2 dimensional array (Matrix).
Multidimensional Arrays
● Multidimensional Array (Matrix) 2D:
Matrix is an array that consists of more than one row and
column. In a 2-D array each element is referred by two
indices.
Multidimensional Arrays
● Declaring a 2D array.

Data_Type Array_Name[number of rows][number of columns];

int x[5][6];

Here, the compiler has reserved a place in the memory to store


a matrix of 5 rows and 6 columns of type int called x.
Multidimensional Arrays
● Initializing 2D Arrays:
1. int matrix[2][3] = {2, 4, 5, 9, 0, 19};
2. int matrix[2][3] = {{2, 4, 5}, {9, 0, 19}};
Multidimensional Arrays
● Accessing the elements of a matrix:

Array_Name[Row_Index ][Column_Index ]
Multidimensional Arrays
◆ Accessing the elements of a matrix using loops:
- Here, we have used a
nested for loop to access
the matrix elements.

1 - The outer loop accesses the


first three rows of the array.

2- The inner loop accesses the


first 2 columns of the array.
Problems
1. Write a program to take a 2D array from user and print the largest and
smallest number in each row.

2. Write a program that takes a 2D array of integers of size (N x N) from user and
print the summation of even numbers in each row and the summation of odd
numbers in each row

3. Write a program to print this 2D array of char of size N * N


if N = 7, so the 2D array is: *******
* *
* *
* *
* *
* *
*******
Notes
● What is a subarray?
- A subarray is a contiguous part
of an array (consecutive positions).

ex: The sub-arrays of the array {1,2,3}


are {1}, {1,2}, {1,2,3}, {2}, {2,3} and {3}.

● For any array of size n, there are


n * (n + 1) / 2 non-empty subarrays.
Notes
● What is a subsequence?
- A subsequence is a sequence that can be derived from
another sequence by deleting zero or more elements without
changing the order of the remaining elements.

● ex: subsequences of {1,2,3,4} are {1}, {2}, {3}, {4}, {1,2}, {1,3}, {1,4},
{2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}, {1,2,3,4}.

● More generally, we can say that for a sequence of size n, we have


(2 ^ n - 1) non-empty sub-sequences in total.
Notes

Arrays

● Try to solve all the problems. If you are stuck in a


problem, ask for a hint on the Facebook group

Facebook Group

Sheets

You might also like