Weekly Challenge: Correct Change

Share
Embed
  • Published on Jan 24, 2019
  • Challenge Page (with solution): www.iamtimcorey.com/courses/c-weekly-challenges/lectures/8950061
    Practice makes perfect, right? Well, mostly, but practice is very important. This series is all about getting you to practice the things you learn. I would encourage you to do every one. See which ones are just easy refreshers and which ones are difficult learning experiences.

Comments • 28

  • americanmultigenic
    americanmultigenic 5 months ago

    Fun to do, and I think I found a pretty workable solution in about 100 lines of code 1within Main(). I am old enough to remember when supermarket cashiers actually had to be able to do this WITHOUT being told what the change total was, let ALONE being told how many of each denomination they should pull out of the drawer as well to make up that total! :-) Pretty old(!)

  • Object Aware
    Object Aware 5 months ago

    I'm curious how others handled invalid input, that is, instead of entering 11.00 the user enters eleven

    • IAmTimCorey
      IAmTimCorey  5 months ago

      I personally didn't worry about validation since we are just testing the change calculator, not the entire application. I do have videos (can't remember which one off the top of my head - maybe someone else can help) that validate the info and re-request the info if it is incorrect/invalid.

  • Shovine Bojraj
    Shovine Bojraj 5 months ago +1

    Hi Tim, i was hoping you could do a very simple way to show us how to send emails from a form in c#. Ive tried to do it but i really cant get it to work. You are the only channel that does c#, sql and visual studio tutorials so well, and if you could do something very simple for idiots like me to understand the mechanics of sending information from a form to an email address, that would be amazing. Again, something simple so we can really get the hang of it. I know your just the guy do it. I really am struggling with this.
    I wana apply for a job and they have sent me a project to do but i keep failing at the part where i need to send the info from a form to an email. I need to send a link in an email, and when the person receives the link in their email, they need to click on it and it must open up in a browser and show the details of the person who filled in the form. Basic form that has name, surname,contact details, and drop down option and radio buttons. All of this info must show.
    I stayed up till 4 am trying to get the form to work but it didnt and i kinda gave up. Also my calender function is not working even though there are tutorials on it. its weird and dam frustrating. I hope you could help me out by making a very detailed and simple video on how to send form information through a link in an email. Not sure if you would do it or have the time, but man, id so appreciate it if you can, coz it killed me not figuring it out and i got burnt out, but i wana try again, and i hope i can get your help Tim.

    • Shovine Bojraj
      Shovine Bojraj 5 months ago

      @IAmTimCorey Thank you Tim ! Im going to check this out

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Here is a video where I set up emailing the user: thexvid.com/video/dSXxLyNd2tw/video.html

      As for the link, you need to add parameters on the link that have the data or you need to add a parameter that tells you what data to look up. Then your page just needs to take those parameters and do something with them (fill in the form or look up the data to fill in the form).

  • Kevin Emmrich
    Kevin Emmrich 5 months ago

    Another nice challenge. Unfortunately, C# is not ingrained enough. I make the simplest syntax errors and have to look everything up. In any case, I pretty quickly (relatively) made it through. The one thing that slowed me down is that I was trying to add all the constants I was using in a public method in the Program Class -- for some reason you can't do that. ??????
    "The price was $26.48 and you gave me $100.03 The change is $73.55
    Your change back is 3 twenties 1 tens 3 dollars 2 quarters 1 nickles"
    I guess the next step is to "ring up" multiple items and ask what bills are being given in. .. and take care of the singular vs. plural in the resultant message. Maybe tomorrow.

    • IAmTimCorey
      IAmTimCorey  5 months ago

      I wouldn't say it is unfortunate. That's the entire purpose of these challenges is to push you to be better at C#. I think it is fortunate that you are growing. My recommendation to you is to not stop. Just because you get a solution doesn't mean it is the best solution. Keep looking into how to make it better.

      Keep up the good works.

    • ZoidbergForPresident
      ZoidbergForPresident 5 months ago

      You declared your constants as "static", right? And putting them in a method won't work either, as their scope is the method, which makes no sense anyway.

  • rekik emnet
    rekik emnet 5 months ago

    Int quarter=0;

    Change = Change × 100;
    While (change>0){
    If(Change%25==0)
    quarter=change%25;
    Else if(change%25>0){
    quarter++;
    Change-=25;
    }
    Else{
    ••••

    • IAmTimCorey
      IAmTimCorey  5 months ago

      That might work for a first version but you have a lot of refactoring to do to make it more efficient and less redundant.

  • Tomasz Mankin
    Tomasz Mankin 5 months ago

    Hello. Can You give me example how application should work? My english is rather average and I am not sure what the context is.

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Danny is right. You can translate the idea into your currency if you feel more comfortable with that. The idea is that this app would be a part of a payment system. So it says you owe X amount (say 5.74 of whatever currency you want). The person making the purchase gives you an amount greater than the total (let's say 10 of the same currency). The Correct Change challenge is to take those two numbers and identify what currency denominations to return. In our case (US currency), we would return four $1 bills, 1 Quarter (worth 0.25 of a dollar), and 1 Penny (worth 0.01 of a dollar).

    • Danny B
      Danny B 5 months ago

      The US dollar denominations are usually $50, 20, 10, 5, and 1. Then $1 can be split into 0.25, 0.10, 0.05, 0.01. So $17.28 can be split with (quantity, denomination) into (1, 10), (1, 5), (2, 1), (2, 0.1), (1, 0.05), and (3, 0.01).

  • ZoidbergForPresident
    ZoidbergForPresident 5 months ago

    I'm not going to bother with american money! Ahahah! XP
    Interesting exercise though, I guess I'd first write a Change class and go from there?

    • ZoidbergForPresident
      ZoidbergForPresident 5 months ago

      @IAmTimCorey Strange how adding 0.01 and 0.20s ended up with me having 0.7000001 then. :P

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Not an error, it is the amount of precision with which it is stored. The more precise, the more expensive it is.

    • ZoidbergForPresident
      ZoidbergForPresident 5 months ago

      @IAmTimCorey But I wonder why float is subject to that rounding error... :/

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Correct. Float is also known as Single. It is precise to 7 places only (and less as you multiply or divide). Double is precise to 15 or 16 places. Decimal is precise to 28 or 29 places.

    • ZoidbergForPresident
      ZoidbergForPresident 5 months ago

      @IAmTimCorey And float is the worst? :P