help with a floor program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elsa
    New Member
    • Jan 2007
    • 29

    help with a floor program

    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    And your question is...?

    Comment

    • elsa
      New Member
      • Jan 2007
      • 29

      #3
      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?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by elsa
        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

        Comment

        • elsa
          New Member
          • Jan 2007
          • 29

          #5
          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"<<en dl;
          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;
          }

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            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?

            Comment

            • elsa
              New Member
              • Jan 2007
              • 29

              #7
              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

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by elsa
                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
                Code:
                #include <iostream>
                # include<math.h>
                
                using std::cout;
                using std::cin;
                using std::endl;
                
                //declaration of function
                double myfloor(double);
                
                int main()
                {
                double a;  // ** make double
                do
                {
                cout<<"Rounding to the nearest integer"<<endl;
                cout<<"Please enter a decimal btween -100 and 100 "<<endl;
                cin>>a ;
                myfloor(a);
                }while(a>=-100|| a<=100);
                cout<<"you choose to quit\a\a\a"<<endl;
                return 0;
                }
                //definition of function
                
                double myfloor (double a)
                {
                double b=floor(a+0.5);
                cout<<"the floor of the number "<<a<<" is "<<b<<endl;
                return b;
                }
                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

                Comment

                • elsa
                  New Member
                  • Jan 2007
                  • 29

                  #9
                  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..

                  Comment

                  • LSB
                    New Member
                    • Jan 2007
                    • 8

                    #10
                    Originally posted by elsa
                    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?

                    Comment

                    • elsa
                      New Member
                      • Jan 2007
                      • 29

                      #11
                      oh yes..lol..im so crazy today..thx alot for ur help..

                      Comment

                      • elsa
                        New Member
                        • Jan 2007
                        • 29

                        #12
                        i should have written &&(and) instead of ||(or)..

                        Comment

                        Working...