Please comment on my integer to string code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manoj1978@gmail.com

    #1

    Please comment on my integer to string code.

    Hi All,
    I wrote the following to print an integer in its string
    representation for base -36 to 36.
    Please comment on this code.

    #include <iostream>
    #include <string>

    using std::abs;
    using std::cout;
    using std::endl;
    using std::reverse;
    using std::string;

    char charTable[] = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ";

    void printBase(int i, int base)
    {
    if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
    {
    cout << "0" << endl;
    return;
    }

    string str = "";
    if ((base > 0) && (i < 0))
    {
    str += "-";
    i = -i;
    }

    do {
    if (i % base >= 0)
    {
    str += charTable[i % base];
    i = i / base;
    } else {
    str += charTable[i % base - base];
    i = i /base + 1;
    }
    } while(i);
    reverse(str.beg in(), str.end());
    cout << str << endl;
    }

    int main()
    {
    for(int i = -55; i <= 55; i++)
    {
    cout << i << " = ";
    printBase(i,-10);
    }
    }

    Thanks and Regards,
    manoj.

  • red floyd

    #2
    Re: Please comment on my integer to string code.

    manoj1978@gmail .com wrote:[color=blue]
    > Hi All,
    > I wrote the following to print an integer in its string
    > representation for base -36 to 36.
    >
    > [code redacted][/color]

    How do you display numbers in a *negative* base?????

    Comment

    • manoj1978@gmail.com

      #3
      Re: Please comment on my integer to string code.


      red floyd wrote:
      [color=blue]
      > manoj1978@gmail .com wrote:[color=green]
      > > Hi All,
      > > I wrote the following to print an integer in its string
      > > representation for base -36 to 36.
      > >
      > > [code redacted][/color]
      >
      > How do you display numbers in a *negative* base?????[/color]

      Consider negadecimal.

      27 will be 187

      since 187 = 1 * -10 * -10 + 8 * -10 + 7 = 27.

      Comment

      • Phlip

        #4
        Re: Please comment on my integer to string code.

        manoj1978@gmail .com wrote:
        [color=blue]
        > int main()
        > {
        > for(int i = -55; i <= 55; i++)
        > {
        > cout << i << " = ";
        > printBase(i,-10);
        > }
        > }[/color]

        Unit tests are the most important aspect of programming. Your printBase
        should return a string, so you can test it, and so it won't "couple" with
        its environment. Here you have some cout outside printBase, and some
        inside. Move all outside, then write unit tests like these:

        assert("whateve r" == printBase(-55);
        assert("whateve r" == printBase(-55);
        assert("whateve r" == printBase(-55);
        assert("whateve r" == printBase(-55);

        Now each time you change the program, run those tests. If you make certain
        kinds of mistakes (but not any mistake) the test will catch you, and you
        can use Undo to get rid of the mistake. Undo is much more efficient than
        debugging.

        --
        Phlip
        http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

        Comment

        • manoj1978@gmail.com

          #5
          Re: Please comment on my integer to string code.


          Phlip wrote:
          [color=blue]
          > Unit tests are the most important aspect of programming. Your printBase
          > should return a string, so you can test it, and so it won't "couple" with
          > its environment. Here you have some cout outside printBase, and some
          > inside. Move all outside, then write unit tests like these:
          >
          > assert("whateve r" == printBase(-55);
          > assert("whateve r" == printBase(-55);
          > assert("whateve r" == printBase(-55);
          > assert("whateve r" == printBase(-55);
          >
          > Now each time you change the program, run those tests. If you make certain
          > kinds of mistakes (but not any mistake) the test will catch you, and you
          > can use Undo to get rid of the mistake. Undo is much more efficient than
          > debugging.
          >
          > --
          > Phlip
          > http://www.greencheese.org/ZeekLand <-- NOT a blog!!![/color]
          Hi Philip,
          Thanks.I will surely keep this in mind.
          This time I had the expected results in a file.
          I was redirecting the output and doing file compare.
          Regards,
          manoj.

          Comment

          • Victor Bazarov

            #6
            Re: Please comment on my integer to string code.

            Phlip wrote:[color=blue]
            > manoj1978@gmail .com wrote:
            >
            >[color=green]
            >>int main()
            >>{
            >> for(int i = -55; i <= 55; i++)
            >> {
            >> cout << i << " = ";
            >> printBase(i,-10);
            >> }
            >>}[/color]
            >
            >
            > Unit tests are the most important aspect of programming. Your printBase
            > should return a string, so you can test it, and so it won't "couple" with
            > its environment. Here you have some cout outside printBase, and some
            > inside. Move all outside, then write unit tests like these:
            >
            > assert("whateve r" == printBase(-55);
            > assert("whateve r" == printBase(-55);
            > assert("whateve r" == printBase(-55);
            > assert("whateve r" == printBase(-55);[/color]

            Why four of them in a row? Are there side effects expected that affect
            the behaviour of the function?
            [color=blue]
            > Now each time you change the program, run those tests. If you make certain
            > kinds of mistakes (but not any mistake) the test will catch you, and you
            > can use Undo to get rid of the mistake. Undo is much more efficient than
            > debugging.[/color]

            V
            --
            Please remove capital As from my address when replying by mail

            Comment

            • Phlip

              #7
              Re: Please comment on my integer to string code.

              Victor Bazarov wrote:
              [color=blue][color=green]
              >>assert("whate ver" == printBase(-55);
              >>assert("whate ver" == printBase(-55);
              >>assert("whate ver" == printBase(-55);
              >>assert("whate ver" == printBase(-55);[/color]
              >
              > Why four of them in a row?  Are there side effects expected that affect
              > the behaviour of the function?[/color]

              I forgot to edit:

              assert("whateve r" == printBase(-55));
              assert("whateve r" == printBase(-54));
              assert("whateve r" == printBase( -1));
              assert("whateve r" == printBase( 0));
              ...

              The next, more complex topic is: How to make sure the tests use enough
              inputs to exercise all the branches in the code. Don't go there. ;-)

              --
              Phlip
              http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

              Comment

              • Ben Pope

                #8
                Re: Please comment on my integer to string code.

                Phlip wrote:[color=blue]
                >
                > The next, more complex topic is: How to make sure the tests use enough
                > inputs to exercise all the branches in the code. Don't go there. ;-)[/color]

                Classification trees and code coverage tests. Don't know of any free
                software to do that.

                Ben Pope
                --
                I'm not just a number. To many, I'm known as a string...

                Comment

                • Pete Becker

                  #9
                  Re: Please comment on my integer to string code.

                  Phlip wrote:
                  [color=blue]
                  >
                  > The next, more complex topic is: How to make sure the tests use enough
                  > inputs to exercise all the branches in the code. Don't go there. ;-)
                  >[/color]

                  Especially since branch testing is known to be inadequate. <g>

                  --

                  Pete Becker
                  Roundhouse Consulting, Ltd.

                  Comment

                  • Phlip

                    #10
                    Re: Please comment on my integer to string code.

                    Pete Becker wrote:
                    [color=blue]
                    > Especially since branch testing is known to be inadequate. <g>[/color]

                    Inadequate for converting an integer to a string???

                    <vbgd&rc>

                    --
                    Phlip
                    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

                    Comment

                    • Pete Becker

                      #11
                      Re: Please comment on my integer to string code.

                      Phlip wrote:
                      [color=blue]
                      > Pete Becker wrote:
                      >
                      >[color=green]
                      >>Especially since branch testing is known to be inadequate. <g>[/color]
                      >
                      >
                      > Inadequate for converting an integer to a string???
                      >[/color]

                      Certainly inadequate as a testing discipline in general, and almost
                      certainly inadequate for testing a function that converts an integer to
                      a string. Whether it gives you adequate coverage in any particular case
                      depends on the case. Applied to the original code it is inadequate.

                      For example, branch analysis could be satisfied with test cases that
                      only exercise the do loop exactly twice: once to branch back to the head
                      of the loop and once to branch out of it. That doesn't test what happens
                      when you need exactly one digit or three or more digits to represent the
                      value. Of course, doing that one thoroughly requires path testing, which
                      is known to be prohibitively expensive <g>, although perhaps workable
                      for this example.

                      For another example, if the charTable had some incorrect values, a test
                      suite based only on branch testing wouldn't necessarily find them.

                      --

                      Pete Becker
                      Roundhouse Consulting, Ltd.

                      Comment

                      • Dietmar Kuehl

                        #12
                        Re: Please comment on my integer to string code.

                        Phlip wrote:[color=blue]
                        > Inadequate for converting an integer to a string???[/color]

                        At least insufficient. For example, branch testing would not detect
                        the error which is in the original code when passing
                        'std::numeric_l imits<int>::min ()': the value range for integers is
                        [normally] asymmetric and "i = -i" is the identity operation for
                        the two values '0' and 'std::numeric_l imits<int>::min ()' (well, for
                        the latter value the behavior may actually be undefined or
                        implementation defined). That is, in addition to branch testing you
                        need to perform at least some testing for corner cases.
                        --
                        <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
                        <http://www.eai-systems.com> - Efficient Artificial Intelligence

                        Comment

                        • Phlip

                          #13
                          Re: Please comment on my integer to string code.

                          Dietmar Kuehl wrote:
                          [color=blue]
                          > That is, in addition to branch testing you
                          > need to perform at least some testing for corner cases.[/color]

                          Tiny little voice: Then add, uh, branches inside the code to handle or throw
                          the corner cases...

                          But thanks guys - I put both yours and Pete's answers on a Wiki page
                          BranchTesting. For some strange reason no page by that name was there
                          yet. ;-)

                          --
                          Phlip
                          http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

                          Comment

                          • BobR

                            #14
                            Re: Please comment on my integer to string code.


                            manoj1978@gmail .com wrote in message
                            <1141916757.291 734.248560@p10g 2000cwp.googleg roups.com>...[color=blue]
                            >Hi All,
                            > I wrote the following to print an integer in its string
                            >representati on for base -36 to 36.
                            > Please comment on this code.
                            >
                            >#include <iostream>
                            >#include <string>
                            >[/color]
                            // >using std::abs;[color=blue]
                            >using std::cout;
                            >using std::endl;[/color]
                            // >using std::reverse;[color=blue]
                            >using std::string;[/color]

                            Style-wise, **IMHO**, (at this point) it stinks!! I'd put the 'using *'
                            inside the function/main if I couldn't type "std::".
                            My suggestions inline below.
                            [color=blue]
                            >[/color]
                            // >char charTable[] = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ";

                            std::string charTable( "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ" );
                            [color=blue]
                            >[/color]
                            // >void printBase(int i, int base){
                            std::string CalcBase(int i, int base){
                            using std::abs; // etc.
                            [color=blue]
                            > if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0)){[/color]
                            // > cout << "0" << endl;
                            // > return;
                            return "0";[color=blue]
                            > }
                            >
                            > string str = "";
                            > if ((base > 0) && (i < 0)){
                            > str += "-"; // prints "-16 = 10-" for base==16
                            > i = -i;
                            > }
                            >
                            > do {
                            > if (i % base >= 0){[/color]
                            // > str += charTable[i % base];
                            str += charTable.at( i % base );
                            [color=blue]
                            > i = i / base;
                            > } else {[/color]
                            // > str += charTable[i % base - base];
                            str += charTable.at( i % base - base );
                            [color=blue]
                            > i = i /base + 1;
                            > }
                            > } while(i);[/color]
                            // > reverse(str.beg in(), str.end());
                            std::reverse(st r.begin(), str.end());

                            // prints "-16 = 10-" for base==16

                            // > cout << str << endl;
                            return str;[color=blue]
                            > } // CalcBase(int,in t)
                            >
                            >int main(){[/color]

                            try{
                            for(int i(-55); i <= 55; ++i){
                            // > cout << i << " = ";
                            // > printBase( i, -10 );
                            std::cout << i <<" = "<<CalcBase ( i, -10 )<<std::endl;
                            [color=blue]
                            > } // for(i)[/color]
                            } // try
                            catch(std::out_ of_range &Oor){
                            std::cerr<<"cau ght "<<Oor.what()<< std::endl;
                            return EXIT_FAILURE;
                            }
                            catch(...){
                            std::cerr<<"cau ght ... something"<<std ::endl;
                            return EXIT_FAILURE;
                            }
                            return 0;
                            [color=blue]
                            > } // main()end
                            >
                            >Thanks and Regards,
                            >manoj.[/color]

                            // - output - (base==-10)
                            -55 = 65
                            -54 = 66
                            [snip]
                            -11 = 29
                            -10 = 10
                            -9 = 11
                            -8 = 12
                            [snip]
                            -2 = 18
                            -1 = 19
                            0 = 0
                            1 = 1
                            [snip]
                            8 = 8
                            9 = 9
                            10 = 190
                            11 = 191
                            [snip]
                            54 = 154
                            55 = 155


                            And your C++ problem is?

                            --
                            Bob R
                            POVrookie


                            Comment

                            • Phlip

                              #15
                              Re: Please comment on my integer to string code.

                              BobR wrote:
                              [color=blue]
                              > catch(...){
                              > std::cerr<<"cau ght ... something"<<std ::endl;
                              > return EXIT_FAILURE;
                              > }[/color]

                              I know a certain lead programmer driven to distraction by code that relied
                              on catch(...). The advice is let the program halt; use a debugger to fix
                              all root causes, and stress-test applications to force out these risks.

                              And catch(...) only catches hardware crashes on Win32 as a compiler-specific
                              extension. Other platforms just dump core, meaning they cannot use that
                              trick to maintain their work loops and keep running.

                              --
                              Phlip
                              http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

                              Comment

                              Working...