Talking Clock - The Daily Programmer #321

**Implementing the Talking Clock Problem**

To implement the talking clock problem, we start by declaring four arrays: hours, tens, ones, and suffixes. The hours array represents the hours on the clock (0-11), the tens array maps numbers to their corresponding string representation in the teens (10-19), the ones array maps numbers 0-9 to their corresponding string representation, and the suffixes array determines whether the time is AM or PM based on the hour.

We then use these arrays to create a function that takes an integer as input and returns a string representing the time on the clock. The first sub-problem is how do we print out the hours? We can simply grab the hours array and begin to mod by 12. This will give us the correct hour value between 1-12.

**Printing Out Hours**

We can then set `const hour is equal to that` and print out the result using the `print` function. In this case, we see that it prints out 12, which is expected. We have now solved the first sub-problem of printing out the hours.

Before moving on to the next step, let's take a closer look at how the hours array works. We can print out the hours array using the `print` function and see its contents. This will give us an idea of what we are working with. We can also use this opportunity to test out edge cases and make sure that our implementation is correct.

**Printing Out Suffix**

The second sub-problem is how do we print out the suffix of AM and PM? We can simply declare a constant `const suffix is equal to` and use a ternary operator to determine whether the hour is less than 12. If it is, we print out "a.m.", otherwise we print out "p.m.". We can then use this suffix value in our final string.

Let's test out this implementation by printing out some examples. For instance, if we input 12, we see that it prints out "12:00 a.m.". This is expected behavior. Similarly, if we input 12, we see that it prints out "12:00 p.m.". Again, this is expected behavior.

We can continue to test out our implementation by inputting different values and seeing how the output behaves. This will help us ensure that our implementation is correct and accurate.

**Handling Different Cases**

The third sub-problem is where we have to handle different cases. Let's declare a variable `minute` and use it to print out whatever the minute value is in the input integer. We can then test out edge cases to make sure that our implementation is working correctly.

In one case, if the minute value is less than 10, we need to print out the ones array corresponding mapping value. This will give us the correct string representation of the minutes digit. Let's test this out by inputting a value such as 1, which should print out "01:00 p.m.". We can also test out edge cases such as inputting 0 or 9 to ensure that our implementation is working correctly.

In another case, if the minute value is greater than or equal to 20, we need to set `minute` equal to and then use the tens array and floor function to determine the string representation of the tens digit. We can then follow this with a space and add the ones array string representation. Let's test out this implementation by inputting a value such as 29, which should print out "11:29 p.m.". Again, we can test out edge cases to ensure that our implementation is working correctly.

**Putting it All Together**

Now that we have implemented all three sub-problems, let's put them together in a single function. We can declare the four arrays and then use them to determine the string value based on the input integer.

Here is the final code:

```c

#include

using namespace std;

int main() {

int hours[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

string tens[] = {"", "", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

string ones[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

string suffixes[] = {"a.m.", "p.m."};

cout << hours[12] << ":00 " << suffixes[(hours[12] < 12) ? 0 : 1] << endl;

return 0;

}

```

This code will output the time on the clock based on the input integer. We can test out edge cases and make sure that our implementation is working correctly.

We have now successfully implemented the talking clock problem!

