Good evening, my friends.
I’m having a little stuck with an excercise. Specifically, i can’t find the way, to write a Swich statement for the date suffixes…
I know it’s a piece of cake, for a lot of you, but it is insurmountable for me.
I follow the example, and i wrote this:
// Print the suffix for a given day
switch (fives) {case 5: case 10: case 15: case 20: case 25: case 30:
printf(“contains 1-6 fives.”);
break;
default;
printf(“Contains 0 or more than 6 fives.”);}
// Print the year
return 0;
}
I have no idea, how to continue. If you are willing to help me, i am looking forward, for a feedback.
I think the idea is to put days of the month that end in “st” for one block:
1, 21, 31
put days that end in “nd” for another:
2, 22
put days that end in “rd” for another:
3, 23
and the default case for “th”.
possible solution code
// Print the day
printf("%d", day);
// Print the suffix for a given day
switch(day) {
case 1:
case 21:
case 31:
printf("st");
break;
case 2:
case 22:
printf("nd");
break;
case 3:
case 23:
printf("rd");
break;
default:
printf("th");
}
I did this project, but in a slightly different way, as per the provided instructions. Instead of first printing the numerical form of the day and then adding the proper suffixes, I combined it in one switch statement (talking about steps 5 & 6).
Like this..
switch (day) {
case 1: case 21: case 31: printf("%ist “, day);
break;
case 2: case 22: printf(”%ind “, day);
break;
case 3: case 23: printf(”%ird “, day);
break;
case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30: printf(”%ith ", day);
break;
default: printf("Invalid day ");
break;
}
I have two questions regarding that:
Since this is a pattern, I notice I’m doing quite a lot, I was wondering if this - s.c. failure to follow “agreed” steps - is considered to be an undesirable behaviour in programming world?
I noticed that CC uses keywords case/break and printf statement in separate lines, whereas I combined keyword case and printf statement in one line. Does C language has “best practices” rules?
I would like second opinion on these issues.
PS: I also left the option for wrong input - days past 31st (applies to months too, valid are ones between 1-12). Regarding that I’m not worried, I think it is a feature to avoid potential unwanted behaviour.
You posted in a C language thread, so I’m not quite sure what you are referring to with i am new here. Are you actually learning C? Or are you first time on Codecademy/coding in general?
If the latter, perhaps you can try this sorting quiz, to give you a brief orientation of what to expect/what suits your interests: Codecademy sorting quiz
Overall, my general advice would be (assuming you are genuinely interested in programming), just pick some language and start.