arrays&(1)&santi/cpe102_files/... · arrays& •...
TRANSCRIPT
![Page 1: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/1.jpg)
Arrays (1)
ISNE 261102 Faculty of Engineering Chiang Mai University
![Page 2: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/2.jpg)
Arrays
• In case we want a variable to hold many values.
• An array is a consecu've group of memory loca'ons that all have the same type.
• To refer to a parHcular locaHon or element in the array, we specify the name of the array and the posiHon number of the parHcular element in the array.
![Page 3: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/3.jpg)
Arrays
![Page 4: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/4.jpg)
Arrays
• Here shows an integer array called c that contains 12 elements.
• We refer to any one of these elements by giving the array name followed by the parHcular element’s posi%on number (called index) in square brackets ([]).
• The first element has index 0 (zero).
![Page 5: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/5.jpg)
Arrays • The array c has 12 elements, which are referred to as c[0]
to c[11]. • The value of c[0] is -‐45, the value of c[1] is 6, the value of
c[2] is 0, the value of c[7] is 62, and the value of c[11] is 78. • To print the sum of the values contained in the first three
elements of array c, we’d write
• To divide the value of c[6] by 2 and assign the result to the variable x, we would write
![Page 6: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/6.jpg)
Declaring Arrays • Like a regular variable, an array must be declared before it is used. • To tell the compiler to reserve 12 elements for integer array c, use
the declaraHon: !int c[12];!
• An array can be explicitly iniHalized to specific values when it is declared by enclosing those iniHal values in braces {}. For example: int c[5] = {16, 2, 77, 40, 12071};!
• The iniHalizer can even have no values !int c[5] = {};!
• We can also leave the square brackets empty []. The compiler will automaHcally assume the size of the array that matches the number of values iniHalized within {}. int c[] = {16, 2, 77, 40, 127};!
![Page 7: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/7.jpg)
Examples Using Arrays
• Example 1: Array of zeros
![Page 8: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/8.jpg)
Examples Using Arrays
• Example 2: IniHalizer
![Page 9: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/9.jpg)
Examples Using Arrays
• Example 3: Assigning values using operaHon
const quanHfier is used to declare a constant variable, which cannot be modified therea_er – also called read-‐only variable.
![Page 10: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/10.jpg)
Examples Using Arrays
• Example 4: Sum
![Page 11: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/11.jpg)
Examples Using Arrays
• Example 5: Frequency
![Page 12: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/12.jpg)
Examples Using Arrays
• Example 6: Histogram
![Page 13: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&](https://reader034.vdocuments.us/reader034/viewer/2022050519/5fa298f2607a3b5c947608ea/html5/thumbnails/13.jpg)
Lab Assignment #8