Multi-Dimension Arrays

Slides:



Advertisements
Similar presentations
1 Techniques of Programming CSCI 131 Lecture 24 Structs.
Advertisements

1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
1 Lecture 22 Chapter 12 Arrays Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
1 Chapter 12-3 Arrays Dale/Weems. 2 Specification of Time class Time // “Time.h” { public : // 7 function members void Set (int hours, int minutes, int.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12 Arrays. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
NICS Index State Participation As of 12/31/2007 DC NE NY WI IN NH MD CA NV IL OR TN PA CT ID MT WY ND SD NM KS TX AR OK MN OH WV MSAL KY SC MO ME MA DE.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
Agencies’ Participation in PBMS January 20, 2015 PA IL TX AZ CA Trained, Partial Data Entry (17) Required Characteristics & 75% of Key Indicators (8) OH.
Collier County Tourism Research
MD VT MA NH DC CT NJ RI DE WA
Collier County Tourism Research
Chapter 11 Arrays.
Uninsured Non-Elderly Adult Rate Increased from 17. 8% to 20
Medicaid Eligibility for Working Parents by Income, January 2013
House Price
House price index for AK
Dale/Weems/Headington
WY WI WV WA VA VT UT TX TN SD SC RI PA OR* OK OH ND NC NY NM* NJ NH
Children's Eligibility for Medicaid/CHIP by Income, January 2013
NJ WY WI WV WA VA VT UT TX TN SD SC RI PA OR OK OH ND NC NY NM NH NV
Two Dimensional Arrays
Collier County Tourism Research
Engineering Problem Solving with C++, Etter/Ingber
Medicaid Costs are Shared by the States and the Federal Government
Expansion states with Republican governors outnumber expansion states with Democratic governors, May 2018 WY WI WV◊ WA VA^ VT UT TX TN SD SC RI PA OR OK.
Expansion states with Republican governors outnumber expansion states with Democratic governors, January WY WI WV◊ WA VA VT UT TX TN SD SC RI PA.
Share of Births Covered by Medicaid, 2006
Non-Citizen Population, by State, 2011
Share of Women Ages 18 – 64 Who Are Uninsured, by State,
Coverage of Low-Income Adults by Scope of Coverage, January 2013
Executive Activity on the Medicaid Expansion Decision, May 9, 2013
Mobility Update and Discussion as of March 25, 2008
Current Status of the Medicaid Expansion Decision, as of May 30, 2013
Chapter 11 Arrays.
IAH CONVERSION: ELIGIBLE BENEFICIARIES BY STATE
WAHBE Brokers / QHPs across the country as of
619 Involvement in State SSIPs
State Health Insurance Marketplace Types, 2015
State Health Insurance Marketplace Types, 2018
HHGM CASE WEIGHTS Early/Late Mix (Weighted Average)
Percent of Women Ages 19 to 64 Uninsured by State,
Sampling Distribution of a Sample Mean
Medicaid Income Eligibility Levels for Parents, January 2017
State Health Insurance Marketplace Types, 2017
S Co-Sponsors by State – May 23, 2014
Seventeen States Had Higher Uninsured Rates Than the National Average in 2013; Of Those, 11 Have Yet to Expand Eligibility for Medicaid AK NH WA VT ME.
Employer Premiums as Percentage of Median Household Income for Under-65 Population, 2003 and percent of under-65 population live where premiums.
Employer Premiums as Percentage of Median Household Income for Under-65 Population, 2003 and percent of under-65 population live where premiums.
Average annual growth rate
Sampling Distribution of a Sample Mean
Percent of Children Ages 0–17 Uninsured by State
Executive Activity on the Medicaid Expansion Decision, May 9, 2013
How State Policies Limiting Abortion Coverage Changed Over Time
United States: age distribution family households and family size
Status of State Medicaid Expansion Decisions
Employer Premiums as Percentage of Median Household Income for Under-65 Population, 2003 and percent of under-65 population live where premiums.
Percent of Adults Ages 18–64 Uninsured by State
States including quality standards in their SSIP improvement strategies for Part C FFY 2013 ( ) States including quality standards in their SSIP.
Status of State Medicaid Expansion Decisions
States including their fiscal systems in their SSIP improvement strategies for Part C FFY 2013 ( ) States including their fiscal systems in their.
Current Status of State Individual Marketplace and Medicaid Expansion Decisions, as of September 30, 2013 WY WI WV WA VA VT UT TX TN SD SC RI PA OR OK.
Status of State Medicaid Expansion Decisions
Income Eligibility Levels for Children in Medicaid/CHIP, January 2017
WY WI WV WA VA VT UT TX TN SD SC RI PA OR OK OH ND NC NY NM NJ NH NV
Presentation transcript:

Multi-Dimension Arrays Lecture 8 Multi-Dimension Arrays Richard Gesick

