CS110_05a_array2funcs - EECS110: 5a Arrays Passed To...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 5a Arrays Passed To Functions Jack Tumblin jet@cs.northwestern.edu I hope you: Would be ready for a Midterm Exam (if we had one) Don't care that Drop Day is coming soon... 1 (Recall) Arrays Array== a fixed-size set of matched variables, indexed (all are the same data type) 1) Declare an array by specifying its name, size and element's data type: type int scores [36]; (good idea: use #define to set array size) 2) Set initial array values like this: char monogram[3] = {'J', 'E', 'T'}; or like this: monogram[0] = `A'; monogram[1] = `C'; monogram[2] = `T'; 2 (Recall) Arrays Use brackets [index] to access an array element, and use it like any other ordinary variable scores[5] = 90 + scores[2]; scanf("%d", &scores[i]); C uses zero-indexing: first element index is 0 last element index is SIZE-1 DANGER! C will accept ANY index!!! 3 (Recall) Math on Entire Arrays? NO! Each element of an array acts just like an ordinary variable: int elvis[5], jimi[5]; for(j=0; j<5; j++) { elvis[j] = j*10; } elvis[3] = 8; printf(" %d\n", elvis[2]); NO whole-array expressions allowed: elvis=0; or jimi=elvis; 4 you MUST index the array to set its values! Function Calling Details Actual arguments == The values used during function calling (a.k.a. actual parameter) Formal parameters == The function's local variables in the argument list (these vars. are declared in the function prototype). actual arguments int main(void) { add(3,4); return 0; } formal parameters int add (int x, int y) { return (x+y); } 5 Function Calling Details At the start of a function call, The actual arguments are copied to the formal parameters (an assignment) IMPORTANT! Know these two terms well! IMPORTANT formal parameters actual arguments int main(void) { add(3,4); return 0; } int add (int x, int y) { return (x+y); } 6 (Recall) Arrays and Memory usr ... 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 ... Example array declaration. usr[0] int usr[3]; Address Address usr[1] Sequential in memory.. How much memory? sizeof(usr) , or array_size *sizeof(int) usr[2] 7 (Recall) Arrays and Memory usr ... 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 ... int usr[3]; usr[0] Address of usr[i] ? (address_of_element_0) + (i * sizeof(int)) . Address Address usr[1] usr[2] But C hides addresses! (today we learn that an array name gives us (address_of_element_0)) 8 Arrays and Functions I Each element of an array acts just like an ordinary variable. variable Of course, you can pass a single array element to a function as one of its arguments: int square(int x); int main(void) { int befor[10], after[10]; int i; int square(int x) { return x x; } for (i=0; i<10; i++) { befor[i] = i*5; after[i] = square(befor[i]); printf("%d squared is %d\n", befor[i], after[i]); } return 0; } 9 Arrays and Functions II Surprise! We can pass the entire array as an argument to a function too! Step 1: Declare & define function using a very strange array parameter: void prntArray( int a, int siz); // prototype ... void prntArray( int a, int siz) { int i; for(i=0; i<siz; i++) { printf(" %d\n",a[i]); } // one int per line } 10 Note EMPTY brackets!! Arrays and Functions II Step 1: Declare, define function using a very strange array argument: Step 2: Call the function using the array name ONLY: ONLY #define BIG 5 void prntArray( int a, int siz); // strange prototype int main(void) { int src[BIG]; int i; for(i=0; i<BIG; i++) src[i] = i+10; prntArray(src,BIG); return 0; } void prntArray( int a, int siz) { int i; for(i=0; i<siz; i++) printf(" %d\n",a[i]); // one int per line } Array name ONLY --No brackets at all!-- 11 Arrays and Functions II: Example A void prntArray( int a, int siz); // strange prototype ... What happens here? int main(void) src acts as func. input { #define BIG 5 // set array size int src[BIG]; int i; Function body for(i=0; i<BIG; i++) void prntArray( int a, int siz) { { int i; src[i] = i+10; } for(i=0; i<siz; i++) prntArray(src,BIG); { } } printf("%d, ",a[i]); } printf("\n"); 12 Arrays and Functions II: Example A void prntArray( int a, int siz); // strange prototype ... int main(void) { What happens here? #define BIG 5 // set array src acts as func. input size int src[BIG]; >10, 11, 12, 13, 14, > int i; for(i=0; i<BIG; i++) void prntArray( int a,int siz) { { int i; src[i] = i+10; } for(i=0; i<siz; i++) prntArray(src,BIG); { } } printf("%d, ",a[i]); } printf("\n"); 13 Arrays and Functions II: Example B void cleanArray( int z, int siz); // strange prototype ... int main(void) What happens here? >0, 0, 0, 0, 0, { > int count[5] = {5,4,3,2,1}; cleanArray(count, 5); printArray(count, 5); return 0; } void cleanArray( int z, int siz) { int i; for(i=0; i<siz; i++) { z[i] = 0; } } 14 ? Why did change to z array ALSO change count array? Arrays and Functions II: Example B void cleanArray( int z, int siz); // strange prototype ... int main(void) What happens here? >0, 0, 0, 0, 0, { > int count[5] = {5,4,3,2,1}; cleanArray(count, 5); printArray(count, 5); return 0; } On return from cleanArray(), count's elements are all 0. No cleanArray()return value(!) Array name (e.g.count) in function argument acted as as both input AND output! output void cleanArray( int z, int siz) { THUS int i; for(i=0; i<siz; i++) { z[i] = 0; } } 15 Arrays and Functions II Look again at that function prototype: void prntArray( int a, int size); // prototype What does this mean?!? (Recall) Function declarations make local variables or `formal parameters' parameters valid only inside your function (Recall) Calling a function with arguments will assigns values (or `copy to') to to the formal parameters char myFunc(char ch); int main() { char strt = `J'; char end; end = myFunc(strt); printf("%c%c",strt,end); return 0; } char myFunc(char ch) { return (ch +1); } 16 Arrays and Functions II Look again at that function prototype: void prntArray( int a, int size); // prototype What does this mean?!? These are `formal parameters'--local vars in prntArray() parameters Thus , int size means prntArray() has a local integer variable named size And int a means prntArray() has an integer array variable named a When we call the function (e.g. prntArray(count,5);) What happens? 17 Arrays and Functions II Look again at that function prototype: void prntArray( int a, int size); // prototype What does this mean?!? These are `formal parameters'--local variables parameters Thus , int size means prntArray() has a local integer variable named size And int a means prntArray() has an integer array variable named a When we call the function (e.g. prntArray(count,5);) --we copy the 5 value into the size variable, and. --we copy count array ADDRESS, not array values,. ADDRESS as for use as the a array.... VERY STRANGE! 18 (Recall) Arrays and Memory a ... 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 ... count count[0] While prntArray() runs, its a array uses the same memory locations as the count array. Any changes to a are also changes to count THUS: `array names' describe addresses in C! 19 Address Address count[1] count[2] count[3] Arrays and Functions II Array names describe an address for the array; Array names used as arguments in function calls don't copy the array, but only the address of the array. Functions only get info on where to find the array (and not its size) Remember this: array name = = address of array's 0th byte. AMAZING: allows functions to change contents of an array received as an `input' argument DANGEROUS: confounds `input/output'. Array scope gets messy and tricky: BE CAREFUL! 20 Scope of Array-type Formal Param. void prntArray2( int a, int max); // prototype ... void prntArray2(int a, int max) { int i; for(i=0; i<max; i++) { printf(" %d\n",a[i]); } } the array named a is defined only within the function body // one int per line 21 Revisit an earlier Example void cleanArray( int a, int siz); // prototype ... int main(void) { int count[5] = {5,4,3,2,1}; cleanArray(count, 5); printArray(count, 5); return 0; } Can we add this line to main()? a[2] = 42; Why/Why not? void cleanArray{int a, int siz)(answer: NO: `a' array doesn't { // clear 1st `siz' elements int i; for (i=0; i<siz; i++) a[i]=0; } exist in main()) 22 Revisit an earlier Example void cleanArray( int a, int siz); // prototype ... int main(void) { int count[5] = {5,4,3,2,1}; cleanArray(count, 5); printArray(count, 5); return 0; } Array name count gives array's 0th byte location, Function call to `cleanArray() copies that address, define its own local array (formal parameter) named a , but a array starts at same memory location as count! 23 Exercise: Write a function that replaces every i-th element in an array of doubles with the sum of its i-th and (i-1)th element. Leave element 0 unchanged. 24 Multidimensional Arrays Simple, but not very useful--fixed size! (Pointers will allow variable size, etc.) Declare: just like you'd expect: #define MAX_PTS 500 #define MAX_DIM 2 double points[MAX_PTS][DIM]; // max # of x,y, coords // dimensions: 2 for x,y Initialize: just like you'd expect; like this,... for(i=0; i<MAX_PTS; i++) { points[i][0] = 0.0; points[i][1] = 0.0; } // set x coordinate // set y coordinate 25 Multidimensional Arrays Or initialize like this: (DIFFERENT!) #define MAX_PTS 3 #define MAX_DIM 2 // max # of x,y, coords // dimensions: 2 for x,y double points[MAX_PTS][DIM] = { {0.0, {0.0, {0.0, } 0.0}, 0.0}, 0.0} As you'd expect, each element acts like an ordinary variable: points[1][2] = 3.0; 26 Functions & Multidim. Arrays: Ugh. Step 1: Declare, define function in a DIFFERENT WAY: only the FIRST dimension can be unspecified Step 2: Call the function in the SAME WAY: using array name ONLY: #define MAX_PTS 5 #define MAX_DIM 2 void prntArray( double a[SIZE], int size); // prototype int main(void) { int shape[MAX_PTS][MAX_DIM]; int i; void prntArray(double a[MAX_DIM], int siz) { int i,j; for(i=0; i<siz; i++) { for(j=0; j<MAX_DIM; j++) printf(" %f\n",a[i][j]); } 27 for(i=0; i<SIZE; i++) } shape[i][0] = i+10; shape[I][1] = i+1; prntArray(shape,MAX_PTS); } return 0; } Book: Multidimensional Arrays Book explains all details in Chapters 8-7, 8-8; EXAMPLE: #define XMAX #define YMAX 640 480 // picture size int img[480][640][3]; // declare 3D array ... for(i=0; i<XMAX; i++) // clear screen { for(j=0; j<YMAX; j++) { img[i][j][0] = 0; img[i][j][1] = 0; img[i][j][2] = 0; } } 28 Book: Multidimensional Arrays Book explains all details in Chapters 8-7, 8-8; EXAMPLE: #define XMAX #define YMAX 640 480 // picture size int img[480][640][3]; // declare 3D array ... for(i=0; i<XMAX; i++) // clear screen { for(j=0; j<YMAX; j++) { Surprise! not very useful; img[i][j][0] = 0; --rigid, bug-prone, archaic. img[i][j][1] = 0; --Instead, use img[i][j][2] = 0; structures & pointers, } coming soon! } 29 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online