English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hey I've got a school project in my Data Structures and Algorithms Class and I'm somewhat stuck at a problem I was wondering if anyone could give me a hint on coming up with an algorithm to solve my particular problem, I’ve fished the rest of the program but...

I'm assigned to write a day of the week calculator program...the books example is...have a set day of the week say Monday, if the user adds 4 to it the system will output Friday...if the date is Tuesday and the user adds 13 days, the day to be returned is Monday...

I've been thinking of different ways to do this, but all of them seem to fail...like my for loop...double for loop...and some other bad ideas.

Keep in mind I don’t need actual code...just some advice on how to solve the problem.

2006-09-21 15:18:54 · 3 answers · asked by D 4 in Computers & Internet Programming & Design

3 answers

I know you don't want code, but it is the easiest way to explain it.

In C++:

const char** Days = { "Monday", "Tuesday", ... };

const char* GetDay(int Offset)
{
int Today = g_Today; // g_Today : "global" day
int modOffset = Offset % 7;

int newDay = (Today + modOffset) % 7;
return Days[newDay];
}

2006-09-21 16:53:36 · answer #1 · answered by Anonymous · 0 1

Try to visualize the math itself. Date problems are cool and a crucial part of your education because as a norm programming languages only give you the bare minimum date/time functions that you will need to solve ALL problems.

For example, many languages don't have a function that tells you how many days in a month. How do to fix this?

Say you want to know how many days in January. Take any date in January, say January 1, 2007. Most programming languages do have a date add function, so add one month to January 1, 2007.

Now you have February 1, 2007.

Most programming languages have a date substract function, so take one day off from February 1, 2007.

Now you have January 31, 2007. Which means January is 31 days long.

It is a stupid problem, but lots of people get stuck in it because they forget to visualize the math AND the calendar and how it works.

2006-09-21 22:24:46 · answer #2 · answered by veraperezp 4 · 0 0

while number is not 0...
check if number is greater than/less than/equal to 7...
do crap depending on what it is
and subtract the number added to the current date...
end...
============
o = original number
x = modulus to divide o
new_date = x + current_date
new_date = 7 modulus new_date
switch statement new_date

--------
The above are two separate ideas.

2006-09-21 22:25:56 · answer #3 · answered by Robin C 4 · 0 0

fedest.com, questions and answers