chapter 8: arrays by: suraya alias

Post on 07-Jan-2016

49 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

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 Presentation

TRANSCRIPT

Chapter 8:Arrays

By: Suraya Alias

1-1

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

Figure 8.1 The Eight Elements of Array x

1-3

Refer table 8.1, table 8.2

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

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

8.3 Using loops for sequential access

1-6

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

1-7

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

Figure 8.4 Data Area for Calling Module and Function do_it

1-9

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

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.

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

1-12

Figure 8.7 Function to Find the Largest Element in an Array

1-13

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

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

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

Figure 8.9 Function to Add Two Arrays

1-17

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.

Figure 8.11 Diagram of Function fill_to_sentinel

1-19

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

1-20

Figure 8.13 Driver for Testing fill_to_sentinel

1-21

top related