Two-Dimensional Array A two-dimensional array is a collection of components, all of the same type, structured in two dimensions, (referred to as rows and columns) Individual components are accessed by a pair of indexes representing the component’s position in each dimension DataType ArrayName[ConstIntExpr][ConstIntExpr]...;

Declaration and Initialization The declaration of a two-dimensional array requires a row size and a column size. A consecutive block of (row size)*(column size) memory locations are allocated. All array elements must be of the same type. Elements accessed by two offsets – a row offset and a column offset. The name of the array holds the address of the first byte of memory

Example //Declaration int data[2][3]; Memory Snapshot ? data

Example int data[2][3]; //Declaration ? row/column form: col 0 col 1

2D Array Definition Syntax data_type identifier[ [row_size] ][column_size] [= initialization_list ]; //row_size and column_size must be integer constants Examples int data[2][5]; //allocates consecutive memory for 10 integer values double t[2][2] = {{3.0,5.0},{2.1,7.2}}; //allocates and initializes Valid References cout << data[1][3]; cout << t[1][0]; Invalid References cout << data[2][5]; //invalid offset. cout << t[-1][-1]; //invalid offset

Initialization Examples int temp[4][3] = {50, 70, 60, 48, 75, 62, 51, 69, 60, 52, 78, 63}; int temp[4][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int t2[7][4] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int temp[][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int temp[][3] = {50, 70, 60, 48, 75, 62, 51, 69, 60, 52, 78, 63};

Example: Input Using cin Nested for loops are often used when inputting and assigning values to a two-dimensional array. Nested loops are generally useful for getting around the 2D arrays… //Declaration double table[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col cin >> table[i][j];

Example: Assignment //Declaration const int RSIZE(3),CSIZE(2); double v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j; V 1 2 3

Example: Computations Compute the average value of an array with n rows and m columns. double sum(0), average; for (int i=0; i<n; ++i) //every row for (int j=0; j<m; ++j )//every col sum += array[i][j]; average = sum / (n*m);

Example: Computations Compute the average value of the nth row of a 2D array with r rows and c columns. double sum(0), rowAverage; for (int j=0; j<c; ++j ) //every col sum += array[n][j]; average = sum / c;

Modify! Modify the C++ statements on the previous slide to compute the average of the mth column.

Outputting 2D Arrays Two dimensional arrays are often printed in a row by row format, using nested for statements. When printing the row values of an array, be sure to print: whitespace between the values in a row. a newline character at the end of each row.

Example: Printing for (int i=0; i<n; ++i) {//every row for (int j=0; j<m; ++j )//every col cout << array[i][j] << ‘ ‘; cout << endl; //add end-of-line each row }

2D Arrays as Function Parameters 2-D arrays are always passed by reference. The column dimension must be specified. The leftmost dimension (row) may be empty []. Function prototype example: int rowAverage(int Arr[][COLSIZE], int whichRow); Array declaration in main: int table [ROWSIZE][COLSIZE]; Function invocation example: avg = rowAverage(table, 3);

EXAMPLE -- Array for monthly high temperatures for all 50 states const int NUM_STATES = 50; const int NUM_MONTHS = 12; int stateHighs[NUM_STATES][NUM_MONTHS]; [0] [1] [2] . . stateHighs[2][7] [48] [49] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9][10][11] row 2, col 7 might be Arizona’s high for August 66 64 72 78 85 90 99 105 98 90 88 80

[1] [2] . . stateHighs[2][AUG] [48] [49] enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; const int NUM_MONTHS = 12; const int NUM_STATES = 50; int stateHighs[NUM_STATES][NUM_MONTHS]; [0] [1] [2] . . stateHighs[2][AUG] [48] [49] [JAN] . . . [AUG] . . [DEC] 66 64 72 78 85 90 99 105 98 90 88 80 row 2, col AUG could be Arizona’s high for August

Array for Monthly High Temperatures for all 50 states, cont... enum State { AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY }; enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; const int NUM_MONTHS = 12; const int NUM_STATES = 50; int stateHighs[NUM_STATES][NUM_MONTHS];

row AZ, col. AUG holds stateHighs[AZ][AUG] Arizona’s high for August [AL] [AK] [AZ] . [WI] [WY] row AZ, col. AUG holds stateHighs[AZ][AUG] Arizona’s high for August [JAN] . . . [AUG] . . [DEC] 66 64 72 78 85 90 99 105 98 90 88 80

Finding the Average High Temperature for Arizona int total = 0; int month; // Without enum types int average; for (month = 0; month < NUM_MONTHS; month ++) total = total + stateHighs[2][month]; average = int (total / 12.0 + 0.5); 85 average

Finding the Average High Temperature for Arizona, cont... int total = 0; Month month; // With enum types defined int average; for (month = JAN; month <= DEC; month = Month(month+1)) total = total + stateHighs[AZ][month]; average = int (total / 12.0 + 0.5); 85 average

const int NUM_STATES = 50; const int NUM_MONTHS = 12; int stateHighs[NUM_STATES][NUM_MONTHS]; 12 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row 8000 8024 8048 STORAGE rows columns In memory, C++ stores arrays in row order; the first row is followed by the second row, etc. Base Address . . .

Viewed another way . . . To locate an element such as stateHighs[2][7] Base Address 8000 To locate an element such as stateHighs[2][7] the compiler needs to know that there are 12 columns in this two-dimensional array. At what address will stateHighs[2][7] be found? Assume 2 bytes for type int.

Arrays as Parameters As with a one-dimensional array, when a two- (or higher) dimensional array is passed as an argument, the base address of the caller’s array is sent to the function The size of all dimensions except the first must be included in the function heading & prototype The sizes of those dimensions in the function’s parameter list must be exactly the same as those declared for the caller’s array

Write a function using the two-dimensional stateHighs array to fill a one-dimensional stateAverages array const int NUM_STATES = 50; const int NUM_MONTHS = 12; int stateHighs[NUM_STATES][NUM_MONTHS]; int stateAverages[NUM_STATES]; [0] 62 [1] 85 [2] . [48] [49] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9][10][11] Alaska Arizona 43 42 50 55 60 78 80 85 81 72 63 40 66 64 72 78 85 90 99 105 98 90 88 80

void FindAverages( /. in. / const int stateHighs[][NUM_MONTHS], /. out void FindAverages( /* in */ const int stateHighs[][NUM_MONTHS], /* out */ int stateAverages[]) //PRE:stateHighs[0..NUM_STATES][0..NUM_MONTHS] // assigned // POST:stateAverages[0..NUM_STATES] contains // rounded high temperature for each state

{ int state; int month; int total; for (state = 0; state < NUM_STATES; state++) total = 0; for (month = 0; month < NUM_MONTHS; month++) total += stateHighs[state][month]; stateAverages[state] = int(total/12.0 + 0.5); }

Using typedef with Arrays The typedef statement helps eliminate the chances of size mismatches between function arguments and parameters. FOR EXAMPLE, typedef int StateHighs [NUM_STATES][NUM_MONTHS]; typedef int StateAverages [NUM_STATES]; void FindAverages( /* in */ const StateHighs stateHighs, /* out */ StateAverages stateAverages) { }

Declaring Multidimensional Arrays Example of three-dimensional array const NUM_DEPTS = 5; // mens, womens, childrens, electronics, furniture const NUM_MONTHS = 12; const NUM_STORES = 3; // White Marsh, Owings Mills, Towson int monthlySales[NUM_DEPTS][NUM_MONTHS][NUM_STORES]; rows columns sheets OR USING TYPEDEF typedef int MonthlySales [NUM_DEPTS][NUM_MONTHS][NUM_STORES]; MonthlySales monthlySales;

sales for electronics in August at White Marsh const NUM_DEPTS = 5; // mens, womens, childrens, electronics, furniture const NUM_MONTHS = 12; const NUM_STORES = 3; // White Marsh, Owings Mills, Towson int monthlySales[NUM_DEPTS][NUM_MONTHS][NUM_STORES]; monthlySales[3][7][0] sales for electronics in August at White Marsh 3 STORES sheets 5 DEPTS rows 12 MONTHS columns

Print Sales for Dec. by Department COMBINED SALES FOR December DEPT # DEPT NAME SALES $ 0 Mens 12345 1 Womens 13200 2 Childrens 11176 3 Electronics 22567 4 Furniture 11230

Print sales for Jan. by department COMBINED SALES FOR January DEPT # DEPT NAME SALES $ 0 Mens 8345 1 Womens 9298 2 Childrens 7645 3 Electronics 14567 4 Furniture 21016

// mens, womens, childrens, electronics, // furniture const NUM_DEPTS = 5; const NUM_MONTHS = 12; // White Marsh, Owings Mills, Towson const NUM_STORES = 3; int monthlySales[NUM_DEPTS][NUM_MONTHS][NUM_STORES]; . . . .

for (month = 0; month < NUM_MONTHS; month++) { cout << “COMBINED SALES FOR ” ; // Function call to write the name of month WriteOut(month); cout << “DEPT # DEPT NAME SALES $” << endl;

for (dept = 0; dept < NUM_DEPTS; dept++) { totalSales = 0; for (store = 0; store < NUM_STORES; store++) totalSales = totalSales + monthlySales[dept][month][store]; WriteDeptNameAndSales(dept, totalSales); }

Adding a Fourth Dimension . . . const NUM_DEPT = 5; // mens, womens, childrens … const NUM_MONTHS = 12; const NUM_STORES = 3; // White Marsh, Owings Mills, Towson const NUM_YEARS = 2; int moreSales[NUM_DEPTS][NUM_MONTHS][NUM_STORES][NUM_YEARS]; year 0 year 1 moreSales[3][7][0][1] for electronics, August, White Marsh, one year after starting year