1 / 8

Arrays

Arrays. The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and matrices. Sorting and searching require an entire list of data to be stored in memory.

cisco
Download Presentation

Arrays

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. Arrays • The simple built-in data types of the C language such as int, float, -are not sufficient to represent complex data such as lists, tables, vectors, and matrices. • Sortingand searchingrequire an entire list of data to be stored in memory. • The most convenient way to accomplish this is to store the data in a one-dimensional array.

  2. One-Dimensional Arrays • An arrayis a named sequence of memory locations that is used to store different values of the same 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 (0,1,2,3,4). • Above, x is the name of the array, containing the elements x[0], x[1], x[2], x[3], and x[4].

  3. Array Declaration and Initialization In C, there are two ways to represent a subscripted variable such as: ai i = 0 to 9 The first way it can be represented is as regular variables, as we have been doing so far: a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 The above are ten different, explicitly named variables. However, if you have a large number of named variables, it is not practical to name them as shown above. Instead, we use subscripts (or array) as shown below: a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] The above is a declaration of an array a which holds 10 different items such as int, or float. The declaration, initialization of arrays is as follows:  //declare the array int i;//declare an integer named i int a[5]; //declare array a: a[0], a[1], a[2], a[3], a[4]  for(i = 0; i < 5; i++)//loop to go through array { a[i] = 2; // initialize array aequal to 2 }

  4. Arrays Declaration and Initialization  One-dimensional arrays may be declared individually as follows: int count[10]; float pressure[20]; float temperature[40]; float velocity[40]; Or all of the float data types can be declared in one declaration statement as follows: float pressure[20], temperature[40], velocity[40]; One-dimensional arrays may be initialized in a declaration statement as follows: inta[5] = {5, 9, 8, 4, 7}; //where a[0] is 5, a[1] is 9, a[2] is 8, a[3] is 4, and a[4] is 7 The following example shows the initialization of fewer values than the size of the array: int a[5] = {5, 9, 8, 4}; //where a[0] is 5, a[1] is 9, a[2] is 8, a[3] is 4, and a[4] is 0 Notice that the unspecified element a[4] is 0. If a value is not specified, then a zero is assigned.

  5. Arrays Declaration/Initialization One-dimensional arrays may be initialized using assignment statements as follows: int a[5]; a[0] = 5; a[1] = 9; a[2] = 8; a[3] = 4; a[4] = 7; Initialization is usually quickly done with a for loop as follows:  int i, count [5]; float velocity[10]; for(i = 0; i < 5; i++){ count[i] = 2; } for(i = 0; i < 10; i++){ velocity[i]= 500.75; }

  6. Array Input/Output

  7. Sorting algorithms: • Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted • It compares each pair of adjacent items and swaps them, if they are in the wrong order. • Every pass, the largest value “bubbles up” to the top. for(int x=0; x<n; x++) { for(int y=0; y<n-1; y++) { if( array[y] > array[y+1]) { int temp = array[y+1]; array[y+1] = array[y]; array[y] = temp; } } }

  8. H.W. Arrays • Compile and run the program on slide 6 - Save it as sampleArrays.c • Write a program to: • Ask the user for 7 temperature and 7 pressure values for a scientific experiment. • Store the values in two one-dimensional arrays. • Identify the highest temperature and pressure. • Show the highest temperature and pressure to the user. • Save your program as tempPressArrays.c

More Related