Hello, I got quite complicated exercise, its about days. Exercise :
Day of the week when new month started : 4th.
Interval of days in the month: 9-17.
Result:
9th day: 5
10 day: 6
11 day: 7
12 day: 1
13 day: 2
14 day: 3
15 day: 4
16 day: 5
17 day: 6
All I have done is this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
int n=4, // day of the week when new month started
k=1; // day of the week counter
for(int i=9;i<=17;i++)
{
if(i%8!=0) cout << i << "Day:" << k++ << endl;
else {k=1; cout << i << "Day:" << k++ << endl;}
}
return 0;
}
I dont know where and how do I use n.
Would be nice to get a hint :P
Hey there, first of all do you really need that variable 'k', I do not think so. It is not even relative to the question.
Think about your program again, what it outputs and what it is supposed to output, for example take the 9th day of the month the output should be 5 but your program outputs 1!
All you need is the variable 'i', using simple calculations, and maybe an if-statement to get the correct value. Since you asked for a hint I won't post the code but I think a simple table of values would help :)
Day of the month: 1 .... 2 .... 3 .... 4 .... 5 .... 6 .... 7 .... 8 .... 9 .... and so on.
Day of the week: 4 .... 5 .... 6 .... 7 .... 1 .... 2 .... 3 .... 4 .... 5 .... and so on.
Use these operators (%, +) to calculate your output.
A bit closer now, but still no good, and I dont know if I can do it without k, it seems to help me out a bit. If "i" is 1, everything is ok, but I need "i" to be 9, and then there begins problems.
imo, you should really solve this on your own. and you are already kinda getting closer :)
[spoiler]: if you want to get the day of the week from the day of the month you have to limit the value from 1 to 7, you can limit it to be from 0 to 6 with % operator, which you did :). now when the value is 0 you use an if to handle it .... here is how to get the day of the week from the day of the month: (i + 3) % 7
3 is actually n-1 this is to set 0 to be 3 and therefore 1 is 4 and so on.
or you can do (i + 2) % 7 + 1 this limits the value to be from 0 to 6 and then add 1 so it limits it in the range 1-7 pretty straight forward right? :)
why (i + 2)? the same as above this sets 1 to 3 instead of 4 and then add 1 in the last after doing ((i + 2) % 7) so it's 4 again and so for other numbers (days of the month)
day of the month (i) : 15
(i + 3) % 7 : (15 + 3) % 7 -> 18 % 7 -> 4
(i + 2) % 7 + 1 : 17 % 7 + 1 -> 3 + 1 -> 4
day of the month : 12
(i + 3) % 7 : (12 + 3) % 7 -> 15 % 7 -> 1
(i + 2) % 7 + 1 : 14 % 7 + 1 -> 0 + 1 -> 1
now the odd case when (i + 3) % 7 = 0 this is the 7th day of the week
day of the month : 11
(i + 3) % 7 : (11 + 3) % 7 -> 14 % 7 -> 0
here you can use an if to change it to 7
What's with line 7 ? Just because it compiles does not make it good coding.
Line 7: int n=4,i,k=0; should be on separate lines.
int n=4;
int k=0;
then in the main you can initilize i inside the loop.
for (int i ....)
Look at your initialization values closer. You are so close to the answer you're over looking it.
When do your variables change values? How does this effect your display out
come? At the time your program prints to the screen, what are the values in your variables?
Ok, I have done a similiar exercise to this one, but maybe a bit easier one ? And Id like to know if its done right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
int m, // modulus
i=1984, // year im starting with it
a=1950; // year basketball championship started
m=a%4;
for(i;i<=2007;i++)
{
if(i%4==m) cout << i << " " << ((i-a)/4)+1 << endl;
}
return 0;
}
(i-a)/4+1 - how many times championship has happened before + continuing.
Expected and got result :
1986 10
1990 11
1994 12
1998 13
2002 14
2006 15
Also, I think ive done my first exercise, but im not sure if its right, numbers are correct though.
Yes that is correct, although you could make it simpler but never mind that works too.
Also as pearlyman pointed out you dont need to include iomanip and cmath libararies, and i variable could be initialized inside the loop. Read his post again.
Iomanip and cmath are in my default code, I oftenly use them for other things. Basketball championship code is right ? And I just checked more about my good old days code, its wrong. Ill try playing around with it a little bit more.