1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Program for round-off error

  1. Mar 11, 2009 #1
    1. The problem statement, all variables and given/known data

    Not a homework just self-teaching. I want to write a program which determines the round-off error of a number.

    2. Relevant equations

    re = Abs(x-x*)<= 0.5x10(n-2)

    x*-0.5x10(n-2) < x < x* + 0.5x10(n-2)

    3. The attempt at a solution

    #include <iostream>
    #include <cmath>
    using namespace std;

    int k, n; //Declares variables k and n.
    double x, x_ro, sub_x, sup_x; //Declares variables x and x*.

    int main()
    {
    cout << "Enter a number.\n";
    cin >> x;
    cout << "Enter the desired number of significant digits.\n";
    cin>> k;
    cout << "Enter the rounded-off number.\n";
    cin >> x_ro;

    int n;
    for (n == 0; n == -10; n--)
    {
    sub_x == x_ro - .5 * pow(10, n - k);
    sup_x == x_ro + .5 * pow(10, n - k);
    cin >> n;

    if (x >= sub_x && x <= sup_x) //To check that condition is met
    {

    break;
    cout << "n = " << n;
    }
    }

    for (n == 0; n == 10; n++)
    {
    sub_x == x_ro - .5 * pow(10, n - k);
    sup_x == x_ro + .5 * pow(10, n - k);
    cin >> n;

    if (x >= sub_x && x <= sup_x)
    {

    break;
    cout << "n = " << n;
    }

    }

    return 0;
    }
     
  2. jcsd
  3. Mar 13, 2009 #2
    No one can offer even a suggestion? Maybe my code is a bit confusing.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook