chapter 8: arrays by: suraya alias
DESCRIPTION
Chapter 8: Arrays By: Suraya Alias. 8.1 Declaring and Referencing Array. Data structure A composite of related data items stored under the same name. Such as an address book : name, contact num, address Array A collection of data items of the same type Array element - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/1.jpg)
Chapter 8:Arrays
By: Suraya Alias
1-1
![Page 2: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/2.jpg)
8.1 Declaring and Referencing Array Data structure
◦ A composite of related data items stored under the same name.
◦ Such as an address book : name, contact num, address Array
◦ A collection of data items of the same type Array element
◦ A data item that is part of an array◦ The declaration
double x[8];◦ Instruct the compiler to associate eight memory cells with
the same name x Subscripted variable x[0](read as x sub zero) is used to
refer to the 1st element, x[1] to the next element and so on. Array subscript is a value or expression enclosed in the
bracket after the array name must be from the range of 0 to one less number of the memory cells in the array.
Parallel array – two or more arrays with the same number of elements used for storing related info of an object. Example, array id and array marks
1-2
![Page 3: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/3.jpg)
Figure 8.1 The Eight Elements of Array x
1-3
Refer table 8.1, table 8.2
![Page 4: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/4.jpg)
Figure 8.2 Arrays answer and score
1-4
#define NUM_QUEST 10#define NUM_CLASS_DAYS 5
typedef enum{ Monday, Tuesday, Wednesday, Thursday, Friday }class_days_t;
char answer[NUM_QUEST ];int score[NUM_CLASS_DAYS];
![Page 5: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/5.jpg)
Array DeclarationSyntax:
element-type aname[size]; //uninitialized
element-type aname[size] = {initialization list}; //initialized
Example:
#define A_SIZE 5
double a[A_SIZE]; // uninitialized
char vowels[ ]={‘A’,’E’,’I’,’O’,’U’}; // initialized
Array subscript
Syntax:
aname[subscript]
The subscript value must be between 0 and n-1
Example;
b[i+1];
a[8] 1-5
![Page 6: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/6.jpg)
8.3 Using loops for sequential access
1-6
![Page 7: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/7.jpg)
Figure 8.3 Program to Print a Table of Differences (cont’d)
1-7
![Page 8: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/8.jpg)
8.4 Using Array Elements as Function ArgumentsUsing printf for (i = 0; i < MAX_ITEM; ++i)
printf("%3d%4c%9.2f%5c%9.2f\n", i, ' ', x[i], ' ', x[i] - mean);
Using scanf for (i = 0; i < MAX_ITEM; ++i)
scanf("%lf", &x[i]);
1-8
![Page 9: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/9.jpg)
Figure 8.4 Data Area for Calling Module and Function do_it
1-9
![Page 10: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/10.jpg)
8.5 Array ArgumentsWe can write functions that have arrays
as arguments.list[i] = in_value;
Will store the same value (in_value) in all elements of the array corresponding to its formal parameter list.
In function fill_array, the array parameter is declared as int list[ ].
If the array size is not provided, we have the flexibility to pass to the function an array of any number of integers.
1-10
![Page 11: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/11.jpg)
Figure 8.5 Function fill_array
1-11
Array Correspondence for Array Parameters
If x is a five element array of type int values, the statementfill_array(x, 5, 1); will cause fill_array to store 1 in all elementsof array x.
![Page 12: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/12.jpg)
Figure 8.6 Data Areas Before Return from fill_array (x, 5, 1);
1-12
![Page 13: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/13.jpg)
Figure 8.7 Function to Find the Largest Element in an Array
1-13
![Page 14: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/14.jpg)
Array as Input ArgumentsArray Input ParameterSyntax: const element-type array-name[]
orconst element-type * array-name
Example:int get_min_sub(const double data[ ], int
data_size){….}
#The reserved word const indicates that the array variable declared is an input parameter and will not be modified by the function
1-14
![Page 15: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/15.jpg)
Returning an Array ResultIn C, is not legal for a function’s return
type to be an arrayThus requires the use of an output
parameter to send the result array to the calling module
Function add_arrays will add two arrays.The sum of ar1 and ar2 is defined as
arsumarsum[i] = ar1[i] + ar2[i], and so onParameter n will specify how many
corresponding elements are summed.1-15
![Page 16: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/16.jpg)
Figure 8.8 Diagram of a Function That Computes an Array Result
1-16
Formal parameter declaration;
Inputconst double ar1[],const double ar2[],int n;
Outputdouble arsum[];
![Page 17: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/17.jpg)
Figure 8.9 Function to Add Two Arrays
1-17
![Page 18: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/18.jpg)
Figure 8.10 Function Data Areas for add_arrays(x, y, x_plus_y, 5);
1-18
Address-of Operator (&) is not applied to the output argument becauseC passes whole arrays as argument by storing the address of the initial array element in the corresponding formal parameter.
![Page 19: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/19.jpg)
Figure 8.11 Diagram of Function fill_to_sentinel
1-19
![Page 20: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/20.jpg)
Figure 8.12 Function Using a Sentinel-Controlled Loop to Store Input Data in an Array
1-20
![Page 21: Chapter 8: Arrays By: Suraya Alias](https://reader036.vdocument.in/reader036/viewer/2022062314/56813dad550346895da77632/html5/thumbnails/21.jpg)
Figure 8.13 Driver for Testing fill_to_sentinel
1-21