473,387 Members | 1,844 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,387 software developers and data experts.

help with a floor program

29
hi
i have a question..
An application of function floor ia rounding a value to the nearest integer. the statement y=floor(x+0.5); rounds the number x to the nearest integer and assigns the result to y. how can we write a program that reads several numbers and uses the preceeding statement to round each of these numbers to the nearest integer. for each number proceeded, print both the original number and the rounded number.
Jan 17 '07 #1
11 4255
Ganon11
3,652 Expert 2GB
And your question is...?
Jan 17 '07 #2
elsa
29
how can we write a program that reads several numbers and uses the preceeding statement to round each of these numbers to the nearest integer?
Jan 17 '07 #3
horace1
1,510 Expert 1GB
how can we write a program that reads several numbers and uses the preceeding statement to round each of these numbers to the nearest integer?
you could have a loop which reads the value of x from the keyboard, calculates y using y=floor(x+0.5); and prints x and y
Jan 17 '07 #4
elsa
29
i have tried this..but it is not working
#include <iostream>
# include<math.h>

using std::cout;
using std::cin;
using std::endl;

//declaration of function
double floor(double);

int main()
{
unsigned long a;
do
{
cout<<"Rounding to the nearest integer"<<endl;
cout<<"Please enter a decimal btween -100 and 100 "<<endl;
cin>>a ;
floor(a);
}while(a>=-100|| a<=100);
cout<<"you choose to quit\a\a\a"<<endl;
return 0;
}
//definition of function

double floor (double a)
{
a=floor(a+0.5);
cout<<"the floor of the number "<<a<<" is "<<a<<endl;
return a;
}
Jan 17 '07 #5
horace1
1,510 Expert 1GB
you cannot call your function the same name as the floor() in the maths library. Change its name - also should the variable a that you read in be a double rather than an int?
Jan 17 '07 #6
elsa
29
i changed its name..but still it is not working..and i didnt get the second part of what u said about changing a into a double rather than the int..where is that? plz help me..im going crazy with this c++..lol
Jan 17 '07 #7
horace1
1,510 Expert 1GB
i changed its name..but still it is not working..and i didnt get the second part of what u said about changing a into a double rather than the int..where is that? plz help me..im going crazy with this c++..lol
I assume you want to convert a real value (with a fractional component) to the nearest integer, which is what floor(a+0.5) does.
see if this does what you require
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. # include<math.h>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. //declaration of function
  9. double myfloor(double);
  10.  
  11. int main()
  12. {
  13. double a;  // ** make double
  14. do
  15. {
  16. cout<<"Rounding to the nearest integer"<<endl;
  17. cout<<"Please enter a decimal btween -100 and 100 "<<endl;
  18. cin>>a ;
  19. myfloor(a);
  20. }while(a>=-100|| a<=100);
  21. cout<<"you choose to quit\a\a\a"<<endl;
  22. return 0;
  23. }
  24. //definition of function
  25.  
  26. double myfloor (double a)
  27. {
  28. double b=floor(a+0.5);
  29. cout<<"the floor of the number "<<a<<" is "<<b<<endl;
  30. return b;
  31. }
  32.  
  33.  
when run it gives (user input in bold)
Rounding to the nearest integer
Please enter a decimal btween -100 and 100
67.3
the floor of the number 67.3 is 67
Rounding to the nearest integer
Please enter a decimal btween -100 and 100
67.7
the floor of the number 67.7 is 68
Rounding to the nearest integer
Please enter a decimal btween -100 and 100
Jan 17 '07 #8
elsa
29
thx alot for ur help..but i wanted to ask u something..why is the program not stopping when i write 100 or -100? i need to solve this problem..
Jan 17 '07 #9
LSB
8
thx alot for ur help..but i wanted to ask u something..why is the program not stopping when i write 100 or -100? i need to solve this problem..
I think it may be that sign "=" in your "while" statement, right?
Jan 17 '07 #10
elsa
29
oh yes..lol..im so crazy today..thx alot for ur help..
Jan 17 '07 #11
elsa
29
i should have written &&(and) instead of ||(or)..
Jan 17 '07 #12

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
2
by: TSK | last post by:
I cannot get my focus() fn to work in my validateInput(userInput) fn. It will not allow the user to go back and correct invalid input like it should. Instead it goes right on through with the...
2
by: Ernst Berg | last post by:
Thankfully there is a C language group! Hello! I am a hobyist who is rediscovering writing C programs on Linux. I use GCC and I am at a loss as to why the compile fails to find the...
38
by: Jordi | last post by:
I have made a little C program that generates the following output: 227.000000 / 5 = 45.400002 Here's the code: int main (int argc, char* argv) { float var = 227; printf("%f / 5 = %f\n", var,...
21
by: BWIGLEY | last post by:
Basically I've just started making a game. So far it makes an array 25 by 20 and tries to make five rooms within it. In scr_make_room() there's parse errors: 20 C:\c\Rooms\Untitled1.c parse error...
0
by: elsa | last post by:
the question that i have to solve says: fuction floor can be used to round a number to a specific decimal place. the statement y=floor(x*10+0.5)/10; rounds x to the tenths position. write a program...
5
by: john.leidel | last post by:
In doing some work on a library I'm writing, I'm trying to use the `floor()` function to get the largest integral value of a variable. I'm compiling everything using the mpich mpicc built with gcc...
6
by: MikeY | last post by:
It seems that when I call the Timer.Stop(). That my main form does not appear correctly. All Added Controls, label box's, buttons are missing. Hopefully someone will have a simple answer: My code...
0
by: stanlew | last post by:
Happy New Year everyone! I'm new to both T-SQL and this forum. I'm currently doing an internship and my first task was to create a small program which will send an email detailing the sales of the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.