9 Replies - 464 Views - Last Post: 25 September 2018 - 12:18 PM Rate Topic: -----

#1 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Can not understand why in output 0

Posted 25 September 2018 - 11:52 AM

In my program I must type r = 24.5
But I don't understand why my program is outputing this:
Type r = 24.5
r = 0.000000
k = 0.000000
alpha = 0.000000
Please, help :surrender:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float alpha, r, k,
          f = 11.862,
          z = 4.83,
          a = 3.65;
    printf("Type r = ");
    scanf("%f", &r);
    k = (z*acos((r+a) / r)) / 2;
    alpha = pow(r * (1 + cos(2 * k * f)), 1 / 3);
    printf("r = %f \n", &r);
    printf("k = %f \n", &k);
    printf("alpha = %f \n", &alpha);
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Can not understand why in output 0

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7293
  • View blog
  • Posts: 15,179
  • Joined: 16-October 07

Re: Can not understand why in output 0

Posted 25 September 2018 - 11:59 AM

Once you throw an integer into the mix you tend to lose your float. You compiler should have told you this.

Try:
k = (z*acos((r+a) / r)) / 2.0;


Was This Post Helpful? 1
  • +
  • -

#3 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:04 PM

View Postbaavgai, on 25 September 2018 - 11:59 AM, said:

Try:
k = (z*acos((r+a) / r)) / 2.0;


It didn't help. Maybe smth else :dontgetit:

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7293
  • View blog
  • Posts: 15,179
  • Joined: 16-October 07

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:07 PM

Oops, I just saw the wonky ampersand on the printf. Don't do that. Do this: printf("r = %f \n", r);

In C, you use & to get and address of a variable. You really don't want to print the address of the variable, you want the value itself.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:08 PM

Also this: printf("r = %f \n", &r); is incorrect, your compiler should be warning you about this issue as well.

main.c|15|warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]|


You don't need the ampersand when trying to print values with printf().


Jim
Was This Post Helpful? 1
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14502
  • View blog
  • Posts: 58,134
  • Joined: 12-June 08

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:08 PM

Break you formula up.

k = (4.83*acos((24.5 + 3.65) / 24.5)) / 2;

What is: (24.5 + 3.65) / 24.5
What is: acos((24.5 + 3.65) / 24.5)
What is: 4.83*acos((24.5 + 3.65) / 24.5)
Was This Post Helpful? 1
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2662
  • View blog
  • Posts: 4,256
  • Joined: 21-June 11

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:10 PM

printf doesn't take pointers (unless you want to actually print a pointer), it takes the values itself (unlike scanf, which needs to take a pointer because it needs to write to the variables). So you're passing a pointer where a double was expected, invoking undefined behavior. The reason that this undefined behavior gives you 0 on your machine is probably that the pointer is stored in an integer register and then printf tries to read its argument from a double register, which was never assigned a value and thus simply contains 0.0.

View Postbaavgai, on 25 September 2018 - 08:59 PM, said:

Once you throw an integer into the mix you tend to lose your float.


No, it's the other way around: Once you throw a floating point number into the mix, you get a floating point result. It's perfectly okay to divide a float by 2 instead of 2.0.
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:10 PM

You also seem to be missing a required #include file for those math functions.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:12 PM

Thanks, man. :rolleyes2: U saved me :innocent:
Was This Post Helpful? 0
  • +
  • -

#10 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: Can not understand why in output 0

Posted 25 September 2018 - 12:18 PM

Thank You all, guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1