210 likes | 343 Views
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
E N D
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 • 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
Figure 8.1 The Eight Elements of Array x Refer table 8.1, table 8.2
Figure 8.2 Arrays answer and score #define NUM_QUEST 10#define NUM_CLASS_DAYS 5typedef enum{ Monday, Tuesday, Wednesday, Thursday, Friday }class_days_t;char answer[NUM_QUEST ];int score[NUM_CLASS_DAYS];
Array Declaration Syntax: 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]
8.4 Using Array Elements as Function Arguments • Using 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]);
8.5 Array Arguments • We 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.
Figure 8.5 Function fill_array 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.
Figure 8.6 Data Areas Before Return from fill_array(x, 5, 1);
Array as Input Arguments • Array Input Parameter • Syntax: const element-type array-name[]orconst element-type * array-name • Example: intget_min_sub(const double data[ ], intdata_size){ …. } #The reserved word const indicates that the array variable declared is an input parameter and will not be modified by the function
Returning an Array Result • In C, is not legal for a function’s return type to be an array • Thus 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 arsum • arsum[i] = ar1[i] + ar2[i], and so on • Parameter n will specify how many corresponding elements are summed.
Figure 8.8 Diagram of a Function That Computes an Array Result Formal parameter declaration; Inputconst double ar1[],const double ar2[], int n; Output double arsum[];
Figure 8.10 Function Data Areas for add_arrays(x, y, x_plus_y, 5); 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.
Figure 8.12 Function Using a Sentinel-Controlled Loop to Store Input Data in an Array