"WEBVTTKind: captionsLanguage: enHey FreeCodeCamp welcome to another the daily programmer web series video the problem todayWe're going to talk about is called talking clockWhich is number three to one let's go ahead and look at the subreddit and read through it alrightso for this problem the description says no more hiding from your alarm clock you decide you want your computer to keep you updated fromTime to time, so you're never late again a talking clock takes a 24 hour time and translates it into wordsso for the input, we're givenThis type of string so military time for instanceWe have the string 20 : 29 which would be 8 29 and then our goal is to take that input string in to printOut its string representations. So in this case 2029 should print out its 829 p.m.And then for 00 if you print out its 12 a.m.So from reading the description just looking at the input data in the output dataIt should be pretty straightforward as to what we're trying to doSo let's start breaking this down in tempo mania on the white board alright, so let's look at a single example hereWhere we have 22?: 35 and the first thing that comes to mind is when you have something like this you need to kind of process it fromYou know the hours and the minutesIt's useful to kind of extract your hours separate from your minutesSo you can go ahead and split this string on the colonwhich will give you an array of22 and 35 as string form and then you canConvert those to integers using the Partizan method or you know math the array and pass it apart and functionBut regardless when you end up with 22 and 35 we're going to be using these to kind of generate these three subproblemsSo if you were to look at the main problem that we're trying to solve there are three differentThings you kind of have to do and you could do this separately so the first one isHow do you print out the hour? So that's like?How you print out one o'clock two o'clock three o'clock to 12 o'clock. How do you print out?The minute is so problem 2 which will be the hardest and we'll save that one for lastAnd that would be like how do you print out?If it's zero five zero five in this case thirty five, how do you print out 35 am or pM and?Then the third sub problem is how do you print out am or pM?andAgain since this military time zero is 12 a.m.. 12 is 12 noon which is 12 p.m.and then 23 would be a11 p.mSo out of these three sub-problem. Let's go ahead and start with the easiest one which would be print a.m.P.m.. So to do that this one should be pretty straightforward, so if you were to take the ourrepresentation here so 22How would you check if this is in the afternoon or if this is in the morning like a.m.Well, basically if it's less than 12We know that should be a.m.Otherwise it should be p.m.So in this case22 would be 10 o'clock at night so we can just use this if statements to determine if it's amber p.m.And that's pretty straightforward so in this case. We're going to say it's p.m.. Go ahead and write p.m.At the end of your string and see that for laterSo we solved the first sub problem. Which is hedy to determine if it's a more p.m.. Pretty straightforward andThen let's move on to sub problem 1. It's how do you print out the hour?So to do this first we want to declare some type of structureso you can divert the number that we have as the input andDetermine that the string. It needs to be out put it right so over hereI went ahead and defined an array which represents the different hoursWe can print out so 12 1 2 3 4 all the way to 11 o'clockAnd I'm mapping some index to those so it turns out that you know these are integersAnd they map pretty well to the indexes of arraysso I went ahead and just decided to choose an array to kind of figure out the conversion, soTo map let's say in number 22 to the corresponding number. It needs to be over in this hours arrayWe could simply just mod it by 12. Which is the number of hours that you have in each segment of the day a.m.And p.m.. So if you do 22 Mod 12we're left with 10 andIf you use that index 10 and figure out what it needs to be in this hours array?Index 10 should point to the string 10So in this case down here. We could just say you know print outhours of22 Mod 12 and that should give us index 10 which would then print out the string?TenI'll just go ahead and start this with it'sRight and so if you were to do something else such as instead of let's say this was in tWenty-twoAnd I said this is twelveWe'd be left with the index of zerowhich would give us 12 andIf the input was let's say zeroWe're also going to get zero which is twelve because you remember zero zero is 12:00 a.m.Which again will work as we intended to so that's how you do the subproblem print hours pretty straightforward as welljust declare an array or each index of the array points to the string representation of that index andThen just mod your input hours to by 12 to get whatever index you need to print outSo let's move on to the last part of this problemWhich is the harder part which is printing out the ministering?So to start off there's there's a couple of different subproblems the first step problem for printing the minute isLet's say it's a number that's less than 10 right those are single-digit numbers, so in this caselet's say this is o 5 instead of 35 we need to print out o5So again, we can still do like the same logic herebut instead of using an hours array, we can go ahead and just use aOnes Array and the ones array same idea of the hours array, but instead of 0 through 11We have indexes 0 through 9 where 0 is a blank string1 is the string 1 ETC until 9 we have the string 9 so in this particular case?How do you print out if let's say we had a 5 as our input?How would you print out a 5 here right? So if it is less than 10?we know that we need to use this ones array to kind of figure out what the string needs to be andagain, we could justTake the digit as our index and print it out hereSo in this case we get oh5 here which would be cast to 5 if we parse it as an int and then our index 5 would just print out 5And we can prefix with ohThen concatenate it with whatever that is again. I'll write outthe first case which is ifm is Less than 10And again the first case and most of ten we just need to print out single-digit the second caseifm is less than twentyWe need to print out the teens right so 13 14 15 11 12, whatever so let's go ahead and declare anotherarray called teensWhich we can use to again map numbers from 10 to 19Using the same process so again this one once we get mWe're actually Gonna have to mod it by 10So we can get what value it is in this array. So in this case if m is let's say15If we mod that by chin, we'll get 5 and if we look up index 5 and this tins array or teens array5 should equal the string 5if we try it with let's say 10 mod 10We're going to be left with 0 which will give us10 is our string ifwe Mod it by19 by 10 we're going to be left with Index 9Which again over here 9 is 19?So we saw sub-problem if m is less than 10We set the subproblem if n is less than 20 now for the last sub problem is elseSo otherwise it's greater than or equal to 20Now in this case. We need yet another array, which will be called our kins arrayAnd what this array is used for is printing out the tens digitSo in this case 35 we need to print out 30 and then we also need to print out 5So taking that example. Let's just go ahead and say okaywellHow do we print out 30 so we have this tens array over here index is 0 through 5?We need to get index 3 right the digit 3 will represent 30 down hereSo to do that. We can just do m divided by 10 and then floor thatAnd that should leave us with 3 in this case in next 3 will give a sturdygo ahead right, it's 10:30 andthen the last part of thisIs we need to get the ones digit so I can say em?mod 10 in this case 35 mod 10 will give us 5 andIf we were looked at up in the ones array over herewe should get the string 5 so if I were to just do that, so we can pronounce 5 here andThat's basically how you work out this problem inThis sub problem where if you were to print out 35, we just use the ones array we use a tens arrayYou kind of connect concatenate them together we can mod by 10 or divide by 10 to get the different digits?So divide by 10 gives us 3 mod by 10 gives us 5 look those up and then print to my down hereSo finally the single input 22 : 35 will give us the output it's 10:35 p.m.hopefully this is a good overview of how to break this down into different subproblems andAgain how to break in break down this sub problem 2 which is print minute into the different cases that we need to handleThere's probably a different way to do thisBut I think this is the easiest that comes to my mind all right so again let's implement this in JavascriptAlright so start off with the implementationI went ahead and declared those 4 arrays that are used for mapping the integer value to the string representation. So you see hereWe have a capital ours array. Which has all the 12 to 11. We have the capital one's arrayWhich has all the different ones digit so 1 2 3 4 5 7 8 9 we have two teams arrayWhich is used for mappingall those different cases where we have 10 through 19 andThen finally we have the tins arrayWhich is used for mapping the tens digit of cases that are greater than equal to 20 and then down here?which is go ahead and start with the use case of0 0 comma 0 0 and let's start declaring the function called talk which takes 8 time as an input parameterSo the first thing we want to do again is take that time string and we go ahead and split itpars the int and map it to an array like say const ofH m is equal to time we've split and then split it by the colons and then map it to by the pars intfunctionAnd then I'll go ahead and return the empty string here just for nowSo just to kind of see what's going on here. I'll print out hMMThose both print out zero so that's expectedif I were to change this to let's say 1 &amp; 1Those both print out 1 &amp; 1 so that's working as intendedSo let's first start with the first sub problemWhich is how do you print out the our string so to do that remember? We have the hours array? Which we can grab?H from and then begin to Mod by 12 andthen we could say const hour is equal to that andIf I were to print out our down here we see that it prints out 12So we just solved the first sub problem, which how do we print out the hours and you see down here?We're printing out 12 which is expected and then before we take a step forwardI'm just going to go ahead and just print out or start working on an array where we can say it's andThen also pre mount hours, and I'm going to join thosewith an empty stringsuch is kind of building up for a final string that we're supposed to print hasn't our output andThen the second sub problem is how do you print out the suffix of am and pm so again?We can say const suffix is equal to and then we use a ternary operator here say h less than 12Then we need to print out a.m.Otherwise we print out p.m.. And we can just go ahead and put suffix into that final array that we're returningSo you see down here at printing out it's 12 a.m.If we change this to 12 it prints out its 12 p.m.. So this is all working as intended. It's 11 p.m.. Works fineandNow for the third sub problem, which is where we have to have those different cases. Let's just go ahead and declare a minuteobject andThen I'm going to go ahead and put that here so we can print out whatever the minute value isIn the first case is if m is less than 10 remember we need to print out the ones arraysCorresponding mapping value, right so we can say minutes is equal toone of EmAnd I'll go ahead and change this to2301 It says it's 11:00 1:00 p.m.. So I forgot to put the o hereSo now it's printing out it's 1101 p.m.. And then go ahead and test out your edge cases of 0-9 it's 11 O 9:00 p.m.So that's working as intended by change this the tinProbably won't work as we went to because it should print out its11:10 p.m.. And we haven't implemented that case yet? So let's move on to the second case which is ifm is Less than 20we need to set minute equal to and then we could say theteens andThen m divided by 10 like we did on the exampleGo ahead and close off that curly braceSo now in this case that's working fine, so it's 11:10 p.m.If I change this to 19 it should pronounced 1119 p.m.. So againWe just implement a case 1 which is it is less than 10?Case 2 for the teens if it's less than 20And then finally we use the or finally we write the last case which will be if it's greater than equal to 20So we could set minute equal to and then first we need to do the tensagain we floor in divided by 10 here, and that will give us our tins string representation of the tens digit andThen followed by space we can dograb the ones arraypass it in Mod 10 andThat will give us the one string are the ones digit string representationAnd then we can test that out here if you put 23 20, it says it's 11:20 p.m.. So that's goodIt's 11:29 p.m.. When we change this to 29Change it to 59 its 11:59 p.m.. So basically we've implemented the same solution that we worked on the whiteboard andkind of as a recap we declared those four arrays andwe're using those arrays to kind of determine what the string value should be based on the integer values and we use theindex for that mappingalright, so that wraps up the implementation of the talking clock problem again if you liked this tutorial be sure to like the video andSubscribe to free code Camp below all right. Thanks for watching\n"