How to Determine Round-Off Error in a C++ Program?

  • Thread starter Thread starter ZachN
  • Start date Start date
  • Tags Tags
    Error Program
AI Thread Summary
The discussion focuses on creating a C++ program to determine the round-off error of a number based on user input for the number, desired significant digits, and the rounded-off number. The user outlines the mathematical equations for calculating round-off error and attempts to implement these in code. However, there are issues in the code, such as incorrect use of comparison operators and the loop structure, which need correction for proper functionality. The user expresses frustration over the lack of feedback or suggestions from others regarding their code. Overall, the thread highlights challenges in coding and seeking assistance in programming tasks.
ZachN
Messages
29
Reaction score
0

Homework Statement



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

Homework Equations



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

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

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;
}
 
Physics news on Phys.org
No one can offer even a suggestion? Maybe my code is a bit confusing.
 

Similar threads

Replies
2
Views
3K
Replies
3
Views
1K
Replies
6
Views
3K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
14
Views
4K
Back
Top