Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.

Similar presentations


Presentation on theme: "C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type."— Presentation transcript:

1 C Static Arrays Pepper

2 What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type size_t – starting at (myarray[2])

3 Defining Arrays Tell the compiler the size and type and give it a name: – int b[10] makes 10 boxes, from 0 to 9 Initialize later or upon creation: – int b[5] = {33,22,10,11,5}; – int a[] = {1,2,3}; - compiler figures out 3 elements You can create a variable length array by using variable in place of a number as the length.

4 Array Structure Array variable name holds the starting address Array can tell where its own self ends You can tell how many if you know the size of the variable type

5 Array Access Sample program to initialize and print all Changes to make: – Initialize too few b[10] – Initialize too many b[2] – See no crash #include int main (void) { int b[5] = { 33, 22, 10,15,5 }; size_t c; for (c = 0; c < 5; c++) { printf ("value at c[%zd] = %d\n", c, b[c]); }

6 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Sum an Array

7 Bubble Sort Work from left to right moving one element over until it is lower than one next to it. Repeat for as many passes as elements Once sorted, can do binary search

8 Bubble Sort ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

9 Bubble Sort ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

10 Bubble Sort

11 Tricky – use Array item as subscript Count frequency of survey responses ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

12 Survey cont. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13 Static Array created inside a function Static keyword on array – values will persist if you return to the function Static int arrays initialize to 0 upon creation #include void arrayInit (void); int main (void) { puts ("first array use"); arrayInit (); puts ("second array use"); arrayInit (); } void arrayInit (void) { static int array1[3]; //int array1[3] = {0,0,0}; size_t i; for (i = 0; i < 3; i++) { printf ("array1[ %zd ] = %d\n", i, array1[i] += 5); }

14 Get Array Size It knows where it starts and where it ends. Ask array for its total size : – it tells you how many bytes – Divide the number of bytes by the array type sizeof(myarray) returns total bytes sizeof(int) returns size of one integer Total array elements is then: – sizeof(myarray) / sizeof(myarray[0])

15 Strings – Character Arrays Initialize as a string: – char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; – char string1[] = "first"; Access full string as array name – printf(“%s”,string1); Access portion with index – printf(“%s”,string1[2]); // will print ‘r’ The array name does hold an address, so no need for & operator on scanf – char string2[ 20 ]; // remember 1 for ‘\0’ – scanf( "%19s", string2 ); – Scanf will overwrite the bounds of the array – String2 knows it ends because of the null, but if write over null, array is confused.

16 Passing Arrays to Functions Passes Array by reference without & or * – Because array name holds the address – Send the size over as well because it cannot figure out the size Passes Array element (scalars) by value – B[1] holds an a variable value, not an address Function header: – void arrayfunc (int b[], size_t arrSize) Call to function: – arrayfunc(b,10); Const If no changes allowed to array elements – void arrayfunc (const int b[], size_t arrSize)

17 Sample Array pass to function ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

18 Multi-dimension Arrays Second subscript : b[4][3] Initialize: – int b[2][3] = {{1,2,3}, {4,5,6}}; Function header needs the second number of elements: – void arrayfunc (int b[][3], size_t arrSize)

19 Ex: Total multidimensional array Sum = 0 // loop through rows of grades for ( i = 0; i < pupils; ++i ) { // loop through columns of grades for ( j = 0; j < tests; ++j ) { sum = sum + grades[i][j]; } // end inner for } // end outer for

20 Comparison to Java FeatureCJava array declarations int a[5]int[] a = new int[N]; array size arrays don't know their own size, but you can call sizeof () to get their memory size and divide by the size of one element. Only works on local arrays. a.length

21 Summary Define an array single or multi subscript Access an array element Pass an array as a function – by reference Get an array size Special character array Static keyword for array


Download ppt "C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type."

Similar presentations


Ads by Google