CS110_04c_arrays - EECS110: 4c Arrays : Groups of Variables...

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: 4c Arrays : Groups of Variables Jack Tumblin jet@cs.northwestern.edu I hope you are: Reviewing the `midterm summary' sheet on Blackboard , as if you had a midterm exam next week... 1 Array: A fixed group of variables A group of statements with 1 name Function A group of variables with 1 name Array. EXAMPLE; for each student in this class, declare a variable to hold the heartbeat rate. With ordinary variables, must declare: float hrate0, hrate1,hrate2, hrate3,...; /* tedium!*/ With an array, you only need to declare: float hrate[37]; /* holds 37 different floats*/ 2 Arrays Variable== a named location in memory Variable for just one stored value Array == a named location in memory for MANY values, stored sequentially Think of an array as a sequence of boxes, each one holding a value element 0 element 1 element 2 element 3 element 4 ... 3 Arrays: Purpose Suppose... each box can hold one changeable value, Created exactly 5 boxes, and Named the set of boxes "nums". nums Element number selects the box. int nums[5] = {381, 4107,932 ,40, 212}; nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 4 Arrays: 1) Declare How? First, Declare the array, (just as we declare variables;) int Data type; (What the boxes hold) nums[5]; Array name Array size "# of boxes" or # of elements (All box contents are initially undefined) nums element 0 element 1 element 2 element 3 element 4 5 Arrays: 2) Initialize Next, assign values in the boxes, either like this... int nums[5] = {381, 4107, 932, 40, 212}; Curly Braces nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 6 Arrays: 2) Initialize Next, assign values in the boxes, ...or like this: (with assignment statements) Index int nums[5]; Selects box #, or `element' nums[0] = 381; nums[1] = 4107; nums[2] = 932; nums nums[3] = 2*2*10; nums[4] = 200 + 12; element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 7 Arrays: 3) Select by Index In C, the `index' (or `box number') of an array always starts at zero, and ends at (array_size-1) zero int nums[5]; nums[0] = 381; nums[1] = 4107; nums[2] = 932; nums nums[3] = 40; nums[4] = 212; element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 8 Arrays Array Declaration Syntax: element_type array_name[size]; size must be a constant expression: variables? Tricky! OK to set declared size, but you cannot change array size at run-time! (size is often set by a #define directive) nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 9 Array Indexing Chosen array element==ordinary variable name[index] /* choose 1 element */ Index == an integer expression, used as `box#' or `element#' Example: for(i=0; i<5; i++) printf("%d,", nums[i]); produces > 381,4107,932,40,212, nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 10 Array Indexing Chosen array element==ordinary variable name[index] /* choose 1 element */ Index == an integer expression, used as `box#' or `element#' Another Example: for(i=0; i<5; i++) printf("%d,", nums[(i+2)%5]); produces > 932,40,212,381,4107 nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 11 Array Indexing Chosen array element==ordinary variable name[index] !!DANGER!! C won't catch a wild/wrong index !!! reading/writing to nums[-3], or nums[398], can corrupt your program, your data or someone else's! nums element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 12 Array Example #include <stdio.h> int main (void) { int simple[4]; int i; for (i=0; i<4; i++) /*fill array with odd integers*/ { simple[i] = 1 + 2*i; printf("simple[%d] = %d\n", i, simple[i]); } return 0; } Output: simple[0] simple[1] simple[2] simple[3] = = = = 1 3 5 7 13 #include <stdio.h> Array Example int main (void) { const double percent[4] = {0.15,0.35,0.42,0.08}; int score[4], j, cnt; double grade; printf("Type scores: midterm, proj, final, quiz\n"); for(j=0; j<4; j++) { cnt = scanf(" %d",&score[j]); if(cnt!=1) // did we read in one integer? { // No! printf("\n Type 0-100 then RETURN, please!\n"); j = j-1; // (don't count this try) } } for(j=0,grade=0; j<3; j++) // Compute final grade { grade += (double)score[j] * percent[j]; } printf("\n Final Score is %g\n",grade); return 0; } 14 #include <stdio.h> Array Example int main (void) { const double percent[4] = {0.15,0.35,0.42,0.08}; int score[4], j, cnt; double grade; >Type scores: midterm, proj, final, quiz: > zbbr printf("Type scores: midterm, proj, final, quiz\n"); > Type 0-100 then RTN, please! for(j=0; j<4; j++) { > 89 cnt = scanf(" %d",&score[j]); 72 > if(cnt!=1) // did we read98 one integer? > in { // No! > 31 printf("\n Type 0-100 then RETURN, please!\n"); > Final score is j = j-1; // (don't count this try) } } for(j=0,grade=0; j<3; j++) // Compute final grade { grade += (double)score[j] * percent[j]; } printf("\n Final Score is %g\n",grade); return 0; } 15 ?Math on entire Array? 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] + 1); OK to compute with single array element, But NO whole-array expressions allowed! elvis=0; or jimi=elvis; (you MUST index the array to set its value) . 16 Summary: Arrays Variable== a place-holder in memory Variable for just one stored value Array == a place-holder in memory for MANY values, sequentially stored and indexed Array elements all of one type (e.g. char, float...), Array size is always constant, never variable, Access array element by its index # In C, index for array always begins at zero, zero C won't prevent an out-of-bounds index!!! 17 Summary: Arrays Before you can use an array, you must: 1) Declare array (set it's type, name, and size), and int elvis[5]; elvis[0] = 381; elvis[2] = 932; elvis[4] = 212; elvis Si*. <;r5 elvis[1] = 4107; elvis[3] = 40; Initially, array holds garbage! element 0 element 1 element 2 element 3 element 4 k79] as4$ p@#$ 18 Summary: Arrays Before you can use an array, you must: 2) Initialize all elements, either like this... elements (set all its values--otherwise it holds `garbage') int elvis[5]; elvis[0] = 381; elvis[2] = 932; elvis[4] = 212; elvis elvis[1] = 4107; elvis[3] = 40; element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 19 Summary: Arrays Before you can use an array, you must: 2) Initialize all elements, or like this: elements (set all its values--otherwise it holds `garbage') int elvis[5] = {381,4107,932,40,212}; elvis[0] = 381; elvis[2] = 932; elvis[4] = 212; elvis elvis[1] = 4107; elvis[3] = 40; element 0 element 1 element 2 element 3 element 4 381 4107 932 40 212 20 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis element 0 element 1 element 2 element 3 element 4 381 4107 0 40 212 21 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis[1] = (elvis[3] / 10)%6; elvis element 0 element 1 element 2 element 3 element 4 381 4 0 40 212 22 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis[1] = (elvis[3] / 10)%6; elvis[elvis[2]] = 5; elvis element 0 element 1 element 2 element 3 element 4 5 4 0 40 212 23 Summary: Arrays 3) A properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression, Pass its value to a function, such as printf() printf("elvis[%d]=%d",elvis[2], elvis[elvis[2]]); elvis element 0 element 1 element 2 element 3 element 4 5 4 0 40 212 24 What is an `array', really? What happens in memory when we `declare a variable' and when we `declare an array' ? How is it different? 25 (Recall) Bits, Bytes and Memory BIT == BInary digiT == a 2-way choice (1/0, yes/no, BI true/false) == the fundamental unit of information Byte = 8 bits grouped together: 28 = 256 choices A byte is atomic for most* computers: the smallest `bite' of info they can retrieve and process. *exceptions: very small and/or special purpose computers Memory is a long list of stored bytes. Sequentially numbered; address. 26 (Recall) Bits, Bytes and Memory ... 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 ... stored bytes Address Address Memory Memory holds the instructions and variables of your program (& much much more...) 27 Declarations and Memory To `declare' a variable is to ==`reserve a named chunk of memory,' or ==(Jargon) `allocate memory for this variable' Address Address int my_int = 5; char ch = `a'; ... 8919 8920 8921 8922 8923 ... +0 00 00 05 a my_int ch 28 Declarations and Memory any variable How much memory gets used? Use sizeof() function to find out: or expression byte_count = sizeof(my_var); For Windows98, NT4, 2000, ME, XP,... memory usage in bytes char int float double == 1 byte, == 4 bytes, == 4 bytes, == 8 bytes. 29 Arrays and Memory usr ... 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 ... int usr[3]; usr[0] Sequential in memory usr[1] Address Address Amount of Memory used: array_size*sizeof(type) usr[2] Addressing is easy: (Address_of_element_0) + (i * sizeof(type)) 30 ...
View Full Document

Ask a homework question - tutors are online