c static arrays pepper. what is an array? memory locations – same type – next to each other...

21
C Static Arrays Pepper

Upload: amber-davidson

Post on 12-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

C Static Arrays

Pepper

Page 2: 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

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])

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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 <stdio.h>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]); }}

Page 6: 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

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

Sum an Array

Page 7: 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

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

Page 8: 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

Bubble Sort

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

Page 9: 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

Bubble Sort

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

Page 10: 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

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

Bubble Sort

Page 11: 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

Tricky – use Array item as subscript • Count frequency of survey responses

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

Page 12: 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

Survey cont.

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

Page 13: 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

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 <stdio.h>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); }}

Page 14: 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

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])

Page 15: 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

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.

Page 16: 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

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)

Page 17: 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

Sample Array pass to function

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

Page 18: 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

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)

Page 19: 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

Ex: Total multidimensional array

Sum = 0// loop through rows of gradesfor ( 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

Page 20: 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

Comparison to Java

Feature C Java

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

Page 21: 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

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