Hi, Any idea why this program is not generating an output? Thanks

Discussion in 'C Programming' started by Anders Koeln, Sep 28, 2010.

  1. Anders Koeln

    Anders Koeln Guest

    #include<stdio.h>
    #include<math.h>
    #define M_PI 3.14159

    int main()
    {
    double theta, phi, sinth;
    double count;
    double incr;
    double s;

    s = ((double) 180)/M_PI; /* converting to radiens */
    incr = 0.5;
    theta = (double) 0;

    for(theta = incr; theta < (double) 180; theta += incr)
    sinth = sin(s *theta);
    for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    count ++;
    printf("%f", count);
    return 0;
    }
    Anders Koeln, Sep 28, 2010
    #1
    1. Advertising

  2. Anders Koeln

    Shao Miller Guest

    Anders Koeln wrote:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {


    In thread "Bus error because of casting?", parjit wrote:
    > ... ... ...
    >
    > #include<stdio.h>
    > #include<string.h>
    > main()
    > {
    > ... ... ...


    Some people enjoy:

    int main(void)
    {
    Shao Miller, Sep 28, 2010
    #2
    1. Advertising

  3. On Sep 28, 12:25 pm, Anders Koeln <> wrote:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    >         double theta, phi, sinth;
    >         double count;
    >         double incr;
    >         double s;
    >
    >         s = ((double) 180)/M_PI; /* converting to radiens */
    >         incr = 0.5;
    >         theta = (double) 0;
    >
    >         for(theta =  incr; theta < (double) 180; theta += incr)
    >               sinth = sin(s *theta);
    >               for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    >                      count ++;
    >               printf("%f", count);
    >         return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    As it is, the program would display the final value of count without a
    line ending (and on some systems, you don't get a line without either
    a line ending or a flush()). If you had in mind to use two nested
    loops, you need to use some "{" "}" pairs as intended. You probably
    also wanted to put in a line ending at the end of the outer loop (as
    intended).
    Michael Angelo Ravera, Sep 28, 2010
    #3
  4. On 2010-09-28 21:25, Anders Koeln wrote:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta< (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi< (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    > }


    You probably want braces around the statements indented under the outer
    for loop. This is C, not Python. Try to print out the value of phi in
    the inner for loop to see what happens.

    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
    August Karlstrom, Sep 28, 2010
    #4
  5. On 2010-09-28 21:51, Scott Fluhrer wrote:
    > "Anders Koeln"<> wrote in message
    > news:i7tfec$tn4$...
    >> #include<stdio.h>
    >> #include<math.h>
    >> #define M_PI 3.14159
    >>
    >> int main()
    >> {
    >> double theta, phi, sinth;
    >> double count;

    >
    > Oh, and I saw another thing just after I posted: you never initialize count;
    > hence it could be any value at all (for example, 1e+23 or some flavor of
    > Not-a-Number). And so, had you reached the printf, the value that you
    > printed out could have been anything.


    I just noticed that gcc gives no warning about this, despite -Wall being
    used. Strange.

    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
    August Karlstrom, Sep 28, 2010
    #5
  6. Anders Koeln <> writes:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    > }


    If you print output without a trailing new-line ("\n"), it's not
    guaranteed to appear. Try
    printf("%f\n", count);

    Using a floating-point variable for a count is generally not a good
    idea. count should be an integer of some type (int is probably ok
    here).

    Indentation does not affect statement grouping. Your indentation
    implies that you're trying to write two nested for loops. In fact,
    your first for loop controls only the assignment to sinth; after it
    completes, the second for loop controls only the "count ++;".

    This seems to be the cause of your problem. The first loop repeatedly
    assigns values to sinth, but only the last value is ever used. The
    final value of sinth controls the execution of the second loop. If it
    happens to be <= 0.0, your loop will never terminate.

    Suggestion: Always use braces for compound statements, even if there's
    only a single controlled statement:

    for (...) {
    count ++;
    }

    If you had either run the program under a debugger or added printf()
    statements to show the progress of execution and the values of your
    variables, you probably could have figured this out for yourself.

    Even if you restructure your program, I'm not sure it makes mathematical
    sense. sin(theta) is going to be negative about half the time, so your
    loop on values of phi could go in either direction.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 28, 2010
    #6
  7. Anders Koeln <> wrote:

    Could you please put ypur question into the body of your message?
    My newreader shows only the 20 to 30 characters of the subject
    line and I think other people have the same problem.

    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159


    Note that on some systems 'M_PI' is a predefined macro.

    > int main()


    int main( void )

    > {
    > double theta, phi, sinth;
    > double count;


    You should initialize 'count', it's not going to be set to
    0 automatically.

    > double incr;
    > double s;


    > s = ((double) 180)/M_PI; /* converting to radiens */


    I guess the first problem is here - it looks as if you actually
    want
    s = M_PI / 180;

    i..e set 's' to 1 degree expressed in radians. Otherwise
    'sinth' later on starts with a negative value, which you
    then use in the calculation of how much you increment 'phi'
    and you thus end up with 'phi' going more and more negative
    instead of becoming incremented by a positive value.

    Note that all your casts to double are unnecessary, the con-
    versions will all happen automatically. And then writing

    (double) 360

    instead of simply

    360.0

    looks rather strange, to say the least.

    > incr = 0.5;
    > theta = (double) 0;


    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);


    Your indentation makes it look as if you expect that there
    are going to be two nested for loops. But this isn't the
    case, what you wrote here are two simple for loops, the
    one using 'theta' getting run all through before the se-
    cond one with 'phi' getting run only afterwards. C isn't
    Python and indentation isn't taken into consideration by
    the compiler, it's only for human consumption. You need
    to enclose blocks of code in '{' and '}' to tell it what
    you want it to do.

    Finally, in this line

    > printf("%f", count);


    you should put in a '\n' in the format string, otherwise
    there's no linefeed at the end

    > printf("%f\n", count);


    > return 0;
    > }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Sep 28, 2010
    #7
  8. August Karlstrom <> writes:
    > On 2010-09-28 21:51, Scott Fluhrer wrote:
    >> "Anders Koeln"<> wrote in message
    >> news:i7tfec$tn4$...
    >>> #include<stdio.h>
    >>> #include<math.h>
    >>> #define M_PI 3.14159
    >>>
    >>> int main()
    >>> {
    >>> double theta, phi, sinth;
    >>> double count;

    >>
    >> Oh, and I saw another thing just after I posted: you never initialize count;
    >> hence it could be any value at all (for example, 1e+23 or some flavor of
    >> Not-a-Number). And so, had you reached the printf, the value that you
    >> printed out could have been anything.

    >
    > I just noticed that gcc gives no warning about this, despite -Wall being
    > used. Strange.


    gcc doesn't perform the analysis necessary to warn about uninitialized
    variables unless it's invoked with optimization (-O1 or higher).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 28, 2010
    #8
  9. (Jens Thoms Toerring) writes:
    > Anders Koeln <> wrote:
    >
    > Could you please put ypur question into the body of your message?
    > My newreader shows only the 20 to 30 characters of the subject
    > line and I think other people have the same problem.
    >
    >> #include<stdio.h>
    >> #include<math.h>
    >> #define M_PI 3.14159

    >
    > Note that on some systems 'M_PI' is a predefined macro.


    Yes, but it's not in the implementation namespace, so as far as
    the C standard is concerned it's perfectly fine to use it like any
    other identifier.

    Still, it might not be a bad idea to pick a different name. On my
    system, gcc is not fully conforming by default. If I invoke it
    with no options, it complains that M_PI is redefined (because it
    was already defined in <math.h>. You can always invoke gcc in
    conforming mode, but defining your own M_PI might later get in
    the way if you need to use system-specific extensions. And it can
    cause confusion to readers who are familiar with the M_PI extension.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 28, 2010
    #9
  10. Anders Koeln

    Ian Collins Guest

    On 09/29/10 08:25 AM, Anders Koeln wrote:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta< (double) 180; theta += incr)
    > sinth = sin(s *theta);


    Assuming this isn't yet another troll (no sign of the OP joining in),
    the result of this line is negative, so the program will never complete.

    > for(phi = 0; phi< (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    > }


    --
    Ian Collins
    Ian Collins, Sep 28, 2010
    #10
  11. Anders Koeln

    Seebs Guest

    On 2010-09-28, Anders Koeln <> wrote:
    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);


    This is not Python. Indentation does not create a block. You almost
    certainly want {} around this indented stuff.

    Similarly, "printf" doesn't write newlines automatically -- so all you
    get for output here is a single "%f" printed, which could easily be something
    small and unnoticeable if it were immediately left of your prompt because
    there was no trailing newline...

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Sep 28, 2010
    #11
  12. Anders Koeln

    Seebs Guest

    On 2010-09-28, Ian Collins <> wrote:
    >> s = ((double) 180)/M_PI; /* converting to radiens */
    >> incr = 0.5;
    >> theta = (double) 0;


    >> for(theta = incr; theta< (double) 180; theta += incr)
    >> sinth = sin(s *theta);


    > Assuming this isn't yet another troll (no sign of the OP joining in),
    > the result of this line is negative, so the program will never complete.


    And before someone jumps in and says something about wrapping -- there's a
    large range of floats (or doubles) where "+= 0.5" won't do anything.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Sep 28, 2010
    #12
  13. Anders Koeln

    Dann Corbit Guest

    In article <i7tfec$tn4$>, lid says...
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    > }
    >
    >


    You might possibly have meant:

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

    int main(void)
    {
    double theta=0, phi, sinth=0;
    unsigned long long count=0;
    const double incr = 0.5;
    double s = 0.017453292519943295769236907684886; /* ~ pi/180 */;

    for (theta = incr; theta < 180.0; theta += incr)
    {
    sinth = sin (s * theta);
    for (phi = 0; phi < 360.0; phi += incr / sinth)
    count++;
    }
    printf ("%llu\n", count);
    return 0;
    }
    /*
    165194 is one possible output
    */
    Dann Corbit, Sep 28, 2010
    #13
  14. On 2010-09-28 22:10, Keith Thompson wrote:
    > gcc doesn't perform the analysis necessary to warn about uninitialized
    > variables unless it's invoked with optimization (-O1 or higher).
    >


    OK, not too intuitive.

    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
    August Karlstrom, Sep 28, 2010
    #14
  15. Anders Koeln

    BartC Guest

    "Dann Corbit" <> wrote in message
    news:-september.org...
    > In article <i7tfec$tn4$>, lid says...


    >> s = ((double) 180)/M_PI; /* converting to radiens */
    >> incr = 0.5;
    >> theta = (double) 0;
    >>
    >> for(theta = incr; theta < (double) 180; theta += incr)
    >> sinth = sin(s *theta);
    >> for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    >> count ++;
    >> printf("%f", count);
    >> return 0;
    >> }


    > You might possibly have meant:
    >
    > #include <stdio.h>
    > #include <math.h>
    >
    > int main(void)
    > {
    > double theta=0, phi, sinth=0;
    > unsigned long long count=0;
    > const double incr = 0.5;
    > double s = 0.017453292519943295769236907684886; /* ~ pi/180 */;
    >
    > for (theta = incr; theta < 180.0; theta += incr)
    > {
    > sinth = sin (s * theta);
    > for (phi = 0; phi < 360.0; phi += incr / sinth)
    > count++;
    > }
    > printf ("%llu\n", count);
    > return 0;
    > }
    > /*
    > 165194 is one possible output


    Or possibly:

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

    int main(void)
    {
    double theta, phi, sinth;
    int count=0;
    double incr;
    double radian=180/M_PI; /* degrees in a radian */

    incr = 0.5;

    for(theta = incr; theta < 180.0; theta += incr) {
    sinth = sin(theta/radian);
    count = 0;
    for(phi = 0; fabs(phi) < 360.0 ; phi += incr/sinth)
    count ++;
    printf("Theta: %4.1f° Count: %d\n", theta, count);
    }
    return 0;
    }

    which has multiple outputs.

    --
    Bartc
    BartC, Sep 29, 2010
    #15
  16. Anders Koeln

    luserXtrog Guest

    On Sep 28, 2:54 pm, August Karlstrom <> wrote:
    > On 2010-09-28 21:51, Scott Fluhrer wrote:
    >
    > > "Anders Koeln"<>  wrote in message
    > >news:i7tfec$tn4$...
    > >> #include<stdio.h>
    > >> #include<math.h>
    > >> #define M_PI 3.14159

    >
    > >> int main()
    > >> {
    > >>         double theta, phi, sinth;
    > >>         double count;

    >
    > > Oh, and I saw another thing just after I posted: you never initialize count;
    > > hence it could be any value at all (for example, 1e+23 or some flavor of
    > > Not-a-Number).  And so, had you reached the printf, the value that you
    > > printed out could have been anything.

    >
    > I just noticed that gcc gives no warning about this, despite -Wall being
    > used. Strange.
    >


    lint does!

    splint warn.c
    Splint 3.1.2 --- 23 Aug 2008

    warn.c: (in function main)
    warn.c:19:22: Variable count used before definition
    An rvalue is used that may not be initialized to a value on some
    execution
    path. (Use -usedef to inhibit warning)
    warn.c:18:62: Variable sinth used before definition (in post loop
    increment)

    Finished checking --- 2 code warnings
    luserXtrog, Sep 29, 2010
    #16
  17. On Tue, 28 Sep 2010 19:25:00 +0000 (UTC), Anders Koeln
    <> wrote:

    >#include<stdio.h>
    >#include<math.h>
    >#define M_PI 3.14159
    >
    >int main()
    >{
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    >}


    And since no one else has mentioned it, every one of your casts serves
    no purpose other than to make the code hard to read.

    --
    Remove del for email
    Barry Schwarz, Sep 29, 2010
    #17
  18. Anders Koeln

    Jorgen Grahn Guest

    On Tue, 2010-09-28, Anders Koeln wrote:
    > #include<stdio.h>
    > #include<math.h>
    > #define M_PI 3.14159
    >
    > int main()
    > {
    > double theta, phi, sinth;
    > double count;
    > double incr;
    > double s;
    >
    > s = ((double) 180)/M_PI; /* converting to radiens */
    > incr = 0.5;
    > theta = (double) 0;
    >
    > for(theta = incr; theta < (double) 180; theta += incr)
    > sinth = sin(s *theta);
    > for(phi = 0; phi < (double) 360 ; phi += incr/ sinth)
    > count ++;
    > printf("%f", count);
    > return 0;
    > }


    tuva:/tmp> gcc -W -Wall -pedantic -ansi -O3 -c foo.c
    foo.c: In function 'main':
    foo.c:8: warning: 'count' may be used uninitialized in this function

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 30, 2010
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee
    Replies:
    9
    Views:
    2,999
    Wendell Petersen
    May 11, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  3. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    701
    Dennis Lee Bieber
    Feb 5, 2008
  4. Replies:
    0
    Views:
    619
  5. lector
    Replies:
    14
    Views:
    547
    Keith Thompson
    Apr 23, 2008
Loading...

Share This Page