Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engr 0012 (04-1) LecNotes 22-01. Engr 0012 (04-1) LecNotes 22-02 arrays collection of values - all of the same type, e.g., int individual values referred.

Similar presentations


Presentation on theme: "Engr 0012 (04-1) LecNotes 22-01. Engr 0012 (04-1) LecNotes 22-02 arrays collection of values - all of the same type, e.g., int individual values referred."— Presentation transcript:

1 Engr 0012 (04-1) LecNotes 22-01

2 Engr 0012 (04-1) LecNotes 22-02 arrays collection of values - all of the same type, e.g., int individual values referred to as an element of the array example: 1 4 14 -3 2 8 -2 0 3 an array of type int an individual element is 14 example: This is an array. an array of type char an individual element is h

3 Engr 0012 (04-1) LecNotes 22-03 arrays - declaring // defined constants #define MAXARRAYSIZE 100 … main() { // begin main // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths char filename[81]; // string array int lengthused, // actual amt in use i; // loop index … use defined constant to specify maximum number of elements in the array type of values meaningful name maximum number of elements in [ ] arrays do not need to be “full” - need to keep track of how much is actually used

4 Engr 0012 (04-1) LecNotes 22-04 arrays - using each “element” of an array must be accessed individually there are no commands that process an entire array!!! contrast with MATLAB area = length.*width single statement can find “area” for set of lengths and widths C language for (i=0; i<lengthused; i=i+1) { // begin area calc area[i] = length[i]*width[i]; } // end area calc must process element by element

5 Engr 0012 (04-1) LecNotes 22-05 arrays - using for (i=0; i<lengthused; i=i+1) { // begin area calc area[i] = length[i]*width[i]; } // end area calc area[3] refers to the 4th element of area area[7] refers to the 8th element of area area[i] refers to the (i+1)th element of area array indices always start at 0 (zero) contrast with MATLAB where indices start at 1 (one) each element of an array is accessed through its index

6 Engr 0012 (04-1) LecNotes 22-06 arrays - using // defined constants #define MAXLENGTH 10 main() { // begin main // variable declaration double length[MAXLENGTH], // length array width[MAXLENGTH], // width array area[MAXLENGTH], // area array perim[MAXLENGTH]; // perimeter array int lengthused, // actual num of lengths i; // loop index maximum size of array is different than size actually in use ==> You, the programmer, must keep track of amount in use and pass the amount in use as a parameter to functions.

7 Engr 0012 (04-1) LecNotes 22-07 arrays - initialization // defined constants #define MAXSIZE 5 main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i; // loop index // algorithm printf( "\na[0] = %f, a[1] = %f, a[2] = %f" "\n\nb[0] = %f, b[1] = %f, b[2] = %f", a[0], a[1], a[2], b[0], b[1], b[2] ); hardwire - can specify size and some elements can specify all elements - let C determine size a[0] = 8.000000, a[1] = 9.000000, a[2] = 0.000000 b[0] = 4.000000, b[1] = 5.000000, b[2] = 6.000000

8 Engr 0012 (04-1) LecNotes 22-08 arrays - initialization // defined constants #define MAXSIZE 5 main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i; // loop index // algorithm for (i=0; i<3; i=i+1) { // begin for c[i] = 2*i; } // end for printf( "\n\nc[0] = %f, c[1] = %f, c[2] = %f" "\nc[3] = %f \nc[4] = %f", c[0], c[1], c[2], c[3], c[4] ); can initialize using assignment statements (with or without loop) c[0] = 0.000000, c[1] = 2.000000, c[2] = 4.000000 c[3] = 0.000000 c[4] = 2353689493169807300000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000

9 Engr 0012 (04-1) LecNotes 22-09 arrays - initialization // defined constants #define MAXSIZE 5 // prototypes void get_d_from_file( double d[], int *pdused ); main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i, dused; // loop index // algorithm get_d_from_file( d, &dused ); can initialize by reading a file with values warning: need to keep track of how many actual values were read

10 Engr 0012 (04-1) LecNotes 22-10 arrays - use simple assignment statements newlength = length[12]; assigns 13th element of length to newlength length[7] = 3*width[7]; assigns 3 times the 8th element of width to the 8th element of length

11 Engr 0012 (04-1) LecNotes 22-11 arrays - use avelength = 0.0; for( i = 0; i < actsize; i = i+1 ) { // begin for loop avelength = avelength + length[i]; } // end for loop avelength = avelength/actsize; computes the average of an array process only elements in use - not whole (MAXSIZE) array uses variable(index) i to access each element of array

12 Engr 0012 (04-1) LecNotes 22-12 arrays - use for( j = 0; j < actsize; j = j+1 ) { // begin for loop length[j] = 3*j+12; } // end for loop assigns a value to each element of array length can use variable (index) names other than i

13 Engr 0012 (04-1) LecNotes 22-13 arrays - use nonsense = length[3*intvalue%2]; index needs to be an integer value how many ways can we produce an integer value?

14 Engr 0012 (04-1) LecNotes 22-14 arrays - function prototypes/parameters // preprocessor commands #define MAXARRAYSIZE 100 … main() { // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths int actualsize, // actual amt in use i; // loop index … // function prototypes void getarray( double length[ ], int *pactsize ); double avearray( double length[ ], int actsize ); used to return number of values actually in use [ ] tell that the variable is an array need to send amount actually in use

15 Engr 0012 (04-1) LecNotes 22-15 arrays - function prototypes/parameters // preprocessor commands #define MAXARRAYSIZE 100 // function prototypes void getarray( double length[ ], int *pactsize ); double avearray( double length[ ], int actsize ); … main() { // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths int actualsize, // actual amt in use i; // loop index // calling functions getarray( length, &actualsize ); ave = avearray( length, actualsize ) name w/o [ ] meaning, not name, is important for value transferred

16 Engr 0012 (04-1) LecNotes 22-16 arrays - addresses &length[k] & used to denote address of single element printf( "\nEnter next length ==> " ); scanf( "%lf", &length[j] ); \\ prototypes double prod( double *plen, double *pwid ); … \\ algorithm area = prod( &length[i], &width[i] );

17 Engr 0012 (04-1) LecNotes 22-17 arrays - addresses name of array alone (without subscript) is the same as the address of its first element length &length[0]


Download ppt "Engr 0012 (04-1) LecNotes 22-01. Engr 0012 (04-1) LecNotes 22-02 arrays collection of values - all of the same type, e.g., int individual values referred."

Similar presentations


Ads by Google