const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int month, day, year; int day_of_year; bool leapYear(int testYear) { if (testYear % 400 == 0 || (testYear % 100 != 0 && testYear %4 == 0)) { return true; } else { return false; } } int dayOfYear() { int numberOfDays; if (month == 1) { day_of_year = day; } else if (month > 2 && leapYear(year)) { numberOfDays = day + 1; for (int i = 1; i < month; i++) { numberOfDays += days[i]; } day_of_year = number_of_days; } else { numberOfDays = day; for (int i = 1; i < month; i++) { numberOfDays += days[i]; } day_of_year = number_of_days; } return day_of_year; }
const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int month, day, year; int day_of_year; bool leapYear(int testYear) { return testYear % 400 == 0 || testYear % 100 != 0 && testYear %4 == 0; } int dayOfYear() { int n = day + (month > 2 && leapYear(year) ? 1 : 0); for (int i = 1; i < month; ++i) { n += days[i]; } return n; }
The function will run faster without a loop.
const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //How many days are in the year ahead of the given month? const int pre[] = { 0, 0, //January pre[ 0] + 31, //February pre[ 1] + 28, //March pre[ 2] + 31, //April pre[ 3] + 30, //May pre[ 4] + 31, //June pre[ 5] + 30, //July pre[ 6] + 31, //August pre[ 7] + 31, //September pre[ 8] + 30, //October pre[ 9] + 31, //November pre[10] + 30 //December }; int month, day, year; int day_of_year; bool leapYear(int testYear) { return testYear % 400 == 0 || testYear % 100 != 0 && testYear %4 == 0; } int dayOfYear() { const int n = day + (month > 2 && leapYear(year) ? 1 : 0); return n + pre[month]; }
There’s no reason to keep the variable
number
alive before and after the
for
loop.
const int NO_OF_EMP = 3; int vacation[NO_OF_EMP], number; for (number = 1; number <= NO_OF_EMP; ++number) cin >> vacation[number - 1];
const size_t NO_OF_EMP = 3; int vacation[NO_OF_EMP]; for (size_t number = 0; number < NO_OF_EMP; ++number) { cin >> vacation[number]; }
int numArr[5]; float total = 0; cout << "Enter 5 numbers and receive their average." << "Enter each value one at a time: " << "\n"; for(int i = 0; i <= 4; i++){ cout << "Enter number " << i << ": "; cin >> numArr[i]; cout << "\n"; } for (int i = 0; i <= 4; i++) total += numArr[i]; cout << "The average was: " << total / 5.0 << "\n";
Write the number 5 only once.
Don’t output whitespace immediately before a newline.
Do as much of the arithmetic as possible with
int
s.
const size_t n = 5; cout << "Enter " << n << " numbers and receive their average. " << "Enter each value one at a time:\n"; int numArr[n]; int total = 0; for (size_t i = 0; i < n; ++i) { cout << "Enter number " << i << ": "; cin >> numArr[i]; cout << "\n"; total += numArr[i]; } cout << "The average was: " << static_cast<double>(total) / n << "\n";
const size_t n = 5; cout << "Enter " << n << " numbers and receive their average. " << "Enter each value one at a time:\n"; int total = 0; for (size_t i = 0; i < n; ++i) { cout << "Enter number " << i << ": "; int x; cin >> x; cout << "\n"; total += x; } cout << "The average was: " << static_cast<double>(total) / n << "\n";
The variables
year
and
distance_year
are
int
s.
The data type of the expressions
year + distance_year - 1
and
year + distance_year + 1
is also
int
.
The values of the expressions are therefore ≥
INT_MIN
and ≤
INT_MAX
.
The
if
is always true.
if (year+distance - 1 < INT_MAX && year+distance_year + 1 > INT_MIN) {
The variables
year
and
years
are
int
s.
The data type of the expression
year + years
is also
int
.
The value of the expression is therefore ≤
INT_MAX
.
The
if
is always false.
if (static_cast<long>(year + years) >= INT_MAX) {
How can we tell if it’s safe to add an integer
n
to
years
?
if (n > 0) { if (years <= INT_MAX - n) { years += n; } else { cerr << "Adding " << n << " to " << years << "would result in a sum greater than " << INT_MAX << ".\n"; } } else if (n < 0) { if (years >= INT_MIN - n) { years += n; } else { cerr << "Adding " << n << " to " << years << "would result in a sum less than " << INT_MIN << ".\n"; } }