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

Lec2 - Arrays (Part2)

The document covers various aspects of arrays in programming, including definitions, declarations, initializations, and operations such as accessing, updating, and sorting elements. It also discusses multi-dimensional arrays and methods for passing arrays to functions. Key sorting techniques like Bubble Sort are explained, along with examples of initializing and accessing both two-dimensional and three-dimensional arrays.

Uploaded by

Denver Derapiza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec2 - Arrays (Part2)

The document covers various aspects of arrays in programming, including definitions, declarations, initializations, and operations such as accessing, updating, and sorting elements. It also discusses multi-dimensional arrays and methods for passing arrays to functions. Key sorting techniques like Bubble Sort are explained, along with examples of initializing and accessing both two-dimensional and three-dimensional arrays.

Uploaded by

Denver Derapiza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit I: ARRAYS

PART 2

Angelika S. Balagot
Instructor
Target Topics

• Topic 1.1: What are Arrays?


• Topic 1.2: Declaring Arrays
• Topic 1.3: Initializing Arrays
• Topic 1.4: Accessing Array Elements
• Topic 1.5: Array Operations
• Topic 1.6: Multi-Dimensional Arrays
• Topic 1.7: Passing Arrays to Functions
Topic 1.5: Array Operations
Array Operations

A. Accessing, reading, and storing array elements


B. Updating and totaling array elements
C. Sorting – sorting/arranging the data into a particular order such
as ascending or descending
Array Operations
A. Accessing, reading, and storing array elements
#include <stdio.h>

