Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Similar presentations


Presentation on theme: "Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler."— Presentation transcript:

1 Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

2 Numeric Arrays 1-D Arrays and Printing Array Elements An array is a data structure in C A grouping of like-type data Topics Definition of arrays Characteristics of an array Using #define directive to define array size Printing array elements

3 1-D Arrays What is a one-dimension array and how do we declare it? element_type array_name [number_of_elements]; element_type specifies the type of the array’s elements array_name is the name of the array number_of_elements specifies the max number of elements that can be stored in the array.

4 1-D Arrays int a[2]; declares the name of the array is a the type of array elements is int the dimension is 1 (with one pair of brackets []) the number of elements or size is 2

5 1-D Arrays How to name an array? Array names are classified as identifiers. c[15], f3_rack[4] (legal identifiers) 12abc[15], y4#[23] (illegal identifiers) How to distinguish between a 1-D, 2-D, or 3-D arrays? int a[2]; int b[2][3]; int c[2][3][2];

6 1-D Array What is the first index (also called subscript) of an array in C? In C, by default, the first index or subscript is 0. int a[2]; Since C begins at 0, a[0] and a[1] are used in the program body. We should not use a[2] in the program body. With a[2], the program may execute completely and give answers (which very likely will be wrong).

7 1-D Array How to print an array element using printf? Treat an array element like we treat a single variable printf(“a[0] = %3d, a[1] = %3d”, a[0], a[1]); printf(“b[3] = %8.2f, b[6] = %8.2f”, b[3], b[6]); What types of variables can be used for array indices? Integer type variables such as int char with their modifiers signed, unsigned, short, and long

8 /* Program for Lesson 6_1 */ #define N 10 void main(void) { int a[2]; double b[N]; a[0]=11; a[1]=22; b[3]=777.7; b[6]=888.8; printf("a[0] = %3d, a[1] = %3d\n", a[0],a[1]); printf("b[3] = %8.2lf, b[6] = %8.2lf \n", b[3],b[6]); printf("b[2] = %lf\n", b[2]); printf("a[3] = %d\n", a[3]); } The results make no sense.

9 Array Initialization Topics Methods for initializing array elements in declarations Initialing array elements using scanf Initialing array elements using assignment statements in loops

10 Initialization What are two ways to initialize the elements of a 1-D array in a declaration? 1. type name [number_of_elements] = {value_0, …, value_n}  int a[3] = {11, 22};  a[0] = 11, a[1] = 22, and a[3] = 0. 2. type name [] = {value_0, …, value_n} int b[] = {44, 55, 66}; Advantages: unnecessary to change the number of elements values and count the number of array elements Disadvantages: may lead to out-of-range errors

11 Initialization How else to initialize an array? Use a read function, such as scanf double x[2]; scanf(“%lf %lf”, &x[0], &x[1]); Use an assignment statement double y[10]; for (i=0; i<10; i++) { y[i] = i*100.0; }

12 Initialization Would the declaration “int b[2]={44, 55, 66}” have caused an error in C? Yes! Since b has only 2 elements, we listed three. Although C will not detect out-of-range errors with arrays, it will detect this error.

13 One-Dimensional Arrays The simple built-in data types of the C language - int, float, double, and char - are not sufficient to represent complex data such as lists, tables, vectors, and matrices. A one-dimensional array is a data type derived from the basic built-in data types. In scientific and engineering computations, there are vectors, matrices, and determinants.

14 The flight path of a space shuttle, projectile or orbiters, as well as pressure measurements under the sea floor, are all recorded and stored as lists using one-dimensional arrays. Operations such as sorting and searching require an entire list of data to be stored in memory. This is accomplished by storing the data in a one- dimensional array.

15 7.1 One-Dimensional Arrays Subscripts and Subscripted Variables Declaration of Arrays Initialization of Arrays

16 array An array is a named sequence of memory locations that is used to store data of a homogeneous data type. Each of the named memory locations is called an element of the array. The elements of an array are identified by a subscript or an index. Here are some practical applications: altitude, depth, pressure, velocity, and temperature measurements may be stored in one-dimensional arrays.

17 Subscripts and Subscripted Variables

18

19

20 Declaration of Arrays

21

22

23

24

25 Initialization of Arrays

26

27

28

29

30 7.2 Input of One-Dimensional Arrays Array Input The input of data to a one-dimensional array may use indexing, or may use array pointers or dynamic pointers which will be presented in Chapter 10. Input of Parallel Arrays

31 Array Input

32

33

34 Input of Parallel Arrays

35

36 7.3 Output of One-Dimensional Arrays Array Output Output may be displayed on a monitor, printed using a printer, or written to an output data file. Output of Parallel Arrays

37 Array Output

38

39

40 Output of Parallel Arrays

41

42 Manipulation of Arrays Arithmetic, relational, and logical operations can be performed on array elements. An array can be added to another array and the result can be stored in a third. All these operations are performed element-by- element using for loops.

43 7.4 Manipulation of Arrays Array Assignment Array Arithmetic

44 Array Assignment

45

46

47

48

49

50 Array Arithmetic

51

52

53

54 Passing Arrays to Functions Arrays can be passed to functions as though they were single variables; but rather than being a set of values, an array name is an address constant. An entire array is effectively passed by using the array name as an argument, which passes the address of the array. If an array is passed element-by-element as single variables, the elements may be passed by value or explicitly by pointer. It is a normal practice to pass arrays by name into input functions, thus the input values from the function are passed back to the storage allocated in the calling function.

