110 likes | 205 Views
Engr 0012 (04-1) LecNotes 25-01. 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 - review.
E N D
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 - review Declaring an 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 … Engr 0012 (04-1) LecNotes 25-02
&length[0] &length[MAXARRAYSIZE-1] … … &length[actualsize-1] declaration sets aside MAXARRAYSIZE memory locations capable of holding specified type of information compiler remembers only location (address of) first location &length[0] length do not need to use entire allocation of memory ==> need to keep track of actualsize Arrays Memory management Engr 0012 (04-1) LecNotes 25-03
&length[0] &length[MAXARRAYSIZE-1] … … &length[actualsize-1] length[0] length[8] length[18] length[-2] length[260] Arrays Using arrays accessing values - name and index Engr 0012 (04-1) LecNotes 25-04
N rows M columns Two dimensional arrays - tables/matrices 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 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
use defined constants to declare dimensions if square matrix, only need one size #define MAX2D 10 specify rows and columns double length[MAX2D][MAX2D]; // table of lengths keep track of amount in use Two dimensional arrays - declaring // 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 … Engr 0012 (04-1) LecNotes 25-06
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 Two dimensional arrays - using Engr 0012 (04-1) LecNotes 25-07
[ ] tells that the variable is an array need to send amount actually in use used to return number of values actually in use 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 … // function prototypes int getarray( double length[ ] ); double avearray( double length[ ], int actsize ); Engr 0012 (04-1) LecNotes 25-08
name w/o [ ] 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 // calling statement actualsize = getarray( length ); ave = avearray( length, actualsize ) ==> sending address of first element Engr 0012 (04-1) LecNotes 25-09
need to send amount in use need to specify maximum number of columns 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 … // function prototypes void fill( double onedarray[], int used1D, double twodarray[][MAX2D], int used2D ); Engr 0012 (04-1) LecNotes 25-10
local array name w/o [] ==> sending address 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 ); Engr 0012 (04-1) LecNotes 25-11