int main () {
Output:
int i;
int value[3];
int numbers[6] = {2,4,6,8,10,12}; 24681012
char word[] = {‘h’,‘e’,‘l’,‘l’,‘o’};
hello
for (i=0; i<6; i++) 555
printf(“%d", numbers[i]);
printf(“\n”);
for (i=0; i<5; i++)
printf(“%c", word[i]);
printf(“\n”);
for (i=0; i<3; i++) {
value[i] = 5;
printf(“%d", value[i]);
}
}
Array Operations
B. Updating and totaling array elements
#include <stdio.h>
Output:
int main () {

int num[3] = {5,10,15};


int i, sum1=0, sum2=0, double_num[3];

for (i=0; i<3; i++){ 5 10 15 The sum is 30


double_num[i] = num[i]*2; 10 20 30 The sum is 60
sum1 = sum1+num[i];
sum2 = sum2+double_num[i];
}
for (i=0; i<3; i++)
printf(“%d ", num[i]);
printf(“The sum is %d\n”, sum1);
for (i=0; i<3; i++)
printf(“%d ", double_num[i]);
printf(“The sum is %d\n”, sum2);
}
Array Operations
C. Sorting

• Sorting is a process of ordering individual elements of a list


according to their proper rank, either in ascending or descending
order.

1, A 2, B 3, C 4, D 5, E …

26, Z 25, Y 24, X 23, W 22, V …


Array Operations
C. Sorting

• Sorting can be done on names, numbers, records, etc. Sorting


reduces the effort required on finding something or for organizing a
certain thing.

• For example, it is relatively easy to look up the phone number of a


friend from a telephone dictionary because the names in the phone
book have been sorted into alphabetical order.
Array Operations
C. Sorting

• In C, there are several techniques/algorithms to achieve the sorting


process:

Insertion sort Merge Sort Quick Sort Radix Sort

Heap Sort Selection sort Bubble sort


Array Operations
C. Sorting – Bubble Sort

• One such well-known technique that we will discuss is called Bubble


Sort. The algorithm gets its name from the way smaller elements
“bubble” to the top of the list.

• Bubble Sort in C is a sorting algorithm where we repeatedly iterate


through the array and swap adjacent elements that are unordered. We
repeat this until the array is sorted.
Array Operations
C. Sorting – Bubble Sort

• Each array element is compared with every other array element in


an organized sequence. When another value is larger (or smaller)
than another, the values are swapped. Otherwise, the
comparison continues.
Array Operations
C. Sorting – Bubble Sort

• As an example, for an array num – [5, 1, 4, 2, 3] …


• …we can see that 5 should not be on the left of 1 and so, we swap
them to get: [1, 5, 4, 2, 3].

• Next, we see that 5 should again not be on the left of 4. We swap 5


and 4 to get [1, 4, 5, 2, 3]. We repeat this for 5 and 2 and
subsequently for 5 and 3 to get [1, 4, 2, 3, 5].
Array Operations
C. Sorting – Bubble Sort

• As can be seen – after one “pass” over the array, the largest element
(5 in this case) has reached its correct position – extreme right. Let us
try to repeat this process, focusing on 4:
• Currently - [1, 4, 2, 3, 5]:
• (1, 4) is correct. However, (4, 2) is an incorrect order. Therefore, we
swap 4 and 2 to get [1, 2, 4, 3, 5]. Now again, (4, 3) is incorrect so we
do another swap and get [1, 2, 3, 4, 5].
Array Operations
C. Sorting – Bubble Sort

• This exactly is how bubble sort in C


works.

• As an example, check this graphic that


pictorially depicts how bubble sort
works.
Array Operations
C. Sorting – Bubble Sort in C

• The sort itself involves nested for


loops: an outer loop and an inner loop.
The outer loop marches through the
entire array, one step at a time. The
inner loop takes it’s position one
element higher in the array and
swoops through each value
individually.
Topic 1.6: Multi-Dimensional Arrays
Multi-Dimensional Arrays

• C allows for arrays of two or more dimensions. An array can


have two, three or even ten or more dimensions.

• More dimensions in an array means more data to be held, but


also means greater difficulty in managing and understanding
arrays.
Multi-Dimensional Arrays

• In C, we can create arrays of an array known as multidimensional


arrays. This type of an array can be; two-dimensional (2D) or
three-dimensional (3D) arrays.

• Example:
int age[3][5];
double salary[2][3][5];
Two-Dimensional Array

• The simplest form of multidimensional array is the 2D or two-


dimensional array. To simplify, this is a list of one-dimensional
arrays. To declare a two-dimensional integer array with a name of
papersize with a size [x][y]:

• int papersize[x][y];
Two-Dimensional Array

• A two-dimensional array can be considered as a table which will


have an x number of rows and y number of columns. For example,
we want the papersize array to contain 3 rows and 5 columns:

• int papersize[3][5];
Two-Dimensional Array

• Every element in the array arr, for example, is identified by an


element name of the form arr[x][y] where is the arr is the name of
the array and x and y are the subscripts that uniquely identify
each element in arr. See next slide for the pictorial representation:
Two-Dimensional Array

Column 0 Column 1 Column 2 Column 3 Column 4

Row 0 arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[0][4]


Row 1 arr[1][0] arr[1][1] arr[1][2] arr[1][3] arr[1][4]
Row 2 arr[2][0] arr[2][1] arr[2][2] arr[2][3] arr[2][4]

• So, to access the value of the 1st Row and 2nd Column, you need
to write:
• data type arr[0][1];
Initializing 2D Arrays

• Multidimensional arrays may be initialized by specifying bracketed


values for each row.
• For example, we have an array with 2 rows and each row has 3
columns:

int a[2][3] = {
{0, 1, 2}, /*Row 1*/
{3, 4, 5} /*Row 2*/
};
Initializing 2D Arrays

• Take note that the nested braces, which indicate the intended
row, is optional.
• The following is equivalent to the previous example:

int a[2][3] = {0, 1, 2, 3, 4, 5};

• So to access the value 4, you can declare the ff:


• int a[1][1];
Initializing 2D Arrays

• We can also initialize two-dimensional arrays these way:

int a[2][3] = {{0, 1, 2}, {3, 4, 5}};

int a[][3] = {{0, 1, 2}, {3, 4, 5}};

int a[2][3] = {0, 1, 2, 3, 4, 5};


Three-Dimensional Array

• We can also create arrays in three dimensions or three-dimensional


array (3D). To simplify, it is a collection or group of 2D arrays. To
declare a three-dimensional integer array with a name of scores
with a size [x][y][z]:

• int scores[x][y][z];
Three-Dimensional Array

• A three-dimensional array can be considered as a collection of


tables which contain 2D arrays; x is the table number, y is the row
number, and z is the column number. For example, we want the
scores array to have 2 tables that contain a 4 rows and 3 columns:

• int scores[x][y][z];
• int scores[2][4][3];
Three-Dimensional Array

Column 0 Column 1 Column 2


Column 0 Column 1 Column 2
Row 0 arr[0][0] arr[0][1] arr[0][2]
Row 0 scores[0][0] scores[0][1] scores[0][2] • So, to access the
Row 1 arr[1][0] arr[1][1] arr[1][2]
Row 1 scores[1][0] scores[1][1] scores[1][2] value of 2nd table, 3rd
Row 2 arr[2][0]
Row 2
arr[2][1]
scores[2][0]
arr[2][2]
scores[2][1] scores[2][2]
Row, 1st Column, you
Row 3 arr[3][0] arr[3][1] arr[3][2] need to write:
Row 3 scores[3][0] scores[3][1] scores[3][2]
• int scores[1][2][0];

0th 2D array 1st 2D array


Initializing 3D Arrays

int a[2][2][3] =
{
{ • Three-dimensional
/*Table 1*/
{0, 1, 2}, /*Row 1*/
arrays may be initialized
{3, 4, 5} /*Row 2*/ by specifying bracketed
}, values for each row. For
{ example, we have an
/*Table 2*/
{6, 7, 8}, /*Row 1*/ array with 2 tables, 2
{9, 10, 11} /*Row 2*/ rows and each row has
}, 3 columns.
};
Initializing 3D Arrays

• Take note that the nested braces, which indicate the intended row,
is optional. The following is equivalent to the previous example:

• So to access the
int a[2][2][3] = {
{ {0, 1, 2},{3, 4, 5} } },
value 3, you can
{ {6, 7, 8},{9, 10, 11} } use the ff:
}; • int a[0][1][0];
Topic 1.7: Passing Arrays to
Functions
Passing Arrays to Functions

• If you want to pass a single-dimension array as an


argument in a function, you would have to declare a formal
parameter in three possible ways:

• As a pointer…
• As a sized array…
• As an unsized array…
As a POINTER

• We can declare a formal parameter as a pointer like so…

#include <stdio.h>

void myFunction (int *param) {


.
.
.
}
As a SIZED ARRAY

• We can declare a formal parameter as a sized array like so…

#include <stdio.h>

void myFunction (int myarray[10]) {


.
.
.
}
As a UNSIZED ARRAY

• We can declare a formal parameter as an unsized array like so…

#include <stdio.h>

void myFunction (int myarray[]) {


.
.
.
}
Target Topics

• Topic 1.1: What are Arrays?


• Topic 1.2: Declaring Arrays
• Topic 1.3: Initializing Arrays
• Topic 1.4: Accessing Array Elements
• Topic 1.5: Array Operations
• Topic 1.6: Multi-Dimensional Arrays
• Topic 1.7: Passing Arrays to Functions
End of Discussion

Angelika S. Balagot
Instructor

You might also like