473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Value-return functions questions???

2
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.
Oct 10 '06 #1
4 2128
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. float halveNumber(int a)
  3. {
  4.    float result;
  5.    result = a / 2;
  6.    return result;
  7.  
  8.    // Could probably write this easier as simply
  9.    // return a / 2;
  10.    // but I wanted to show the operation
  11. }
  12.  
  13.  
2.

Expand|Select|Wrap|Line Numbers
  1.  
  2. char getChar()
  3. {
  4.    char result;
  5.    cin << result;
  6.    return result;
  7. }
  8.  
  9.  
Hope that helps.
Oct 10 '06 #2
MT20
2
Thank you!

Okay, here we go:

1.

Expand|Select|Wrap|Line Numbers
  1.  
  2. float halveNumber(int a)
  3. {
  4.    float result;
  5.    result = a / 2;
  6.    return result;
  7.  
  8.    // Could probably write this easier as simply
  9.    // return a / 2;
  10.    // but I wanted to show the operation
  11. }
  12.  
  13.  
2.

Expand|Select|Wrap|Line Numbers
  1.  
  2. char getChar()
  3. {
  4.    char result;
  5.    cin << result;
  6.    return result;
  7. }
  8.  
  9.  
Hope that helps.
Oct 10 '06 #3
float halvevalue(int i)
{
float a;
a=i/2;
return a;
}


note--> a should be return to a flaot variable
Oct 13 '06 #4
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. float halveNumber(int a)
  2. {
  3.    float result;
  4.    result = a / 2;
  5.    return result;
  6.  
  7.    // Could probably write this easier as simply
  8.    // return a / 2;
  9.    // but I wanted to show the operation
  10. }
  11.  
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

Expand|Select|Wrap|Line Numbers
  1. float halveNumber(int a)
  2. {
  3.    float result;
  4.    result = a / 2.F;
  5.    return result;
  6.  
  7. }
  8.  
Oct 13 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: matthew_carver | last post by:
Hello, I have an ASP page that loops through a SQL Server 2000 table, then downloads an Excel sheet the users can save, etc. Works fine, except, I see that in one particular "comments" field the...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
2
by: Joey | last post by:
Hi There, I am trying to get the selected value of a listbox when I click a button, everything works ok and I can bind the list and when I have a basic page and click a button to invoke a sub it...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
8
by: dotnetnoob | last post by:
EvtEnrollAttr.Value = "3748" EvtEnrollElem.Attributes.Append(EvtEnrollAttr) i need the attr value to be <stuff ID = '3748'> in single quote how can i do that with visual basic.net? thanks
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
1
by: cbellew | last post by:
Hi guys, I have a problem with an option group and a two corresponding text boxes. When the user chooses an option value i want the text boxes to populate with text dependent on the choice made....
3
by: Christof Warlich | last post by:
Hi all, in the following example, Index<unsigned int x>::value allows to calculate the "rounded up" index from any "increasing" value during compile time. Unfortunately, the definition of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.