Engr 0012 (04-1) LecNotes 25-01
Arrays - review Declaring an array use defined constant to specify maximum number of elements in the array // 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 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 Engr 0012 (04-1) LecNotes 25-02
Arrays Memory management compiler remembers only location (address of) first location &length[0] length &length[0] &length[MAXARRAYSIZE-1] … declaration sets aside MAXARRAYSIZE memory locations capable of holding specified type of information &length[actualsize-1] do not need to use entire allocation of memory ==> need to keep track of actualsize Engr 0012 (04-1) LecNotes 25-03
Arrays Using arrays … &length[0] &length[MAXARRAYSIZE-1] &length[actualsize-1] length[0] length[18] length[8] length[-2] length[260] accessing values - name and index Engr 0012 (04-1) LecNotes 25-04
Two dimensional arrays - tables/matrices N rows 1.200 3.412 5.321 21.872 -16.322 -23.333 16.098 0.987 9.765 2.908 -0.888 312.099 222.987 16.999 87.434 -8.909 54.669 322.419 -435.666 -87.554 0.988 16.234 -76.287 12.359 M columns Dimension: NxM (rows x columns) if N = M (i.e., dimension NxN) square matrix still require all values be of same type Engr 0012 (04-1) LecNotes 25-05
Two dimensional arrays - declaring use defined constants to declare dimensions // preprocessor commands #define MAXROW 10 #define MAXCOL 10 … main() { // variable declaration double length[MAXROW][MAXCOL]; // table of lengths int actrows, // actual rows in use actcol, // actual col in use i,j,k; // loop indices #define MAX2D 10 if square matrix, only need one size specify rows and columns double length[MAX2D][MAX2D]; // table of lengths keep track of amount in use Engr 0012 (04-1) LecNotes 25-06
Two dimensional arrays - using length[1][2] value in 2nd row, 3rd column &length[6][4] address of value in 7th row, 5th column &length[0][0] length address of first element Engr 0012 (04-1) LecNotes 25-07
[ ] tells that the variable is an array Arrays - function prototypes/parameter lists // 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 [ ] tells that the variable is an array // function prototypes int getarray( double length[ ] ); double avearray( double length[ ], int actsize ); used to return number of values actually in use need to send amount actually in use Engr 0012 (04-1) LecNotes 25-08
Arrays - calling statements/parameter lists // preprocessor commands #define MAXARRAYSIZE 100 // function prototypes int getarray( double length[ ] ); 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 name w/o [ ] // calling statement actualsize = getarray( length ); ave = avearray( length, actualsize ) ==> sending address of first element Engr 0012 (04-1) LecNotes 25-09
2D arrays - function prototypes/parameter lists // preprocessor commands #define MAX1D 100 #define MAX2D 100 … main() { // variable declaration double oned[MAX1D]; // 1D array double twod[MAX2D][MAX2D]; // 2D array int onedact, // 1D array in use actrows, // actual amt in use i,j,k; // loop indices need to send amount in use // function prototypes void fill( double onedarray[], int used1D, double twodarray[][MAX2D], int used2D ); need to specify maximum number of columns Engr 0012 (04-1) LecNotes 25-10
2D arrays - calling statements/parameter lists // preprocessor commands #define MAX1D 100 #define MAX2D 100 … main() { // variable declaration double oned[MAX1D]; // 1D array double twod[MAX2D][MAX2D]; // 2D array int onedact, // 1D array in use actrows, // actual amt in use i,j,k; // loop indices // function prototypes void fill( double onedarray[], int used1D, double twodarray[][MAX2D], int used2D ); // calling statement fill( oned, onedact, twod, actrows ); local array name w/o [] ==> sending address Engr 0012 (04-1) LecNotes 25-11