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

I am currently writing a c++ program for my college homework, and I am almost done but here is the problem:

Part of this problem is adding days of the week and coming up with the correct answer, such as adding 13 days to Wednesday has to output Tuesday, but if the number added has a range, I would be fine, but the thing is is that you should be able to put anything in and arrive at the answer.

I need help coming up with a formula that I can use.

I assigned numbers to the days so I can use it mathematically.

Sunday is 0,
Monday is 1,

and so forth.

So basically, I need to find a formula that will give me one constant answer depending on what day of the week it is.

I know this is hard to explain.

What I need is a formula using the days the person entered (such as 14, 22, 2, etc) and the day assigned to the current day (0-6) to give me a range of 7 answers using the 7 days.

Programmers probably have a better idea of what I am talking about than anyone.

2007-06-06 18:50:58 · 4 answers · asked by Anonymous in Science & Mathematics Mathematics

Oh my god thank you all. I kinda didn't get the % when I was reading it in the book. I guess I'll go back and relearn it as it seems very important.

2007-06-06 19:06:38 · update #1

4 answers

I don't know if C++ has a modulo operator or not, but languages usually do... it's usually the percent sign (%) too. I'd take the number, say x and do the following, given the number of the current day, y

(y+x) % 7

That should give you the number of the day of the week you want. You may or may not need to add or subtract 1 in the parenthesis due to array indexes and all, but that's the basic math behind it.

Note: "Modulo" simply means the remainder after division. 10 % 3 = 1, 12 % 5 = 2, and so on.

2007-06-06 18:56:06 · answer #1 · answered by atmtarzy 2 · 2 0

Use the modulo ("Mod") function %. Start day + number of days Mod 7 will give you an answer that is the remainder of days 0 - 6 which will convert back to days of the week.

Wednesday + 13 days Mod 7 = Tuesday
3 + 13 % 7 = 2

2007-06-07 02:11:35 · answer #2 · answered by Michael 2 · 1 0

If x = value of the day of the week
then set x = mod(x,7) at the end of the operation
mod(x,7) divides x by 7 and outputs the remained.

So if x = 4, mod(x,7) = 4
if x = 13, mod(x,7) = 6

Just make sure C++ uses that terminology. But that's the idea.

**EDIT**
I think the answerer above me is correct. C/C++/Javascript uses that % sign, but it means the same thing.

2007-06-07 01:56:38 · answer #3 · answered by Dr D 7 · 0 1

Hi. Check out modulo 7.

2007-06-07 01:54:36 · answer #4 · answered by Cirric 7 · 3 0

fedest.com, questions and answers