473,503 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is my c++ code incorrect?

2 New Member
Here's my interpretation of what my code should be doing. First it will ask me to enter the tolerance 'x'. (Relative error test 'f' should be less than the tolerance for the iteration be terminated.) then ask for the range wherein the root is located. let's say the domain is given [1,2]. Afterwards, it will go inside loop and get the respective values of 'fa' and 'fb'. Then the values will be transferred to 'c' which will then be used in 'fc'. The error test 'g' will have its own value and then be tested on the first "if statement (g<=x)". if the statement is false, it most likely will go to the else statement and satisfy the conditions inside. Before it exits the loop, the variable 't' will get 'c' present value. then the program will execute the loop again then 'c' and 't' have different values. Thanks for replying! Also, I am to get the root of the polynomial by means of false position method.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. #include <cmath>
  5.  
  6. int main ()
  7. {
  8.     double a,b,fa,fb,fc,c,f,x,t,g;
  9.     int max=10;
  10.     int i;
  11.     cout<<"Enter tolerance: ";
  12.     cin>>y;
  13.     cout<<"Enter starting values (domain):"<<endl;
  14.     cout<<"a= ";
  15.     cin>>a;
  16.     cout<<"b= ";
  17.     cin>>b;
  18.  
  19.     fa=1.0;
  20.     fb=1.0;
  21.     t=1.0;
  22.     c=1.0;
  23.     fc=1.0;
  24.     f=1.0;
  25.     g=1.0;
  26.  
  27.     for (i=0;i<=max;i++)
  28.  
  29.     {
  30.         fa=a*a*a-3*a-5;
  31.         fb=b*b*b-3*b-5;
  32.         t=1.0;
  33.         c=(b*fa-a*fb)/(fa-fb);
  34.         fc=c*c*c-3*c-5;
  35.         f=(c-t)/c;
  36.         g=fabs(f);
  37.  
  38.         if (g <= y)
  39.  
  40.             break;
  41.  
  42.         else 
  43.             if (fa*fc > 0)
  44.             {
  45.                 t=c;
  46.                 a=c;
  47.             }
  48.             else
  49.             {
  50.                 t=c;
  51.                 b=c;
  52.             }
  53.  
  54.     }
  55.     cout<<c;
  56.  
  57.  
  58.  
  59.         system ("pause");
  60.         return 0;
  61. }
Jul 28 '11 #1
1 1853
weaknessforcats
9,208 Recognized Expert Moderator Expert
You don't say what is incorrect.

Debugging is part of software development so this represents an opportunity to learn how to use the debugger that comes with your compiler.

Short of that you can, as a beginner, insert cout statements in the oode to show what's happening ang the way. I would do this if it's not possible to use the debugger.
Jul 28 '11 #2

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

Similar topics

25
3694
by: delerious | last post by:
I see some code examples like this: <DIV onmouseover="this.style.background='blue'"> and other code examples like this: <DIV onmouseover="javascript:this.style.background='blue'"> Which...
1
3914
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I get...
3
3324
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I...
19
2236
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
4
2727
by: Peter Ritchie | last post by:
Does anyone know how to suppress a specific warning for a line or block of code in C#? C++ has a nice facility to disable a warning for a block of code with #pragma for warnings that are incorrect...
0
8290
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
10
3383
by: arial | last post by:
Hi, I am getting this error message: Incorrect syntax near the keyword 'where'. Description: An unhandled exception occurred during the execution of the current web request. Please review...
3
1718
lee123
by: lee123 | last post by:
I have a problem getting the correct to count +1 every time I get an answer right and the incorrect is the same. I have two lbl's named number1 and number2 which produces a Rnd# in each lbl. ...
0
4077
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
144
4849
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
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
7271
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,...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.