cs31day11 Wk 5 W

cs31day11 Wk 5 W - ARRAYS CONTINUED year, month, day bool...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
ARRAYS CONTINUED year, month, day bool isValidDate(int y, int m, int d) { int daysInMonth[12] = { 31, 28, 31, 30, 31, 30 // every element must be the same type 31, 31, 30, 31, 30, 31 }; if (d < 1 || d > daysnMonth[m-1]) // because start couting at 0 return false; ... } make sure you dont refer to an element that is past the range of the array. make sure you dont base anything on undefined double PI = 3.141592 what if part of your program changes this value and it messes up your program: const double PI = 3.141592 this will make this value constant since daysInMonth will not change, we can protect our values by using const: bool . ...... { const int daysInMonth[12]. ... ....................... } ________________________________________________________________ ____________________________ using STRINGS in an array string monthName[12] = { "January", "February", "March", . .. }; cout << monthName[m-1]; // assuming m is 1-12 ________________________________________________________________ ____________________________ if we DIDNT have array
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch (m) { case 1: cout << "January"; break; case 2: . .. ... } ________________________________________________________________ ____________________________ what if we wanted to look for a certain item and CHANGE it const int NMONTHS = 12; const string monthName[NMONTHS] = { 31, 28,. ........ ................ }; const string monthName[NMONTHS] = { "January",. ................ }; so if we wanted to change the constant 12 to a different number, we can easily change all these values by using NMONTHS. (if we add more items to the array) ________________________________________________________________ _______________________________ what if we want to search out all the items with a common factor that we want cout << "These months have 31 days:" << endl; for (int k = 0; k < NMONTHS; k++) { if (daysInMonth[k] == 31) // get out when k = 12 because that's out of range cout << monthName[k] << endl;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

cs31day11 Wk 5 W - ARRAYS CONTINUED year, month, day bool...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online