Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 11 rArrays rExercises Note: Read the whole Chapter 8.

Similar presentations


Presentation on theme: "Lab 11 rArrays rExercises Note: Read the whole Chapter 8."— Presentation transcript:

1 Lab 11 rArrays rExercises Note: Read the whole Chapter 8.

2 Arrays rA data structure is a grouping of related data items in memory. rAn array is a data structure used to store a collection of data items of the same type. rAn 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]. rAn 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 rFor 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.

3 Arrays (continue) rThe name of an array with no subscript always refers to the address of the initial array element. rA function that creates an array result should require the calling module to pass an output argument array in which to store the result. rExamples: 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]).

4 rInitialization: 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. rInput 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. rInput/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.

5 rArray 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(" %6d", matrix[i][j]);

6 Exercises rExercise1: 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. rExercise 2: Write a C program that uses a function get_max() to find the largest of the first n values in an array list. rExercise 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.

7 Multidimensional Arrays rYou can initialize arrays in their declarations just like you initialize one- dimensional 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. rArrays 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.

8 Exercises rWrite a C program that finds and displays the total number of students in each course. rWrite a C program that displays the number of students at each campus.


Download ppt "Lab 11 rArrays rExercises Note: Read the whole Chapter 8."

Similar presentations


Ads by Google