need to compile this ( trying to find roots of a bisection)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anand

    need to compile this ( trying to find roots of a bisection)

    *************** *************** *************** *************** *************** *************** *************** **

    #include<stdio. h>
    #include<conio. h>
    #include<math.h >

    void main()
    {
    double a,b,c,fa,fb,fc, err;
    int count;
    clrscr();
    printf("\n enter value for %lf",a);
    scanf("%lf",&a) ;
    printf("\n enter value for %lf",b);
    scanf("%lf",&b) ;
    err = exp(-6);
    printf("\n value of %lf",err);
    while(fabs(a-b)<=err)
    {
    fa=2-(3*sin(a)*exp(a ));
    printf("\n the value of %lf",fa);
    fb=2-(3*sin(b)*exp(b ));
    printf("\n the value of %lf",fb);
    c=(a+b)/2;
    fc=(fa+fb)/2;
    If (fa!=0)
    {
    If ((fa>0)&&(fb>0) )
    a = c;
    else
    b = c;
    }
    Else
    {
    printf("\n the value of fa does not exist, proceed with fb");
    }

    If (fb != 0)
    {
    If (fb > 0)
    If (fc > 0)
    b = c;
    else
    a = c;
    }
    Else
    {
    printf ("\n fa,fb do not exist, cannot proceed");
    }
    count++
    }
    printf("\n the value of %lf",a);
    printf("\n the value of %lf",b);
    printf("\n the value of %d",count);
    }



    *************** *************** *************** *************** *************** *************** *************** *

  • Michael Mair

    #2
    Re: need to compile this ( trying to find roots of a bisection)

    anand schrieb:[color=blue]
    > *************** *************** *************** *************** *************** *************** *************** **[/color]
    Please state your problem in the message text, too.
    BTW: "need to compile this ( trying to find roots of a bisection)"
    is not very helpful.
    [color=blue]
    > #include<stdio. h>
    > #include<conio. h>[/color]

    This is not a standard C header, so I will ignore it and
    all functions which have no prototype in one of the other
    included headers.
    [color=blue]
    > #include<math.h >
    >
    > void main()[/color]

    This is the wrong type for main().
    Write either
    int main (void)
    or
    int main (int argc, char **argv)
    Read

    Question 11.12a through 11.15[color=blue]
    > {
    > double a,b,c,fa,fb,fc, err;
    > int count;
    > clrscr();[/color]

    Unknown function.
    [color=blue]
    > printf("\n enter value for %lf",a);[/color]

    In C89, using %lf in printf() leads to undefined behaviour.

    Even if you used %f, %e, %g, you still are passing an
    uninitialized variable as argument.
    So you luck out in every possible way. Your program is already
    dead.
    [color=blue]
    > scanf("%lf",&a) ;[/color]

    Note: 1)If you want the preceding printf() to be printed out,
    either terminate it by '\n' or use fflush(stdout) -- otherwise,
    the output prompt might show up after the input it should
    precede.
    2) scanf() is not really good for interactive input.
    Read the comp.lang.c FAQ on this:


    [color=blue]
    > printf("\n enter value for %lf",b);
    > scanf("%lf",&b) ;[/color]

    dito.
    [color=blue]
    > err = exp(-6);
    > printf("\n value of %lf",err);
    > while(fabs(a-b)<=err)
    > {
    > fa=2-(3*sin(a)*exp(a ));
    > printf("\n the value of %lf",fa);
    > fb=2-(3*sin(b)*exp(b ));
    > printf("\n the value of %lf",fb);
    > c=(a+b)/2;
    > fc=(fa+fb)/2;
    > If (fa!=0)[/color]

    Here you are not even trying anymore.
    C is a case sensitive language, so you have to stay
    with "if", "else", ...[color=blue]
    > {
    > If ((fa>0)&&(fb>0) )
    > a = c;
    > else
    > b = c;
    > }
    > Else
    > {
    > printf("\n the value of fa does not exist, proceed with fb");
    > }
    >
    > If (fb != 0)
    > {
    > If (fb > 0)
    > If (fc > 0)
    > b = c;
    > else
    > a = c;
    > }
    > Else
    > {
    > printf ("\n fa,fb do not exist, cannot proceed");
    > }
    > count++
    > }
    > printf("\n the value of %lf",a);
    > printf("\n the value of %lf",b);
    > printf("\n the value of %d",count);[/color]

    return 0;[color=blue]
    > }[/color]

    I did not look at the algorithm or at the loop contents.
    Fix the above first and describe further problems.

    Reading the whole comp.lang.c FAQ may help you avoid other
    errors.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Martin Ambuhl

      #3
      Re: need to compile this ( trying to find roots of a bisection)

      anand wrote in the subject line "need to compile this ( trying to find
      roots of a bisection)" followed by illiterate garbage claiming to be C
      [mercifully snipped].

      No, you need
      1) To open your C text to page 1 and start over and
      2) learn to not put an important part of your post in the headers, where
      many people will ignore it.

      Comment

      • CBFalconer

        #4
        Re: need to compile this ( trying to find roots of a bisection)

        anand wrote:[color=blue]
        >
        > *************** *************** *************** *************** *************** *************** *************** **
        >
        > #include<stdio. h>
        > #include<conio. h>
        > #include<math.h >
        >
        > void main()
        > {
        > double a,b,c,fa,fb,fc, err;
        > int count;
        > clrscr();[/color]

        It won't compile. There is no such header file as conio.h. There
        is no such routine as clrscr(). main returns an int, not void. I
        did not look further.

        At least in C. If you are using some other language with vague
        similarities to C you need to use a newsgroup that deals with that
        language. This is not it.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>
        Also see <http://www.safalra.com/special/googlegroupsrep ly/>

        Comment

        • osmium

          #5
          Re: need to compile this ( trying to find roots of a bisection)

          "anand" writes:

          *************** *************** *************** *************** *************** *************** *************** **[color=blue]
          >
          > #include<stdio. h>
          > #include<conio. h>
          > #include<math.h >
          >
          > void main()
          > {
          > double a,b,c,fa,fb,fc, err;
          > int count;
          > clrscr();
          > printf("\n enter value for %lf",a);
          > scanf("%lf",&a) ;
          > printf("\n enter value for %lf",b);
          > scanf("%lf",&b) ;
          > err = exp(-6);
          > printf("\n value of %lf",err);
          > while(fabs(a-b)<=err)
          > {
          > fa=2-(3*sin(a)*exp(a ));
          > printf("\n the value of %lf",fa);
          > fb=2-(3*sin(b)*exp(b ));
          > printf("\n the value of %lf",fb);
          > c=(a+b)/2;
          > fc=(fa+fb)/2;
          > If (fa!=0)[/color]

          Use cut and paste, not retype. There is no reason in the world to expect
          that to compile. The magic word is "if", not "If". If you repost tell us
          what happens. Did it compile? Don't imply something or other by saying
          "Need to compile".

          <snip>


          Comment

          • Keith Thompson

            #6
            Re: need to compile this ( trying to find roots of a bisection)

            CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
            > anand wrote:[/color]
            [...][color=blue][color=green]
            >> #include<stdio. h>
            >> #include<conio. h>
            >> #include<math.h >
            >>
            >> void main()
            >> {
            >> double a,b,c,fa,fb,fc, err;
            >> int count;
            >> clrscr();[/color]
            >
            > It won't compile. There is no such header file as conio.h. There
            > is no such routine as clrscr(). main returns an int, not void. I
            > did not look further.
            >
            > At least in C. If you are using some other language with vague
            > similarities to C you need to use a newsgroup that deals with that
            > language. This is not it.[/color]

            As I'm sure you know, there is a header file called conio.h and a
            function called clrscr() *on some systems*. (Of course, there's no
            reason to use either in a program that just performs some mathematical
            operations.)

            Declaring "void main()" makes it incorrect C, not non-C.

            The use of system-specific headers and functions makes the program
            non-portable (and off-topic here); it doesn't make it non-C. One of
            C's greatest assets is its ability to support system-specific
            extensions.

            What makes the program non-C is its use of "Else" and "If" rather than
            "else" and "if".

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Ronaldão

              #7
              Re: need to compile this ( trying to find roots of a bisection)

              As far as I could see, you may be using borland C or similar compiler
              for DOS or windows environments because of the conio.h header file.

              Your code will not compile because of some "If" and "Else" statemens.
              You can't use capital letters on them. About your main function, there
              is nothing wrong with it. It can have one of the forms:

              void main(void)
              int main(void)
              int main(int argc, char **argv)
              int main(int argc, char *argv[])
              and some extensions supports the form
              int main(int argc, char **argv, char **envp)

              Next time you post something here, try to be more explicit about the
              problems you are going through.

              Comment

              • CBFalconer

                #8
                Re: need to compile this ( trying to find roots of a bisection)

                Ronaldão wrote:[color=blue]
                >
                > As far as I could see, you may be using borland C or similar
                > compiler for DOS or windows environments because of the conio.h
                > header file.
                >
                > Your code will not compile because of some "If" and "Else"
                > statemens. You can't use capital letters on them. About your main
                > function, there is nothing wrong with it. It can have one of the
                > forms:
                >
                > void main(void)[/color]

                No, this is not allowed. main always returns int.
                [color=blue]
                > int main(void)
                > int main(int argc, char **argv)
                > int main(int argc, char *argv[])
                > and some extensions supports the form
                > int main(int argc, char **argv, char **envp)[/color]

                This is not specified in the C standard. Do not assume it. See
                the standard routines getenv and putenv.
                [color=blue]
                >
                > Next time you post something here, try to be more explicit about
                > the problems you are going through.[/color]

                The next time you post here try to be more accurate in your
                answers.

                --
                "If you want to post a followup via groups.google.c om, don't use
                the broken "Reply" link at the bottom of the article. Click on
                "show options" at the top of the article, then click on the
                "Reply" at the bottom of the article headers." - Keith Thompson
                More details at: <http://cfaj.freeshell. org/google/>
                Also see <http://www.safalra.com/special/googlegroupsrep ly/>

                Comment

                • Jordan Abel

                  #9
                  Re: need to compile this ( trying to find roots of a bisection)

                  On 2006-02-26, CBFalconer <cbfalconer@yah oo.com> wrote:[color=blue]
                  > Ronaldão wrote:[color=green]
                  >> and some extensions supports the form
                  >> int main(int argc, char **argv, char **envp)[/color]
                  >
                  > This is not specified in the C standard. Do not assume it. See
                  > the standard routines getenv and putenv.[/color]

                  putenv is only _barely_ more standard [in that it's defined in _a_
                  standard] than envp, and isn't likely to be supported on any
                  implementations where envp is not. He _did_ say it was an extension.

                  Comment

                  • Micah Cowan

                    #10
                    Re: need to compile this ( trying to find roots of a bisection)

                    Keith Thompson <kst-u@mib.org> writes:
                    [color=blue]
                    > CBFalconer <cbfalconer@yah oo.com> writes:[/color]
                    [color=blue]
                    > Declaring "void main()" makes it incorrect C, not non-C.[/color]

                    Well, nowadays, it makes it non-portable C, not incorrect C. :-)

                    'course, depending on your definition for "incorrect C", the two may
                    overlap... :-)
                    [color=blue]
                    > What makes the program non-C is its use of "Else" and "If" rather than
                    > "else" and "if".[/color]

                    I suspect the OP was used to MS Visual Basic or somesuch.

                    Comment

                    • Richard G. Riley

                      #11
                      Re: need to compile this ( trying to find roots of a bisection)

                      On 2006-02-27, Micah Cowan <micah@cowan.na me> wrote:[color=blue]
                      > Keith Thompson <kst-u@mib.org> writes:
                      >[color=green]
                      >> CBFalconer <cbfalconer@yah oo.com> writes:[/color]
                      >[color=green]
                      >> Declaring "void main()" makes it incorrect C, not non-C.[/color]
                      >
                      > Well, nowadays, it makes it non-portable C, not incorrect C. :-)
                      >
                      > 'course, depending on your definition for "incorrect C", the two may
                      > overlap... :-)
                      >[color=green]
                      >> What makes the program non-C is its use of "Else" and "If" rather than
                      >> "else" and "if".[/color]
                      >
                      > I suspect the OP was used to MS Visual Basic or somesuch.[/color]

                      It makes it non "standard" : and CBF was, as is his wont, pointing
                      that out.

                      The ANSI ISO 9899 standard says that main must be defined to return
                      type of int.

                      --
                      Remove evomer to reply

                      Comment

                      • Keith Thompson

                        #12
                        Re: need to compile this ( trying to find roots of a bisection)

                        "Richard G. Riley" <rgrdev@gmail.c om> writes:
                        [...][color=blue]
                        > The ANSI ISO 9899 standard says that main must be defined to return
                        > type of int.[/color]

                        That's not exactly true. Grab a copy of n1124.pdf (which incorporates
                        the C99 standard plus TC1 and TC2) to see exactly what it says.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Richard G. Riley

                          #13
                          Re: need to compile this ( trying to find roots of a bisection)

                          On 2006-02-27, Keith Thompson <kst-u@mib.org> wrote:[color=blue]
                          > "Richard G. Riley" <rgrdev@gmail.c om> writes:
                          > [...][color=green]
                          >> The ANSI ISO 9899 standard says that main must be defined to return
                          >> type of int.[/color]
                          >
                          > That's not exactly true. Grab a copy of n1124.pdf (which incorporates
                          > the C99 standard plus TC1 and TC2) to see exactly what it says.
                          >[/color]

                          I need practice reading standards.

                          Doesnt the "or in some other implementation-defined manner" make a
                          mockery of the first two? On first read I thought that referred to the
                          parameters to the function.


                          --
                          Remove evomer to reply

                          Comment

                          • Keith Thompson

                            #14
                            Re: need to compile this ( trying to find roots of a bisection)

                            "Richard G. Riley" <rgrdev@gmail.c om> writes:[color=blue]
                            > On 2006-02-27, Keith Thompson <kst-u@mib.org> wrote:[color=green]
                            >> "Richard G. Riley" <rgrdev@gmail.c om> writes:
                            >> [...][color=darkred]
                            >>> The ANSI ISO 9899 standard says that main must be defined to return
                            >>> type of int.[/color]
                            >>
                            >> That's not exactly true. Grab a copy of n1124.pdf (which incorporates
                            >> the C99 standard plus TC1 and TC2) to see exactly what it says.
                            >>[/color]
                            >
                            > I need practice reading standards.
                            >
                            > Doesnt the "or in some other implementation-defined manner" make a
                            > mockery of the first two?[/color]

                            No, it doesn't. Look up the definition of "implementa tion-defined".

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Micah Cowan

                              #15
                              Re: need to compile this ( trying to find roots of a bisection)

                              Keith Thompson <kst-u@mib.org> writes:
                              [color=blue]
                              > "Richard G. Riley" <rgrdev@gmail.c om> writes:[color=green]
                              > > On 2006-02-27, Keith Thompson <kst-u@mib.org> wrote:[color=darkred]
                              > >> "Richard G. Riley" <rgrdev@gmail.c om> writes:
                              > >> [...]
                              > >>> The ANSI ISO 9899 standard says that main must be defined to return
                              > >>> type of int.
                              > >>
                              > >> That's not exactly true. Grab a copy of n1124.pdf (which incorporates
                              > >> the C99 standard plus TC1 and TC2) to see exactly what it says.
                              > >>[/color]
                              > >
                              > > I need practice reading standards.
                              > >
                              > > Doesnt the "or in some other implementation-defined manner" make a
                              > > mockery of the first two?[/color]
                              >
                              > No, it doesn't. Look up the definition of "implementa tion-defined".[/color]

                              Actually, I'm somewhat inclined to agree with Mr Riley on this
                              point. It does kind of make a mockery of the first two. Or maybe it's
                              just a mockery of us.

                              For years on this group, we had been saying, "main() always returns an
                              int." Many of us still do this, but thanks to the '99 committee, it's
                              no longer strictly true.

                              Regardless, a void-returning main is still outside the scope of this
                              NG. It just changes the way we have to say that. :-/

                              Comment

                              Working...