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

A

Anders Koeln

#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;
}
 
M

Michael Angelo Ravera

#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).
 
A

August Karlstrom

#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
 
A

August Karlstrom

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
 
K

Keith Thompson

Anders Koeln said:
#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.
 
J

Jens Thoms Toerring

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
 
K

Keith Thompson

August Karlstrom said:
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).
 
K

Keith Thompson

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.


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.
 
I

Ian Collins

#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.
 
S

Seebs

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
 
S

Seebs

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
 
D

Dann Corbit

#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
*/
 
A

August Karlstrom

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
 
B

BartC

Dann Corbit said:
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.
 
L

luserXtrog

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
 
B

Barry Schwarz

#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.
 
J

Jorgen Grahn

#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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top