HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMajor

Is it Friday yet?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
yetfridaystackoverflow

Problem

This is the first program that I have written in my C++ saga that I actually think is useful. The description for this assignment is kinda long and mundane though:


Write a program that inputs a date (for example: July 4, 2008) and
outputs the day of the week that corresponds to that date. The
following algorithm is from Wikipedia. The implementation will
require several functions.



-
bool isLeapYear(int year);


This function should return true if year is a leap year and false if it is not. Here is the pseudo code to determine a leap year:

leap_year = (year divisible by 400) or (year divisible by 4 and not divisible by 100)



-
int getCenturyValue(int year);


This function should take the first two digits of the year (that is, the century), divide by \$ 4 \$, and save the remainder. Subtract the remainder from \$ 3 \$ and return this value multiplied by \$ 2 \$. For example, the year \$ 2008 \$ becomes \$ \dfrac{20}{4} = 5 \$ with a remainder of \$ 0 \$. \$ 3 - 0 = 3 \$. Return \$ 3 \cdot 2 = 6 \$.

-
int getYearValue(int year);


This functions computes a value based on the years since the beginning of the century. First, extract the last two digits of the year. For example, \$ 08 \$ is extracted for \$ 2008 \$. Next, factor in leap years. Divide the value from the previous step by \$ 4 \$ and discard the remainder. Add the two results together and return this value. For example, from \$ 2008 \$ we extract \$ 08 \$. Then \$ \dfrac{8}{4} = 2 \$ with a remainder of \$ 0 \$. Return \$ 2 + 8 = 10 \$.

-
int getMonthValue(int month, int year);


This function should return a value based on the table below and will require invoking the isLeapYear() function.


$$
\newcommand\T{\Rule{0pt}{1em}{.5em}}
\begin{array}{|c|c|}
\hline \textbf{Month} & \textbf{Return Value} \\\hline
\text{January} \T & 0 \left(6 \text{ if year is a leap year} \right) \\\hline

Solution

This is improper in a few ways:

std::string *weekDays = new std::string[7] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


Not only is this a global variable (no const), but it's being dynamically allocated without being deallocated with delete somewhere. Plus, it can be deallocated anywhere, and possibly more than once if you're not careful. Either way, since std::string already does its own memory management, just leave out new.

Assuming no C++11 (no access to std::array), you should end up with this:

const std::string weekDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


In C++, it's also best to leave the [] empty when initializing a C array. The compiler will determine the correct size by itself, so you don't need to worry about keeping it up-to-date yourself.

Code Snippets

std::string *weekDays = new std::string[7] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const std::string weekDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Context

StackExchange Code Review Q#67636, answer score: 34

Revisions (0)

No revisions yet.