1 / 17

Engr 0012 (04-1) LecNotes 22-01

Engr 0012 (04-1) LecNotes 22-01. 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.

ayoka
Télécharger la présentation

Engr 0012 (04-1) LecNotes 22-01

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 Engr 0012 (04-1) LecNotes 22-02

  3. 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 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 … Engr 0012 (04-1) LecNotes 22-03

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

  5. each element of an array is accessed through its index 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) Engr 0012 (04-1) LecNotes 22-05

  6. maximum size of array is different than size actually in use 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 ==> You, the programmer, must keep track of amount in use and pass the amount in use as a parameter to functions. Engr 0012 (04-1) LecNotes 22-06

  7. hardwire - can specify size and some elements can specify all elements - let C determine size 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] ); a[0] = 8.000000, a[1] = 9.000000, a[2] = 0.000000 b[0] = 4.000000, b[1] = 5.000000, b[2] = 6.000000 Engr 0012 (04-1) LecNotes 22-07

  8. arrays - initialization can initialize using assignment statements (with or without loop) // 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] ); c[0] = 0.000000, c[1] = 2.000000, c[2] = 4.000000 c[3] = 0.000000 c[4] = 2353689493169807300000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 Engr 0012 (04-1) LecNotes 22-08

  9. can initialize by reading a file with values warning: need to keep track of how many actual values were read 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 ); Engr 0012 (04-1) LecNotes 22-09

  10. 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 arrays - use simple assignment statements Engr 0012 (04-1) LecNotes 22-10

  11. process only elements in use - not whole (MAXSIZE) array uses variable(index) i to access each element of array 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 Engr 0012 (04-1) LecNotes 22-11

  12. can use variable (index) names other than i 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 Engr 0012 (04-1) LecNotes 22-12

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

  14. used to return number of values actually in use [ ] tell that the variable is an array need to send amount actually in use 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 );   Engr 0012 (04-1) LecNotes 22-14

  15. meaning, not name, is important for value transferred name w/o [ ] 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 )  Engr 0012 (04-1) LecNotes 22-15

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

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

More Related