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 arrays collection of values - all of the same type, e.g., int individual values referred to as an element of the array example: 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 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 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

5 Engr 0012 (04-1) LecNotes arrays - using for (i=0; i

6 Engr 0012 (04-1) LecNotes 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 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] = , a[1] = , a[2] = b[0] = , b[1] = , b[2] =

8 Engr 0012 (04-1) LecNotes 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] = , c[1] = , c[2] = c[3] = c[4] =

9 Engr 0012 (04-1) LecNotes 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 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 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 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 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 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 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 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 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