55 7.5 Passing Arrays to Functions Passing Fixed-Sized Arrays Passing Array Elements

56 Functions and 1-D Arrays Topics Passing individual array elements to functions Passing entire arrays to functions Passing entire arrays to functions with a restriction Illustrate how values and address of array elements are passed

57 Function Call How to pass a single array element to a function? We treat a single element like a simple variable. If we want to change the value of the array element in the function, we use the “address of” operator, &, before the array element in the function call. If we want to pass element without having it changed in the function, we simple put the array element in the parameter list. function1(&a[5], a[8]);

58 Function Prototype When receiving an address, we must use a pointer variable (indicated in the declaration by *). When receiving a value, we use a simple variable. void function1(int *d, int e); function1(&a[5], a[8]); void function1(int *d, int e) Value passed to simple variable Address passed to pointer variable *d = 100 + e;

59 Function Call and Prototype How to pass the ability to access an entire 1-D array to a function? Pass the address of the first element of the array With the address of the first element, C can internally calculate the address of any element in the array. The address of the first element of an array is indicated by the array name with no brackets following it. &c[0] and c are equivalent.

60 Function Call and Prototype function2(c, 5) Pass the address of the first element of the array c[ ] to function2, which gives function2 the ability to modify the array c[ ]. The prototype for the function must indicate that it is receiving an address. Use * in the declaration void function2 (double *b, int num_elem); Use brackets [ ] void function2 (double b[ ], int num_elem);

61 Function Call and Prototype function2(c, 5) void function2 (double b[ ], int num_elem); Number of array elements passed as a simple variable Address of the first element passed to pointer variable indicated with brackets

62 Function Definition Within a function that has received an array’s address, how to work with the array? Must be cognizant of the number of elements that the array contains. void function2 (double b[ ], int num_elem) void function2 (double b[5]) This is correct – A separate parameter is used to transfer the number of elements. This is incorrect – It does not indicate that b[ ] has only five elements.

63 Passing Fixed-Sized Arrays

64

65 Output of an Array in a Function

66 Manipulation of Arrays in a Function

67 Passing Array Elements

68 Sample Programs Reynolds Numbers Stress and Strain Standard Deviation Maximum and Minimum Values Sorting Searching Inventory of an Engineering Sales Company

69 Bubble Sort What concept underlines a bubble sort? Want to sort the 1-D array, b[4] = {33, 44, 11, 22}, in ascending order. First compare b[0] and b[1]; if b[0] > b[1], swap the values; otherwise, no rearrangement. Perform the same action for b[1] and b[2]. Continue this with the last pair, b[2] and b[3]. This concludes the first round. The largest element, 44, bubbles down and stops in the last element of the array.

70 Bubble Sort Bubble sort Round 1 b[3] is the largest of the four values Round 2 b[2] is the largest of the three values Round 3 b[1] is the larger of the two values Round 4 b[0] is automatically the smallest Swapping the values of array elements temp = b[1]; copy the value of b[1] into temp b[1] = b[2]; copy the value of b[2] into b[1] b[2] = temp; copy the value of temp into b[2]

71 #include #define START 0 #define END 4 #define SIZE 10 void main(void) { int i, j, k, b[SIZE], c[SIZE], d[SIZE]; int temp, max, wheremax=END-1, min, wheremin=START; int a[END]={33,44,11,22}; /************************************************ ** INITIALIZE ARRAYS b, c, AND d ************************************************/ for (i=START; i<END ;i++) b[i]=c[i]=d[i]=a[i]; /************************************************ ** BUBBLE SORT ************************************************/ for (i=START;i <END;i++) { for (j=START; j <END-i-1;j++) { if (b[j] > b[j+1] ) { temp=b[j+1]; b[j+1]=b[j] ; b[j]=temp; } Each time through loop is one round for the bubble sort The swap is performed Beginning and ending of portion of array to be sorted

72 Exchange Maximum Sort Exchange maximum sort Assign the last array element to the variable max. Compare max with the rest of the array elements; if the max is smaller than any array element, then replace max and use the variable wheremax to relocate the new maximum. Continue the process until the max in the array is found. Put the found max at the last element of the array. This completes the first round. Eliminate the last element from the search group. Repeat steps 2, 3, and 4 until the array is in ascending order.

73 /******************************************* ** EXCHANGE MAXIMUM SORT *******************************************/ for (i = END-1; i >= START; i--) { max = c[i]; for (j = i; j >= START; j--) { if (max <= c[j]) { max = c[j]; wheremax = j; } c[wheremax] = c[i]; c[i] = max; } Each time through loop is one round for the exchange maximum sort The swap is performed Compare max with the rest of the array elements; if the max is smaller than any array element, then replace max and use the variable wheremax to relocate the new maximum. Put the found max at the last element of the array. Eliminate the last element from the search group

74 Exchange Minimum Sort Similar to the Exchange Maximum Sort Finds the min value and puts it in the first loation In ascending order

75 /******************************************** ** EXCHANGE MINIMUM SORT ********************************************/ for (i=START;i <END;i++) { min=d[i]; for (j=i;j <END;j++) { if (min >= d[j]) { min=d[j]; wheremin=j; } d[wheremin]=d[i]; d[i]=min; }


Download ppt "Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler."

Similar presentations


Ads by Google