Bài giảng Introduction to computer programming - Chapter 7: Arrays

Ho Chi Minh City University of Technology  
Faculty of Computer Science and Engineering  
Chapter 7: Arrays  
Introduction to Computer Programming  
(C language)  
TS. Võ Thị Ngọc Châu  
2017 – 2018, Semester 2  
Course Content  
C.1. Introduction to Computers and  
Programming  
C.2. C Program Structure and its  
Components  
C.3. Variables and Basic Data Types  
C.4. Selection Statements  
C.5. Repetition Statements  
C.6. Functions  
C.7. Arrays  
C.8. Pointers  
C.9. File Processing  
2
References  
[1] “C: How to Program”, 7th Ed. – Paul  
Deitel and Harvey Deitel, Prentice Hall, 2012.  
[2] “The C Programming Language”, 2nd Ed.  
– Brian W. Kernighan and Dennis M. Ritchie,  
Prentice Hall, 1988  
and others, especially those on the Internet  
3
Content  
Introduction  
One-dimension arrays  
Memory model of a C array  
Access to the elements of a C array  
Arrays of characters for strings  
Multidimensional arrays  
Passing arrays to functions  
Summary  
4
Introduction – Data Types in C  
(Recall from Chapter 3)  
Built-in data types (primitive/fundamental)  
char (signed char), unsigned char  
short int, unsigned short, int, unsigned int, long int,  
unsigned long int, long long int, unsigned long long  
float, double, long double  
void  
enum (enumerated data associated with integers)  
Derived data types  
arrays [] of objects of a given type  
pointers * to objects of a given type  
structures struct containing objects of other types  
union containing any one of several objects of  
5
various types  
Introduction  
void main() {  
Given a  
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};  
set of n  
positive  
numbers,  
find the  
smallest  
one.  
Variable declaration  
for an array of  
doubles.  
int n = 10;  
double minNumber = positiveNumber[0];  
int iteration = 1;  
We haven‟t discussed  
it, have we???  
while (iteration < n) {  
if (minNumber <= positiveNumber[iteration])  
iteration = iteration + 1;  
else {  
minNumber = positiveNumber[iteration];  
iteration = iteration + 1;  
}
(Chapter 1 –  
}
Real code in C)  
}
6
Introduction  
In your exercise for practice, B.6, chapter  
3, you are asked to recommend the nearest  
place from 4 given places to student B  
based on his/her current location.  
How did you implement those?  
What happens if there are 20 places for recommendation?  
7
Introduction  
In our real world, our problem is related to  
not only a single object of interest but also  
a set of objects of interest.  
Example 1: grade a list of submissions of each  
of you for every chapter  
Example 2: recommend the most relevant place  
to visit from a list of places in a city  
Example 3: sort a list of suppliers based on their  
transaction‟s values  
Example 4: find a suitable topic among a list of  
topics for your assignment  
8
…  
Introduction  
Array  
A means to represent collections in our real world  
A data structure consisting of related data items of  
the same type  
A group of contiguous memory locations that all  
have the same type  
An array remains the same size throughout its existence.  
Suppliers  
lstSupplier in memory  
id  
A C T I V E S F O R  
1 2 3 8 3 4 7 2 9 6  
value  
struct supplier lstSupplier[10];  
9
Real world vs. computer‟s world  
Introduction  
Array  
A means to represent collections in our real world  
A data structure consisting of related data items of  
the same type  
A group of contiguous memory locations that all  
have the same type  
An array remains the same size throughout its existence.  
The 1st supplier  
The 6th supplier  
The 10th supplier  
Suppliers  
lstSupplier in mry  
id  
A C T I V E S F O R  
1 2 3 8 3 4 7 2 9 6  
value  
struct supplier lstSupplier[10];  
10  
Real world vs. computer‟s world  
One-dimension arrays  
A one-dimension array is the simplest type of  
array in C.  
type_name variable_name[constant_expressionopt] =opt {expression_list}opt;  
variable_name: a valid identifier for a collection  
type_name: a valid data type  
Basic data type name, derived data type name, or new  
name of some existing data type  
constant_expressionopt: optional, if specified, an  
integer expression used for a size of the array  
showing the number of objects of interest in the collection  
expression_list: optional, a list of expressions  
separated by comma for initialized values  
11  
One-dimension arrays  
A one-dimension array is the simplest type of  
array in C.  
type_name variable_name[constant_expressionopt] =opt {expression_list}opt;  
Example 1: an array of 15 integer numbers to represent the number  
of products bought by 15 customers  
numProduct in memory  
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
int numProduct[15];  
12  
One-dimension arrays  
A one-dimension array is the simplest type of  
array in C.  
type_name variable_name[constant_expressionopt] =opt {expression_list}opt;  
Example 2: an array of 10 doubles to represent 10 positive numbers  
positiveNumber in memory  
2
1
3 10 8  
3
4
5
9 12  
double positiveNumber[10];  
13  
One-dimension arrays  
A one-dimension array is the simplest type of  
array in C.  
type_name variable_name[constant_expressionopt] =opt {expression_list}opt;  
Example 3: an array of 10 elements of struct supplier to represent  
10 suppliers  
lstSupplier in memory  
id  
A C T I V E S F O R  
1 2 3 8 3 4 7 2 9 6  
value  
14  
struct supplier lstSupplier[10];  
Memory model of a C array  
When defined, an array can be:  
A global variable,  
A local variable,  
A static local variable,  
A dynamic local variable, …  
It will be organized with a fixed size in an appropriate  
corresponding memory segment (.data, .bss, stack, heap).  
Regardless of its memory segment, an array is a group of  
contiguous memory locations that all have the same type.  
Array name is the address of the first location among these contiguous  
memory locations.  
positiveNumber in memory  
positiveNumber is the address of  
2
1
3 10 8  
3
4
5
9 12  
the first double memory location.  
double positiveNumber[10];  
15  
Memory model of a C array  
8 bytes  
8 byte8s bytes8 bytes  
positiveNumber in memory  
2
1
3 10 8  
3
4
5
9 12  
double positiveNumber[10];  
16  
Memory model of a C array  
Array name is the address of the first location among  
these contiguous memory locations.  
Therefore, assignment for an entire array is not valid!!!  
a = {4, 2, 9, -8, 0}; // INVALID!!!!!!!!!!!!!!  
17  
Memory model of a C array  
intArray in memory  
3
0
8
9 -2  
int intArray[] = {3, 0, 8, 9, -2};  
The compiler determined the size of this  
array based on the number of initial values  
18  
Access to the elements of  
a C array  
Element access is based on index starting  
from 0 (zero) with name[..].  
Index for the first element is 0: name[0]  
Index for the second element is 1: name[1]  
Index for the (i+1)-th element is i: name[i]  
Index for the n-th element is n-1: name[n-1]  
positiveNumber in memory  
2
1
3 10 8  
3
4
5
9 12  
positiveNumber[0]  
double positiveNumber[10];  
positiveNumber[5] positiveNumber[9]  
19  
Access to the elements of  
a C array  
Element access is based on index with name[..].  
intArray in memory  
3
0
8
9 -2  
int intArray[] = {3, 0, 8, 9, -2};  
20  
Tải về để xem bản đầy đủ
pdf 58 trang yennguyen 19460 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to computer programming - Chapter 7: Arrays", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_chapter_7_arr.pdf