Lab 11 r Arrays r Exercises Note Read

Lab 11 r Arrays r Exercises Note: Read the whole Chapter 8.

Arrays r A data structure is a grouping of related data items in memory. r An array is a data structure used to store a collection of data items of the same type. r An individual array element is referenced by placing immediately after the array name a square-bracketed subscript for each dimension. l The initial element of one-dimensional array x is referenced by x[0]. If x has n elements, the final element is referenced as x[n-1]. r An indexed for loop whose counter runs from 0 to one less than an array’s size enables us to reference all the elements of a one-dimensional array in sequence by using the loop counter as the array subscript. Nested for loops provide sequential access to multidimensional array elements r For an array declared as function parameter, space is allocated in the function data area for only the address of the initial element of the actual argument passed.

Arrays (continue) r The name of an array with no subscript always refers to the address of the initial array element. r A function that creates an array result should require the calling module to pass an output argument array in which to store the result. r Examples: l Array declarations: - double data[30]; : allocates storage for 30 type double items in array data (data[0], data[1]……data[29]) - int matrix[2][3]: 6 type int items (2 rows of 3 columns) in two-dimensional array matrix (matrix[0][0], matrix[0][1], …. . matrix[1][2]).
![r Initialization: l char vowels[3] ={‘A’, ’E’, ’I’}: allocates storage for 3 type char r Initialization: l char vowels[3] ={‘A’, ’E’, ’I’}: allocates storage for 3 type char](http://slidetodoc.com/presentation_image_h/a94e748c147d6d7edf7ce8abe2d585c7/image-4.jpg)
r Initialization: l char vowels[3] ={‘A’, ’E’, ’I’}: allocates storage for 3 type char items in array vowels and initializes the array contents: vowels[0]=‘A’, vowels[1]=‘E’, vowels[2]=‘I’. l int id[2][2]={{1, 0}, {0, 1}}: Allocates 4 locations for the 2*2 matrix id, initializing the storage so id[0][0]=1, id[0][1]=0, id[0][0]=0, id[0][1]=1. r Input parameter l void print_alpha(const char alpha[], const int m[], int a_size, int m_size) : states that the function print_alpha uses arrays aplha and m as input parameters, only print_alpha will not change their contents. r Input/Output parameter l void fill(int nums[], int n) or …(int*nums, …): states that function fill can both look at and modify the actual argument array passed to nums.
![r Array references l if (data[0]<39. 8) : compares value of initial element of r Array references l if (data[0]<39. 8) : compares value of initial element of](http://slidetodoc.com/presentation_image_h/a94e748c147d6d7edf7ce8abe2d585c7/image-5.jpg)
r Array references l if (data[0]<39. 8) : compares value of initial element of array data to 39. 8 l for (i=0; i<30, i++) data[i]/=2. 0; ? ? l for(i=0; i<2; ++i) ? ? for(j=0; j<3; ++j) printf(" %6 d", matrix[i][j]);

Exercises r Exercise 1: Write a C program that computes the mean and standard deviation of an array of data and displays the difference between each value and the mean. r Exercise 2: Write a C program that uses a function get_max() to find the largest of the first n values in an array list. r Exercise 3: Write a C program that uses a function search() to search for the target item in first n elements of an array gotten as input.

Multidimensional Arrays r You can initialize arrays in their declarations just like you initialize onedimensional arrays. Still, instead of listing all table values in one list, the values are usually grouped by rows. l example: char tictac[3][3]={ {‘ ‘ , ‘ ‘} , {‘ ‘ , ‘ ‘, ‘ ‘}}; it initializes the content of the tic-tac-toe to blanks. r Arrays with several dimensions l Example: int enroll[MAXCRS][5][4]; This is a three-dimensional array that may be used to store the enrollment data for a college. The MAXCRS refers to the maximum number of courses offered at 5 different campuses. With the 4 refers to the year : 0 refers to the freshman year 0, the sophomore year 1, and so on. Thus, the enroll [1][4][3] represents the number of seniors taking course 1 at campus 4.

Exercises r Write a C program that finds and displays the total number of students in each course. r Write a C program that displays the number of students at each campus.
- Slides: 8