Divide by zero error 
Author Message
 Divide by zero error

I'm trying to write a t-test program but no matter what i do, the number of
trials variable 'num' is always 0  here's the code:

#include <stdio.h>
#include <math.h>
float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;

void main()
{
num=10;
while (num!=0)
{
/*printf("Enter number of trials ");
scanf("%d",number);*/
printf("Enter Mean 1 ");
scanf("%f",mean1);
printf("Enter Mean 2 ");
scanf("%f",mean2);
printf("Enter Standard Deviation 1 ");
scanf("%f",stdev1);
printf("Enter Standard Deviation 2 ");
scanf("%f",stdev2);

totalstd=((stdev1)*(stdev1))+((stdev2)*(stdev2));
printf("%f\n",totalstd);

denom=(sqrt(totalstd/num));
t=(mean1-mean2)/(denom);

Quote:
}

any ideas?


Thu, 24 Aug 2000 03:00:00 GMT  
 Divide by zero error

Quote:

> I'm trying to write a t-test program but no matter what i do, the number of
> trials variable 'num' is always 0  here's the code:

> #include <stdio.h>
> #include <math.h>
> float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;

  ^ you better declare these variables local to main
Quote:

> void main()

  ^ should be: int main ()
Quote:
> {
> num=10;
> while (num!=0)
> {
> /*printf("Enter number of trials ");
> scanf("%d",number);*/

             ^ should be: &num  
          ^ should be %f
Quote:
> printf("Enter Mean 1 ");
> scanf("%f",mean1);

             ^ should be: &mean1  
Quote:
> printf("Enter Mean 2 ");
> scanf("%f",mean2);

             ^ should be: &mean2  
Quote:
> printf("Enter Standard Deviation 1 ");
> scanf("%f",stdev1);

             ^ should be: &stdev1
Quote:
> printf("Enter Standard Deviation 2 ");
> scanf("%f",stdev2);

             ^ should be: &stdev2
Quote:

> totalstd=((stdev1)*(stdev1))+((stdev2)*(stdev2));

            ^ () not necessary try:
            totalstd = stdev1*stdev1 + stdev2*stdev2;
Quote:
> printf("%f\n",totalstd);

> denom=(sqrt(totalstd/num));

        ^ () not necessary try: denom=sqrt(totalstd/num);
Quote:
> t=(mean1-mean2)/(denom);

                  ^ () not necessary try: t=(mean1-mean2)/denom;  
Quote:

> }

> any ideas?

Yes - scanf needs the address of the variables passed, not their values!
Try again
Markus


Thu, 24 Aug 2000 03:00:00 GMT  
 Divide by zero error

Quote:

> I'm trying to write a t-test program but no matter what i do, the number of
> trials variable 'num' is always 0  here's the code:
:
> float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;

        Why is num a float?  Can you have 5.23 trials?

Quote:
> void main()
> {
> num=10;
> while (num!=0)

        I tested this and my compiler converted the 10 to 10.0 and assessed
the as being true.  However, you might try the following,

*       I would recommend trying        num = 10.0;
        and                             while (num != 0.0)

*       However, you would probably have better results making num an int.

Quote:
> printf("Enter Mean 1 ");
> scanf("%f",mean1);

:
        That last line should be re-written as scanf( "%f", &mean1);

        The scanf() function takes the address-of the variable, and not its
value.  In all likelihood, were you to run the while loop, it would crash.

                                                        -- Stone



Thu, 24 Aug 2000 03:00:00 GMT  
 Divide by zero error

: I'm trying to write a t-test program but no matter what i do, the number of
: trials variable 'num' is always 0  here's the code:

: #include <stdio.h>
: #include <math.h>
: float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;

: void main()
/* try int main(void) */
: {
: num=10;
: while (num!=0)
/* It's a bad idea to test floating point numbers against
 * a fixed value.  It's also a bad idea to use them as
 * loop controls.
 */
: {
: /*printf("Enter number of trials ");
: scanf("%d",number);*/
: printf("Enter Mean 1 ");
: scanf("%f",mean1);
: printf("Enter Mean 2 ");
: scanf("%f",mean2);
: printf("Enter Standard Deviation 1 ");
: scanf("%f",stdev1);
: printf("Enter Standard Deviation 2 ");
: scanf("%f",stdev2);
/* scanf() takes pointer arguments; add '&' to all your
 * variable names in the calls.
 */

: totalstd=((stdev1)*(stdev1))+((stdev2)*(stdev2));
/* The brackets are unnecessary */
: printf("%f\n",totalstd);

: denom=(sqrt(totalstd/num));
/* sqrt() returns double */
: t=(mean1-mean2)/(denom);
/* Add return 0; here */
: }

Will



Thu, 24 Aug 2000 03:00:00 GMT  
 Divide by zero error

Quote:

> I'm trying to write a t-test program but no matter what i do, the number of
> trials variable 'num' is always 0  here's the code:

> #include <stdio.h>
> #include <math.h>
> float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;

> void main()
> {
> num=10;
> while (num!=0)
> {
> /*printf("Enter number of trials ");
> scanf("%d",number);*/
> printf("Enter Mean 1 ");
> scanf("%f",mean1);
> printf("Enter Mean 2 ");
> scanf("%f",mean2);
> printf("Enter Standard Deviation 1 ");
> scanf("%f",stdev1);
> printf("Enter Standard Deviation 2 ");
> scanf("%f",stdev2);

> totalstd=((stdev1)*(stdev1))+((stdev2)*(stdev2));
> printf("%f\n",totalstd);

> denom=(sqrt(totalstd/num));
> t=(mean1-mean2)/(denom);

> }

> any ideas?

   Two points:
 1) scanf requires pointers to objects where you do want
to store input. So your calls must be like
   scanf("%d",&number);
  /* passing an address of a variable "number" */
 2) main() returns int

        Regards,
                Alex Krol



Fri, 25 Aug 2000 03:00:00 GMT  
 Divide by zero error



Quote:

>> I'm trying to write a t-test program but no matter what i do, the number of
>> trials variable 'num' is always 0  here's the code:

<snip>
>>float num,stdev1,stdev2,mean1,mean2,t,totalstd,denom;
<snip>
>> num=10;
>> while (num!=0)
>> {
>> /*printf("Enter number of trials ");
>> scanf("%d",number);*/
<snip>
>> any ideas?
<snip>
> 1) scanf requires pointers to objects where you do want
>to store input. So your calls must be like
>   scanf("%d",&number);
>  /* passing an address of a variable "number" */

But since you never declared a variable called "number", it's hard to
see how this code could even have compiled.  Aside from all of the other
bugs in the code, the reason num will never change is that there's no
code which changes num.

After you fix all of your other problems (void main() -> int main(), and
passing addresses to scanf), you probably want to change this particular
call to scanf to
    scanf("%d", &num);
and change the declaration of num from float to int.

--

Kenan Systems Corporation



Fri, 25 Aug 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. catching divide by zero errors in C Dll used by VB app

2. turboc "divide by zero" error

3. Exception or Error-Message on divide by zero?

4. Catching a divide-by-zero error?

5. Divide by Zero

6. Divide By Zero

7. Need Recoveralble Divide by Zero Trap for Borland C

8. compiler detecting divide by zero

9. Divide By zero...

10. Divide by zero - How to avoid system message

11. double's Divide By Zero constant.

12. Trapping Divide-by-zero

 

 
Powered by phpBB® Forum Software