Value-return functions questions???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MT20
    New Member
    • Oct 2006
    • 2

    Value-return functions questions???

    These are two questions from my book I just can't figure out.
    Write the code for:
    1. a function that recieves an integer passed to it, function named halveNumber(), should divide the integer by 2, the return the result(which could be a decimal place)

    2. a function that prompts the user to enter a character, stores the user's response in char variable, should return character entered by the user, name of function is getChar().

    I am just not understanding how to write these and the Prof isn't help at all.
  • apusateri
    New Member
    • Oct 2006
    • 27

    #2
    Originally posted by MT20
    These are two questions from my book I just can't figure out.
    Write the code for:
    1. a function that recieves an integer passed to it, function named halveNumber(), should divide the integer by 2, the return the result(which could be a decimal place)

    2. a function that prompts the user to enter a character, stores the user's response in char variable, should return character entered by the user, name of function is getChar().

    I am just not understanding how to write these and the Prof isn't help at all.
    Okay, here we go:

    1.

    Code:
    float halveNumber(int a)
    {
       float result;
       result = a / 2;
       return result;
       
       // Could probably write this easier as simply
       // return a / 2;
       // but I wanted to show the operation
    }
    2.

    Code:
    char getChar()
    {
       char result;
       cin << result;
       return result;
    }
    Hope that helps.

    Comment

    • MT20
      New Member
      • Oct 2006
      • 2

      #3
      Thank you!

      Originally posted by apusateri
      Okay, here we go:

      1.

      Code:
      float halveNumber(int a)
      {
         float result;
         result = a / 2;
         return result;
         
         // Could probably write this easier as simply
         // return a / 2;
         // but I wanted to show the operation
      }
      2.

      Code:
      char getChar()
      {
         char result;
         cin << result;
         return result;
      }
      Hope that helps.

      Comment

      • ronitonline2006
        New Member
        • Oct 2006
        • 3

        #4
        float halvevalue(int i)
        {
        float a;
        a=i/2;
        return a;
        }


        note--> a should be return to a flaot variable

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by apusateri
          Code:
          float halveNumber(int a)
          {
             float result;
             result = a / 2;
             return result;
             
             // Could probably write this easier as simply
             // return a / 2;
             // but I wanted to show the operation
          }
          Unfortunately this doesn't work, because a is a int and the constant 2 is an int the division is done as integer arithmatic so if a odd number is supplied the answer returned is wrong;

          halveNumber(5) returns 2

          You need to make one of the operands to / a float so that the division is done in floating point like so

          Code:
          float halveNumber(int a)
          {
             float result;
             result = a / 2.F;
             return result;
          
          }

          Comment

          Working...