Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 11th, 2012, 01:48 AM
 absid
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 38 m 10 sec
Reputation Power: 0
Fault that cannot be detected

The following is a program on solving an equation by bisection method.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int i=0,count=0,flag=0;
float x1,x2,x3=0;
float fx1=0,fx2=0,fx3=0,t=0;
float func(float f)
{
return(log10(f)-cos(f));
}
int check()
{
printf("\n\t INITIAL x1= ");
scanf("%f",&x1);
printf("\n\t INITIAL x2= ");
scanf("%f",&x2);
fx1=fx2=fx3=0.0;
if((func(x1)*func(x2))>0)
{
printf("\n\t INITIAL VALUES ARE NOT PERFECT.");
return(1);
}
return(0);
}
void main()
{
clrscr();
printf("\n\n\t\t PROGRAM FOR BISECTION METHOD");
printf("\n\n\t EQUATION:::::LOG(X)-COS(X)");
while(1)
{
if (check()==0)
{
flag=1;
break;
}
check();
}
printf("\n ***********************************************************");
printf("\n ITERATION X1 FX1 X2 FX2 X3 FX3 ");
printf("\n ***********************************************************");
if (flag==1)
{
while ((fabs(x2-x1))>=0.0001)
{
count++;
fx1=fx2=fx3=0;
fx1=func(x1);
fx2=func(x2);
printf ("\n %d %.3f %.3f %.3f %3f %.3f %.3f",count,x1,fx1,x2,fx2,x3,fx3);
t= fx1*fx3;
if (t>0)
{
x1=x3;
}
if (t<0)
{
x2=x3;
}
}
printf("\n\t ROOT OF EQUATION IS %f::::",x3);
}
getch();
}
I have found this on the Net. An output is also given which I am not getting. Can someone tell me what is going wrong.

#2
November 11th, 2012, 02:20 AM
 Astrodude
Contributing User

Join Date: Oct 2012
Posts: 37
Time spent in forums: 7 h 37 m 25 sec
Reputation Power: 5
Not quite sure, but where is fx3 used once it's initialized to zero? I think that's the problem. fx3 is permanently zero.
Lux Perpetua agrees!

#3
November 11th, 2012, 02:52 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
You should really try to understand this program that you've downloaded from somewhere on the web. As you are finding out now if you didn't already know, stuff found on the internet can be pretty unreliable. Astrodude's comment is valid, and that's not even the most serious bug in the code, which is that this program doesn't actually implement bisection (or anything useful, actually). On top of that, this program follows a number of bad coding practices ("void main()", questionable use of global variables, superfluous use of conio.h in what should be a pure console program, among others).

By the way, next time you post code on this forum, make sure to put it in [code][/code] tags. As you can see, the formatting of the code in your post has not been preserved.

#4
November 11th, 2012, 03:26 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,258
Time spent in forums: 2 Months 4 Weeks 1 Day 14 h 21 m 32 sec
Reputation Power: 1827
Ah yes, yet another here is some found code I don't understand poster.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#5
November 11th, 2012, 09:15 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
Unsurprisingly, the attribution has been removed from the code. Surprisingly, so have the two lines of the algorithm that give it the name "bisection."
salem agrees: LOL - one edit too far, unbelievable.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Fault that cannot be detected