Data Structures Arrays Array Example Array Initialization

Data Structures Arrays Array Example Array Initialization

57:017, Computers in Engineering Data Structures Review of Arrays, Sorting, z Our focus thus far has mainly been on Searching control structures z An array is a data structure consisting of data items of the same type z Arrays are static data structures that stay the same size throughout the program z Dynamic data structures are also possible z Will discuss these later Arrays Array Example z An Array is: z A collection of contiguous memory locations c[0] 12 z Same name c[1] 9 z Same type c[2] 24 c[3] -15 z Locations (elements) in the array are c[[]4] 75 c[5] 319 referenced by c[6] 335 c[7] 59 z array name c[8] 61 z position within the array Note: Array indices always start at 0 z index notation: a[0] refers to the first element of array a, a[1] refers to the second element of array a, etc. /* Fig. 6.3: fig06_03.c initializing an array */ #include <stdio.h> Array Initialization int main() { z int n[ 10 ]; /* n is an array of 10 integers */ Before using arrays, we must reserve int i; /* counter */ space for all elements in the computer /* initialize elements of array n to 0 */ memory for ( i = 0; i < 10; i++ ) { z Examples n[ i ] = 0; /* set element at location i to 0 */ } ///* end for */ z int c[12]; /* Array of 12 items */ printf( "%s%13s\n", "Element", "Value" ); z int b[100]; z int x[27]; /* output contents of array n in tabular format */ for ( i = 0; i < 10; i++ ) { z Array initialization example (p. 203) printf( "%7d%13d\n", i, n[ i ] ); } /* end for */ z Initializes each element to 0 return 0; /* indicates successful termination */ } /* end main */ 1 Output Produced by the Program Array Initializers z An alternate way to initialize an array (Deitel p. 204) Element Value int n[10] = {32,27,64,9,95,14,90,70,60,37}; 0 0 z Number of initializers should equal number of array 1 0 elements 2 0 z If #initializers < #elements , all the remaining 3 0 elements are initialized to zero 4 0 z At least one element has to be initialized to zero for the rest 5 0 of the elements to be zeroed 6 0 z For example: 7 0 int n[10] = {0}; 8 0 9 0 z Arrays are NOT automatically initialized to zero. z If #initializers > #elements, ignore extra initializers. Using Symbolic Constants to Arrays as Arguments to Specify Array Sizes Functions #include <stdio.h> #define SIZE 10 z Arrays can be used as arguments to functions int main() { z To pass an array argument to a function specify int xarray[SIZE], i; the name of the array without any brackets /* Fill array w ith even num bers */ z Arrays are passed by reference for (i = 0; i < SIZE; i++) { xarray[i] = (i + 1)*2; z Example: } int hourlyTemperatures[24]; /* Print results */ for (i = 0; i < SIZE; i++) { modifyArray(hourlyTemperatures,24); printf("x[%d] = %d\n",i,xarray[i]); } } Array Arguments--Continued Array Argument Example z Arrays are passed by reference. z Example Program 6.13 from text z Function can modify the element values of the array z Prints five elements of int array a z Unsubscripted array name is actually a pointer-- contains the starting address of the array z a and its size passed to function modifyArray z Therefore, the C function can access the elements z a is repr in te d a fter the ca ll to sh ow tha t a is of the actual array modified by the functions z However, if individual array elements are used as z Element of a passed as argument is not arguments, they are passed by value modified z Stay tuned for more information on arrays and pointers z Call by value 2 /* array a passed by reference */ /* Passing arrays and individual array elements modifyArray(a, SIZE); to functions */ printf("The values of the modified array are:\n"); #include <stdio.h> for (i = 0; i < SIZE ; i++) { #define SIZE 5 printf("%3d", a[i]); } /* function prototypes */ void modifyArray(int array[], int size); printf("\n\nEffects of passing array element call " void modifyElement(int element); "by value:\nValue of a[3] is %d\n", a[3]); modifyElement(a[3]); int main() { printf("The value of a[3] is %d\n", a[3]); int a[SIZE] = {0, 1, 2, 3, 4}; int i; return 0; } printf("Effects of passing entire array call " "by reference:\n\nThe values of the " /* definition of function modifyArray() */ "original array are:\n"); void modifyArray(int b[], int size){ for (i = 0; i < SIZE ; i++) { int j; printf("%3d", a[i]); for (j = 0; j < size ; j++) { } b[j] *= 2; printf("\n"); } (Continued) } /* definition of function modifyElement() */ void modifyElement(int e) { printf("Value in modifyElement is %d\n", The const type qualifier e *= 2); } z Question: How can you prevent an array from being modified by a function if it is passed in Program Output: as a parameter? Effects of ppgassing entire array call by reference: z The values of the original array are: Answer: Use the type qualifier, const in the 0 1 2 3 4 array declaration: The values of the modified array are: 0 2 4 6 8 void tryToModify(const int b[] ) Effects of passing array element call by value: Value of a[3] is 6 z Will result in a run-time error if function Value in modifyElement is 12 attempts to modify the array. The value of a[3] is 6 Sorting and Searching Arrays Sorting Arrays z Searching for information in large data z Sorting: Placing the data into some particular structures is an important part of many order computer applications z E.g. Ascending or descending order. z Searching is usually much more efficient if z Very important computing operation the data is sorted into some order. z Bubble sort - a very simple sorting algorithm z Imagine trying to look up a number in the phone z Small values “bubble” their way to the top book if the listings were not in alphabetical order z Larger values move down to the bottom of the list z Therefore, sorting is also an important problem 3 Bubble Sort A bubble sort function: /* Bubble sorts array’s values into ascending order */ z Successive pairs of elements are compared bubblesort(int a[], int size ) { z If pair is in increasing order or identical: int i, pass, hold; for (pass = 1; pass < size ; pass++) { z Leave order alone for (i = 0; i < size - 1; i++) { Note: size-1 passes z If pair is in decreasing order if (a[i] > a[i + 1]) { /* Swap elements */ Note: Each pass z Swap the order of the two elements hold = a[[];i]; steps through the z Bubble sort is OK for sorting small arrays a[i] = a[i + 1]; array comparing and a[i + 1] = hold; possibly swapping z However it is not efficient for very large arrays } // end of if each adjacent pair of elements z Several more efficient sorting algorithms exist: } //end of inner for loop } // end of outer for loop z quick sort } // end of function bubblesort z heap sort z etc. Bubble Sort Example Bubble Sort Example First Pass: Second Pass: 24 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 89 89 89 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 89 28 28 28 28 28 28 28 28 28 1 1 1 1 28 28 28 28 89 1 1 1 1 1 1 1 1 28 24 24 24 1 1 1 1 1 89 24 24 24 24 24 24 24 24 28 28 28 24 24 24 24 24 24 89 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 89 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 89 89 89 89 89 89 89 89 89 Everything == no swap == swap == no swap == swap below this line is finished Bubble Sort Example Bubble Sort Example Third Pass: Fourth Pass: 13 13 13 13 13 13 13 13 13 13 13 13 13 24 24 24 24 24 24 24 24 24 1 1 1 1 27 27 27 1 1 1 1 1 1 24 24 24 24 1 1 1 27 24 24 24 24 24 24 24 24 24 24 24 24 24 27 27 27 27 27 27 27 27 27 28 28 28 28 28 28 28 28 28 28 28 28 28 53 53 53 53 53 53 53 53 53 53 53 53 53 Everything 60 60 60 60 60 60 60 60 60 60 60 60 60 below this line is finished 89 89 89 89 89 89 89 Everything 89 89 89 89 89 89 below this line is finished == no swap == swap == no swap == swap 4 Bubble Sort Example Bubble Sort Example Fifth Pass: Sixth Pass: 13 1 1 1 1 1 1 1 1 Note: Since nothing 1 13 13 13 13 13 13 13 13 was swapped during 24 24 24 24 24 24 24 24 24 this pass, we know that the sort is finished 24 24 24 24 24 24 24 24 24 27 27 27 27 27 27 27 27 27 Everything 28 28 28 28 28 28 28 28 28 below this 53 53 53 53 53 Everything 53 53 53 53 line is finished below this 60 60 60 60 60 line is finished 60 60 60 60 89 89 89 89 89 89 89 89 89 == no swap == swap == no swap == swap Example—Using the Bubblesort function Bubble Sort—A slight optimization /* Program to illustrate the bubblesort function printf( “\nArrayb[] sorted in ascending order\n" ); /* bubble sort */ This program sorts an array's values into /* output sorted array */ /* loop to control number of passes */ for ( i = 0; i < SIZE; i++ ) { ascending order using the function bubblesort() */ Not_Done = 1; #include <stdio.h> printf( "%4d", b[ i ] ); #define SIZE 10 } /* end for */ for ( pass = 1; (pass < size) && Not_Done); pass++ ) { Note: Portion Not_Done = 0; int bubblesort(int a[], int size); printf( "\n" ); of array that /* loop to control number of comparisons per pass */ has to be return 0; /* indicates successful termination */ for ( i = 0; i < (size – pass) ; i++ ) { int main() { considered shrinks /* initialize b */ /* compare adjacent elements and swap them if first by one on each int b[ SIZE ] ]{,,,,,,,,, = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; } /* end main */ element is greater than second element */ successive pass.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us