0% found this document useful (0 votes)
31 views38 pages

Lecture 3

Uploaded by

amaniel
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)
31 views38 pages

Lecture 3

Uploaded by

amaniel
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/ 38

Lecture 3 - Arrays

Introduction to Programming in C
Ben-Gurion University of the Negev
Based on slides by Dr. Wan-Lei Zhao
• Opening Discussion

• We have the following problem:


• We have 10 students in the class, and we want the
average/ sum/ max/ min score of their tests. We also
would like to ranks them.
• Based on what we’ve learned, should we keep 10
variables of the same type?
• What happens if we have 100 students?
• Opening Discussion

• Sorting is even harder!


• This is where the array comes
• 1D Arrays - Declaration

• 1D array is defined in the following form:

• Type could be any type defined in C. E.g., int, float


• “arrayName” should be unique
• An array is a variable/constant, so the same rules apply to it
as well
• “size” should be an integer or integer constant greater than 0
• 1D Arrays - Declaration
• 1D Arrays – Element Visitation

• Element in an array is visited by the subscript operator


• Subscript goes from 0 to N-1
• For example, to visit the 3rd element of x[N], we write “x[2]”
• 1D Arrays – Element Visitation

• In the example below, you are not allowed to use subscript


beyond 39
• You will invade other variables territory
• 1D Arrays – How Does It Look Like?

• The system opens a continuous memory block for an array


• Actual size depends on both the type and length of the array
• 1D Arrays – How Does It Look Like?

• The system opens a continuous memory block for an array


• Actual size depends on both the type and length of the array
• 1D Arrays – How Does It Look Like?

• The system opens a continuous memory block for an array


• Actual size depends on both the type and length of the array
• 1D Arrays – Initialization

• Array with no initialization, what happens?

Initializes to Initializes to zeros Initializes to zeros


random numbers
• Don’t rely on default initialization
• 1D Arrays – Initialization

• Initializers as follows are valid


• 1D Arrays – Initialization

• Initializers as follows are invalid


• 1D Arrays – Example

• Given an array: a[10] = {3, 21, 5, 8, 5, 11, 22, 14, 9, 51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}
• 1D Arrays – Example

• Given an array: a[10] = {3, 21, 5, 8, 5, 11, 22, 14, 9, 51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}
• The idea is that we only need to swap two elements each
time
• One for the start, and one for the end
10
• We do this times
2

15 minutes to solve
• 1D Arrays – Example

• Given an array: a[10] = {3, 21, 5, 8, 5, 11, 22, 14, 9, 51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}
𝑁
• For i from 0 to
2
• Exchange a[i] with a[N-i-1]
• End-for
• 1D Arrays – Example
𝑁
• For i from 0 to
2
• Exchange a[i] with a[N-i-1]
• End-for
• 1D Arrays – Example 2

• An integer number given by the user


• Int a[n]
• Input “n” numbers and assign them to “a[n]”
• Print out array “a”

15 minutes to solve
• 1D Arrays – Example 2
• 1D Arrays – Example 3

• Given a sorted array: a[7] = {3, 14, 15, 18, 22, 35}
• Insert an input number to the array while keeping the array
sorted.
• You can pop the first element to clear space.

15 minutes to solve
• 1D Arrays – Example 3
• 1D Arrays – Example 4

• Given an array: a[10] = {21, 3, 5, 8, 5, 11, 22, 14, 51, 9}


• Sort the array in ascending order: {3, 5, 5, 8, 9, 11, 14, 21, 22,
51}

5 minutes to think of a solution


• 1D Arrays – Example 4

• Given an array: a[10] = {21, 3, 5, 8, 5, 11, 22, 14, 51, 9}


• Sort the array in ascending order: {3, 5, 5, 8, 9, 11, 14, 21, 22,
51}
• The idea is bubble sort, which is a classic method for sorting
• Each time, we move the largest element to the rear of the
array
• Repeat this on the sub-array left N times.
• 1D Arrays – Example 4

• An example of one sorting iteration


• 1D Arrays – Example 4

• Let’s outline the procedure


• For I from 0 to N do
• For j from 0 to N-I do
• Check a[j] and a[j+1]
• If a[j] > a[j+1]: swap them
• End-for(j)
• End-for(i)
• 1D Arrays – Example 4
• 2D Arrays – Opening Discussion

• Let’s continue with the opening example from the last section
• In your class, you might have several courses.
• So we need several 1D arrays
• Or alternatively, we can use a 2D array
• 2D Arrays – Declaration

• Similar to 1D array, type is required


• “arrayName” should be unique
• “row” and “column” should be constant expressions
• 2D Arrays – Initialization

• Following way is also valid


• 2D Arrays – Initialization

𝑁
• Following way is also valid, 𝑟𝑜𝑤 =
4

• If no initialization, the element is set to be 0 by default


• 2D Arrays – Initialization

• Following way is also invalid

• The 2D array is organized in row major order


• 2D Arrays – How Does it Look Like?
• 2D Arrays – Visiting each element of the array
• 2D Arrays – Example 1

• Given a 2D matrix, kept in a 1D array


• A[12] = {12, 13, 5, 5, 7, 21, 6, 4, 10, 5, 5, 9}
• Transpose A
• 2D Arrays – Example 1

• Given a 2D matrix, kept in a 1D array


• A[12] = {12, 13, 5, 5, 7, 21, 6, 4, 10, 5, 5, 9}
• Transpose A

• A[i][j] swaps with B[j][i]


• 2D Arrays – Example 1

• b[12] = {12, 5, 6, 5, 13, 7, 4, 5, 5, 21, 10, 9}


• A[i][j] is a[i*c1+j], where c1=3
• B[j][i] is b[j*c2+i], where c2=4
• 2D Arrays – Example 1
• 2D Arrays – Example 1

• Now do the same with two 2D arrays of relevant sizes.


• Do the same with a square matrix in-place (without a second
2D array).

You might also like