1 / 8

Lab 11

Lab 11. Arrays Exercises Note: Read the whole Chapter 8. Arrays. A data structure is a grouping of related data items in memory. An array is a data structure used to store a collection of data items of the same type.

zahir-hart
Download Presentation

Lab 11

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. Lab 11 • Arrays • Exercises Note: Read the whole Chapter 8.

  2. Arrays • A data structure is a grouping of related data items in memory. • An array is a data structure used to store a collection of data items of the same type. • An individual array element is referenced by placing immediately after the array name a square-bracketed subscript for each dimension. • 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]. • 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 • 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.

  3. Arrays (continue) • The name of an array with no subscript always refers to the address of the initial array element. • A function that creates an array result should require the calling module to pass an output argument array in which to store the result. • Examples: • 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. Initialization: • 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’. • 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. • Input parameter • 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. • Input/Output parameter • 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. Array references • if (data[0]<39.8) : compares value of initial element of array data to 39.8 • for (i=0;i<30,i++) data[i]/=2.0; ?? • for(i=0;i<2;++i) ?? for(j=0;j<3;++j) printf(" %6d", matrix[i][j]);

  6. Exercises • Exercise1: 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. • 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. • 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.

  7. Multidimensional Arrays • You 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. • example: char tictac[3][3]={ {‘ ‘ , ‘ ‘, ‘ ‘} , {‘ ‘ , ‘ ‘, ‘ ‘}, {‘ ‘ , ‘ ‘, ‘ ‘}}; it initializes the content of the tic-tac-toe to blanks. • Arrays with several dimensions • 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 • Write a C program that finds and displays the total number of students in each course. • Write a C program that displays the number of students at each campus.

More Related