1 / 21

Chapter 8: Arrays By: Suraya Alias

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

mervyn
Download Presentation

Chapter 8: Arrays By: Suraya Alias

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8:Arrays By: Suraya Alias

  2. 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

  3. Figure 8.1 The Eight Elements of Array x Refer table 8.1, table 8.2

  4. 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];

  5. 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]

  6. 8.3 Using loops for sequential access

  7. Figure 8.3 Program to Print a Table of Differences (cont’d)

  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]);

  9. Figure 8.4 Data Area for Calling Module and Function do_it

  10. 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.

  11. 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.

  12. Figure 8.6 Data Areas Before Return from fill_array(x, 5, 1);

  13. Figure 8.7 Function to Find the Largest Element in an Array

  14. 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

  15. 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.

  16. 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[];

  17. Figure 8.9 Function to Add Two Arrays

  18. 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.

  19. Figure 8.11 Diagram of Function fill_to_sentinel

  20. Figure 8.12 Function Using a Sentinel-Controlled Loop to Store Input Data in an Array

  21. Figure 8.13 Driver for Testing fill_to_sentinel

More Related