Y
Y
Yan Nesterov2018-12-14 16:13:16
C++ / C#
Yan Nesterov, 2018-12-14 16:13:16

Why doesn't the C code work?

#include <stdio.h>
int main()
{
    float s = 0;
    float a;
    int i = 1;
    while(s>=0,0001)
    {
      a = 1/(pow(i,4)+4*pow(i,2)+1);
      s -= a;
      i++;
    }
    printf("%f",&a);
    return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mercury13, 2018-12-14
@muridse

So far I see No need to take the address from a. 1) Fractional numbers in all PL are written through a dot. For some reason (parallel loops), C has a "comma" operation, so the code can even compile. 2) The loop will not be executed even once, because 0 < 0.0001.

J
jcmvbkbc, 2018-12-14
@jcmvbkbc

while(s>=0,0001)

-- this is an infinite loop. Because the while condition always has the value 1. Because this is not a comparison of s with 0.0001, but two expressions connected by the "comma" operator -- s >= 0 and 0001.

F
fastkulob, 2018-12-15
@fastkulob

in the last printf, remove the & in front of the variable a letter.
You have printf("%f",&a);
Correct printf("%f",a);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question