1. Hey Guest Did you know you can win an Honor 10 phone worth ₹33,000 and an additional ₹70,000 in paytm vouchers, just by replying to some threads and taking part in the discussions happening in the Honor Hub?

    What are you waiting for? Start commenting and start winning! Remember to read the instructions posted here.

    Dismiss Notice

can you tell me what mistake i made?

Discussion in 'QnA (read only)' started by geekgod, Aug 26, 2004.

Thread Status:
Not open for further replies.
  1. geekgod

    geekgod New Member

    Joined:
    Aug 18, 2004
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    hi, i was writing a c program for my class assignment, and i got stuck. i paste it below.
    it appears to me that the program structure is ok, but when i run it, it does'nt give any output. i have also tried by taking the last 'printf' statement out of the for loop, but then the out put is not what i expected. it prints the value of x as -10 continously 50 times. :( :( :(.
    help plz!! :oops: :oops:
     
  2. OP
    OP
    geekgod

    geekgod New Member

    Joined:
    Aug 18, 2004
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    ohhh, sorry! i gave the heading wrong. the equatin is x^2-4*x-10=0.
     
  3. vswizard

    vswizard New Member

    Joined:
    May 9, 2004
    Messages:
    60
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Cyber City
    This is wat i used

    // Prog for Bisection method

    #include<math.h>
    void main()
    {
    float fun(float x);
    void bis(float *x,float a , float b, int *itr);
    int itr=0,maxit;
    float x,a,b,aerr,x1;

    clrscr();
    printf("\n\n\nEnter Value of a ");
    scanf("%f",&a);
    printf("\nEnter Value of b ");
    scanf("%f",&b);
    printf("\nEnter Value of allowed error ");
    scanf("%f",&aerr);
    printf("\nEnter Maximum Iteration allowed ");
    scanf("%d",&maxit);

    bis(&x,a,b,&itr);

    do
    {

    if(fun(a)*fun(x)<0)
    {b=x;}
    else
    {a=x;}

    bis(&x1,a,b,&itr);

    if(fabs(x1-x)<aerr)
    {
    printf("\n After %d Iterations ,Root is %.4f ",itr,x1);
    getch();
    exit(0);
    }

    x=x1;
    }

    while(itr<maxit);
    {
    printf("\n Solution Does not converge after %d iteration",maxit);
    }
    getch();
    exit(1);

    }

    float fun(float x)
    {
    return ((x*x*x)-(x)-11);
    }

    void bis(float *x,float a,float b,int *itr)
    {
    *x=(a+b)/2;
    ++(*itr);
    printf("\n Iteration number %3d x = %.4f ",*itr,*x);
    }
     
  4. Satan_Rulez

    Satan_Rulez New Member

    Joined:
    Aug 9, 2004
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Mumbai
    I had to program in Pascal for CONM.
    Which university r u from?
     
  5. technomodel

    technomodel New Member

    Joined:
    Dec 23, 2003
    Messages:
    123
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    kolkata
    i think ur program is okay. just change x1 to x0 in the final output statement, coz what u need is the value of x0, and x1 will takethat value only if a particular condition is satisfied (f1*f0>0). also the equation you chose appears to have distributed roots, so you will have to try a number of ranges before u get a proper answer.
     
  6. IG

    IG New Member

    Joined:
    Mar 17, 2004
    Messages:
    188
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    dood....dont tell me ur in eee as in electrical and electronics engg....
     
  7. sandeep8000861

    sandeep8000861 New Member

    Joined:
    Jul 7, 2004
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Bangalore
    Thats pretty clear explanation. Follow it up.
     
  8. aadipa

    aadipa New Member

    Joined:
    Feb 12, 2004
    Messages:
    997
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Palghar, Mumbai
    here r my code

    1 Bisection Method
    Code:
    /*
     * Find root of equation xýSin(x)-xCos(x)+5 = 0 using bisection method
     * upto 6 decimal places
     *
     * GUESS 1 & 2
     */
    
    #include "math.h"
    
    double function1(double);
    
    void main() {
      int i;
      float a=0,b=0,c,fa,fb,fc;
      long n,nc=0;
    
      start:
    
      clrscr();
      printf("\n   -----------------------------------------------------------");
      printf("\n   -------------------- BISECTION METHOD  --------------------");
      printf("\n   -------------- y = f(x) = xýSin(x)-xCos(x)+5 --------------");
      printf("\n   -----------------------------------------------------------");
    
      printf("\nEnter your guesses\
      \n\nFirst guess\t: ");
      scanf("%f",&a);
      printf("\nSecond guess\t: ");
      scanf("%f",&b);
      fa=function1(a);
      fb=function1(b);
      if(fa*fb > 0) {
        printf("\n\nWrong Guesses");
        printf("\na=%f, f(a)=%f",a,fa);
        printf("\nb=%f, f(b)=%f",b,fb);
        getch();
        goto start;
        }
    
      printf("\n\nNo. of iterations : ");
      scanf("%ld",&n);
    
      while(1) {
        c=(a+b)/2;
        nc++;
        if(nc > n) break;
        fc=function1(c);
        fa=function1(a);
        fb=function1(b);
    
        printf("\nn=%02ld a=%+f b=%+f c=%+f fa=%+f fb=%+f fc=%+f",nc,a,b,c,fa,fb,fc);
    
        if(fa*fc <= 0)
         b=c;
        else
         if(fb*fc <= 0)
          a=c;
    
        if(fabsl(a-b) < 1e-6) break;
        }
      printf("\n\nFinal Value of root is \t  c =%+.10f,\n\t\t\tf(c)=%+.10f",c,fc);
      printf("\nSolution converges to root after %ld iterations",nc);
      }
    
    
    double function1(double x) {
      return(x*x*sin(x)-x*cos(x)+5);
      }
    
    2. NR Method
    Code:
    /*
     * Find root of equation x^3-2x+5 = 0 by Newton Raphson method upto
     * five decimal places.
     *
     * GUESS = -2.1
     */
    
    #include "math.h"
    
    float f(float);
    float fd(float);
    void main(void);
    
    float f(float a) {
     return(a*a*a-2*a+5);
     }
    
    float fd(float a) {
     return(3*a*a-2);
     }
    
    void main(void) {
     float a,nr,dr,p;
     float const error=1e-5;
     int n,s;
    
     start:
     clrscr();
    
      printf("\n   -----------------------------------------------------------");
      printf("\n   ------------------ NEWTON RAPHSON METHOD ------------------");
      printf("\n   ------------------- y = f(x) = x^3-2x+5 -------------------");
      printf("\n   -----------------------------------------------------------");
    
     printf("\n Enter your guess :: ");
     scanf("%f",&a);
     nr=f(a);
     dr=fd(a);
     n=1;
     p=a;
     printf("\n Enter max iterations :: ");
     scanf("%d",&s);
    
     while(n<=s) {
      if(dr==0) {
       printf("\nERROR! Division by zero.");
       getch();
       goto start;
       }
      printf("\n n=%2d a=%+f fa=%+f f'a=%f",n,a,nr,dr);
      a-=(nr/dr);
      nr=f(a);dr=fd(a);n++;
      if( fabs(a-p) < error ) break;
      p=a;
      printf(" a=>>%+f",a);
      }
    
     printf("\n Root = %f\n Iterations = %d",a,n-1);
     getch();
     }
    
     
  9. aadipa

    aadipa New Member

    Joined:
    Feb 12, 2004
    Messages:
    997
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Palghar, Mumbai
    i even wrote a code to show how the solution converges to root graphically using Turbo C. I don't have that code, i was in my college and i am sure that college hdd must had be formatted atleast once in last 4 years otherwise i would have given that code too.
     
  10. Satan_Rulez

    Satan_Rulez New Member

    Joined:
    Aug 9, 2004
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Mumbai
    Guys i want to program a small application.
    Like i want to write a letter as an exe file.
    When downloaded n opened it should popup a dos window n the
    text should appear one character at a time with a very short delay.
    Can anyone tell me how to in Basic cos currently i dont have any c compiler.
     
Thread Status:
Not open for further replies.

Share This Page