Download presentation
Presentation is loading. Please wait.
Published byΧθόνια Αλεξίου Modified over 5 years ago
1
Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei
2
Array What arrays are? How to use arrays in your program?
How memory is used by an array? What a multidimensional array is? How to write a game of tic-tac-toe? Beiginning C / Qinpei Zhao 2019/4/27
3
Recursive Rabbit problem
A rabbit gives birth of a baby rabbit every month since the third month of its own birth. And the baby rabbit gives birth of baby rabbit every month since the third month of its birth. Suppose the rabbits never die, how many rabbits are there in every month? Rabbit problem
4
Programming without arrays
the average of ten numbers Not storing each grade (lec5_1_noarray.c) Beginning C / Qinpei Zhao 2019/4/27
5
Storing numbers in hard way
Beginning C / Qinpei Zhao 2019/4/27
6
What is an array? An array is a fixed number of data items that are all of the same type. The data items in an array are referred to as elements. How many elements you want to store in your array is called the array dimension. long numbers[10]; Each individual value in the array is identified by an “index value”. numbers[0], numbers[1]… Beginning C / Qinpei Zhao 2019/4/27
7
What is an array? place an expression to access the value of an element, the expression would have to result in an integer value that corresponds to one of the possible index values numbers[i+2] note the legal range of an array “out of bounds” number[10] Beginning C / Qinpei Zhao 2019/4/27
8
Using arrays Declare an array Store numbers in the array
Beginning C / Qinpei Zhao 2019/4/27
9
About memory again Each variable that you use in a program takes up a certain amount of memory, measured by bytes (8 bits). The label for a byte is called its address in hexadecimal . Address of operator & loaded memory differs each time a new format specifier %p Beginning C / Qinpei Zhao 2019/4/27
10
Memory (cont.) Stack Segment Low High Beginning C / Qinpei Zhao
2019/4/27
11
Arrays and Addresses The organization of an array in memory
Value of the ith element address Beginning C / Qinpei Zhao 2019/4/27
12
Initializing an array Only for safety’s sake, assign initial values for the elements of the array If there are fewer initializing values than elements, the elements without initializing values will be 0. The compiler can deduce the number of elements from the list of values. 10 Beginning C / Qinpei Zhao 2019/4/27
13
An example (lec5_3_1array.c) Beginning C / Qinpei Zhao 2019/4/27
14
An example on initialization
(lec5_4_nodata/somedata.c) Beginning C / Qinpei Zhao 2019/4/27
15
Multidimensional arrays
2-dimensional array float numbers[3][5]; Beginning C / Qinpei Zhao 2019/4/27
16
Initializing multidimensional arrays
Beginning C / Qinpei Zhao 2019/4/27
17
An example Get a max value in a 2-dimensional array
Beginning C / Qinpei Zhao 2019/4/27
18
Designing a program tic – tac –toe (noughts and crosses)
Tic-tac-toe is played on a 3*3 grid of squares. Two players take turns entering either an X or an O in the grid. The player that first manages to get three of his or her symbols in a line horizontally, vertically, or diagonally is the winner. tic – tac –toe (noughts and crosses) lec5_problem.c Beginning C / Qinpei Zhao 2019/4/27
19
Summary About array A fixed number of elements of the same type Access any element within the array using the array name and one or more index values Index values are integers Combining arrays with loops provides a very powerful programming capability. Organize the data using multidimensional arrays By applying nested loops to multidimensional arrays, you can process all the array elements with a very small amount of code. To be continued on array with strings of characters. Beginning C / Qinpei Zhao 2019/4/27
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.