Help with function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    Help with function

    Trying to write a function that does the following:

    Joe gets 100 points for every 30 widgets he sells.

    So if he sells less than thirty he gets nothing. If he gets between 30 and
    59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....

    How can I display the points joe gets depending on then number of widgets
    entered.


  • Paul

    #2
    Re: Help with function

    Nothing like asking for help and getting shot in the face. Disregard then.


    "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
    news:vjik3nk9ti j893@corp.super news.com...[color=blue]
    > "Paul" <PB@nospam.netw orktel.net> wrote...[color=green]
    > > Trying to write a function that does the following:
    > >
    > > Joe gets 100 points for every 30 widgets he sells.
    > >
    > > So if he sells less than thirty he gets nothing. If he gets between 30[/color]
    > and[color=green]
    > > 59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....
    > >
    > > How can I display the points joe gets depending on then number of[/color][/color]
    widgets[color=blue][color=green]
    > > entered.[/color]
    >
    > What have you already done (aside from typing your assignment)?
    > If you have a program that doesn't compile, post it, we could
    > help you fix it. If you haven't done nothing, well, go do
    > something. We can help, but we're not going to do your homework
    > for you.
    >
    > If you don't know where to begin, here is a simple program that
    > allows you to enter a number and then it prints it back:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > cout << "enter a number " << flush;
    > int number;
    > cin >> number;
    > cout << "you entered " << number << endl;
    > return 0;
    > }
    >
    > Victor
    >
    >[/color]


    Comment

    • Paul

      #3
      Re: Help with function

      In anycase I know that if I divide the number of widgets by 30 and then
      taking the whole
      number of the result and multiply by 100 I will get it. Thought there might
      be a cleaner way.

      "Paul" <PB@nospam.netw orktel.net> wrote in message
      news:vjijbsecb3 s179@corp.super news.com...[color=blue]
      > Trying to write a function that does the following:
      >
      > Joe gets 100 points for every 30 widgets he sells.
      >
      > So if he sells less than thirty he gets nothing. If he gets between 30[/color]
      and[color=blue]
      > 59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....
      >
      > How can I display the points joe gets depending on then number of widgets
      > entered.
      >
      >[/color]


      Comment

      • John Harrison

        #4
        Re: Help with function


        "Paul" <timK@nospamaol .com> wrote in message
        news:vjikl7kn50 c23f@corp.super news.com...[color=blue]
        > Nothing like asking for help and getting shot in the face. Disregard[/color]
        then.[color=blue]
        >[/color]

        No-one's shot you in the face (or even slapped you in the face). There are
        three good reasons why you should post the code you've already written,

        1) It helps everyone assess what skill you already have and so give advice
        appropriate to your level of skill
        2) It helps describe your problem and what exactly you are stuck on much
        better than a prose description can ever do, therefore posters won't waste
        time answering the wrong question.
        3) It proves that you are prepared to make some effort yourself, and aren't
        just expecting to be handed the answer.

        These rules apply to everyone, no-one is picking on you. So, post the code
        you've already written and you'll get good answers very quickly, and
        everyone will be happy.

        john


        Comment

        • John Harrison

          #5
          Re: Help with function


          "Paul" <timK@nospamaol .com> wrote in message
          news:vjil4091hg o0aa@corp.super news.com...[color=blue]
          > In anycase I know that if I divide the number of widgets by 30 and then
          > taking the whole
          > number of the result and multiply by 100 I will get it. Thought there[/color]
          might[color=blue]
          > be a cleaner way.
          >[/color]

          Sounds good to me. That's how I would do it.

          john


          Comment

          • Howard

            #6
            Re: Help with function


            "Paul" <timK@nospamaol .com> wrote in message
            news:vjil4091hg o0aa@corp.super news.com...[color=blue]
            > In anycase I know that if I divide the number of widgets by 30 and then
            > taking the whole
            > number of the result and multiply by 100 I will get it. Thought there[/color]
            might[color=blue]
            > be a cleaner way.
            >[/color]

            That already sounds like the cleanest way to me!

            int points( int widgets )
            {
            return 100 * ( widgets / 30 );
            }

            If widgets is an integer and the function returns an integer, then that
            division already gives you the "whole number" part, so the above code should
            be sufficient.

            -Howard


            Comment

            • Paul

              #7
              Re: Help with function

              Thanks John.


              "John Harrison" <john_andronicu s@hotmail.com> wrote in message
              news:bhbkkq$10c q44$1@ID-196037.news.uni-berlin.de...[color=blue]
              >
              > "Paul" <timK@nospamaol .com> wrote in message
              > news:vjil4091hg o0aa@corp.super news.com...[color=green]
              > > In anycase I know that if I divide the number of widgets by 30 and then
              > > taking the whole
              > > number of the result and multiply by 100 I will get it. Thought there[/color]
              > might[color=green]
              > > be a cleaner way.
              > >[/color]
              >
              > Sounds good to me. That's how I would do it.
              >
              > john
              >
              >[/color]


              Comment

              • John Harrison

                #8
                Re: Help with function


                "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                news:bhbkkq$10c q44$1@ID-196037.news.uni-berlin.de...[color=blue]
                >
                > "Paul" <timK@nospamaol .com> wrote in message
                > news:vjil4091hg o0aa@corp.super news.com...[color=green]
                > > In anycase I know that if I divide the number of widgets by 30 and then
                > > taking the whole
                > > number of the result and multiply by 100 I will get it. Thought there[/color]
                > might[color=green]
                > > be a cleaner way.
                > >[/color]
                >
                > Sounds good to me. That's how I would do it.
                >[/color]

                But note also, you don't need to take the whole number of the result. In C++
                when you divide two integers the answer is always another integer.

                int points_per_widg et(int widgets)
                {
                return (widgets/30)*100;
                }

                john


                Comment

                • Paul

                  #9
                  Re: Help with function

                  Ah makes perfect sense now!

                  Thank you
                  "Howard" <alicebt@hotmai l.com> wrote in message
                  news:bhbkrn$bj2 @dispatch.conce ntric.net...[color=blue]
                  >
                  > "Paul" <timK@nospamaol .com> wrote in message
                  > news:vjil4091hg o0aa@corp.super news.com...[color=green]
                  > > In anycase I know that if I divide the number of widgets by 30 and then
                  > > taking the whole
                  > > number of the result and multiply by 100 I will get it. Thought there[/color]
                  > might[color=green]
                  > > be a cleaner way.
                  > >[/color]
                  >
                  > That already sounds like the cleanest way to me!
                  >
                  > int points( int widgets )
                  > {
                  > return 100 * ( widgets / 30 );
                  > }
                  >
                  > If widgets is an integer and the function returns an integer, then that
                  > division already gives you the "whole number" part, so the above code[/color]
                  should[color=blue]
                  > be sufficient.
                  >
                  > -Howard
                  >
                  >[/color]


                  Comment

                  • Default User

                    #10
                    Re: Help with function

                    Paul wrote:[color=blue]
                    >
                    > Nothing like asking for help and getting shot in the face. Disregard then.[/color]


                    Are you looking to the third jackass to hit the killfile today? We don't
                    do homework, which if you'd taken a bare amount of time to find the
                    newsgroup FAQ and read you'd already know.

                    You have to show some sort of intiative, and posting your homework
                    assignment doesn't cut it.




                    Brian Rodenborn

                    Comment

                    • Thomas Matthews

                      #11
                      Re: Help with function

                      Paul wrote:[color=blue]
                      > Trying to write a function that does the following:
                      >
                      > Joe gets 100 points for every 30 widgets he sells.
                      >
                      > So if he sells less than thirty he gets nothing. If he gets between 30 and
                      > 59 he gets 100, 60-89 he gets 200, 90-119 he gets 300 etc.....
                      >
                      > How can I display the points joe gets depending on then number of widgets
                      > entered.
                      >
                      >[/color]

                      Many people solving this problem or similar problems use a cascade
                      set of "if" statements:
                      unsigned int widgets_sold;
                      unsigned int points_earned;

                      //...
                      if (widgets_sold < 30)
                      {
                      points_earned = 0;
                      }
                      if ((widgets_sold >= 30) && (widgets_sold < 60))
                      {
                      points_earned = 100;
                      }
                      // Und so weite.

                      A switch statement is not an efficient approach since the case
                      statement requires an integral constant and doesn't handle
                      ranges. All the values within the ranges would have to be
                      listed with cases:
                      switch (widgets_sold)
                      {
                      case 0:
                      case 1:
                      // ...
                      case 29:
                      points_earned = 0;
                      break;
                      // Und so weite.
                      }


                      Another, maybe more proper method, is to use a range, value
                      table. The table contains records of
                      <range start, range end, value>

                      struct Range_Record
                      {
                      unsigned int start;
                      unsigned int end;
                      unsigned int points;
                      };

                      Then create the table:
                      const Range_Record Price_Table[] =
                      {
                      // start end points
                      { 0, 29, 0},
                      { 30, 59, 100},
                      { 60, 89, 200},
                      // Und so weite.
                      };
                      const unsigned int NUM_PRICE_RECOR DS =
                      sizeof (Price_Table) / sizeof (Price_Table[0]);


                      The above methods are usually applied when there is no
                      mathematical expression to the data and the range
                      of values is finite.

                      The big advantage to using the table, is that you can
                      add entries without having to retest the code that
                      searches the table. In many situations, modifying
                      executable code causes many problems and much regression
                      testing.

                      Just showing alternatives.

                      --
                      Thomas Matthews

                      C++ newsgroup welcome message:

                      C++ Faq: http://www.parashift.com/c++-faq-lite
                      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                      alt.comp.lang.l earn.c-c++ faq:

                      Other sites:
                      http://www.josuttis.com -- C++ STL Library book

                      Comment

                      Working...