Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Hi, Any idea why this program is not generating an output? Thanks

Reply
Thread Tools

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

 
 
Anders Koeln
Guest
Posts: n/a
 
      09-28-2010
#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;
}
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      09-28-2010
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)
{
 
Reply With Quote
 
 
 
 
Michael Angelo Ravera
Guest
Posts: n/a
 
      09-28-2010
On Sep 28, 12:25*pm, Anders Koeln <(E-Mail Removed)> 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).
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      09-28-2010
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
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      09-28-2010
On 2010-09-28 21:51, Scott Fluhrer wrote:
> "Anders Koeln"<(E-Mail Removed)> wrote in message
> news:i7tfec$tn4$(E-Mail Removed)...
>> #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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-28-2010
Anders Koeln <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      09-28-2010
Anders Koeln <(E-Mail Removed)> 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 ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-28-2010
August Karlstrom <(E-Mail Removed)> writes:
> On 2010-09-28 21:51, Scott Fluhrer wrote:
>> "Anders Koeln"<(E-Mail Removed)> wrote in message
>> news:i7tfec$tn4$(E-Mail Removed)...
>>> #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) (E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-28-2010
(E-Mail Removed) (Jens Thoms Toerring) writes:
> Anders Koeln <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-28-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any idea why this program is not generating an output ? lector C Programming 14 04-23-2008 05:28 AM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Any idea about generating SAIF files ? New to Power Analysis VHDL 3 10-22-2004 09:24 AM
Any idea on VHDL and C cosimulation?Thanks Lee VHDL 9 05-11-2004 08:39 PM



Advertisments