Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to make this code work?!

+3 votes
asked Dec 30, 2021 by Alexandr (300 points)
double sum(double eps)
{
     double a, s=0, p=-1, four=4,five=25;
     while (1)
     {
           a=p/(four+five);
           s+=a;
           if (abs(a)<eps) return s;
           p=-p;
           four=four*4;
           five=five*5;
     }
}

1 Answer

0 votes
answered Dec 30, 2021 by Peter Minarik (55,760 points)
  1. What is the problem?
  2. What input do you use when the problem happens?
  3. What is your program supposed to do?
  4. Do you have a main() function to call sum?
  5. Have you defined abs()?

Your function "works" (returns a value) in the following scenario:

#include <stdio.h>

#define abs(x) (x) < 0 ? (x) : -(x)

double sum(double eps)
{
     double a, s=0, p=-1, four=4,five=25;
     while (1)
     {
           a=p/(four+five);
           s+=a;
           if (abs(a)<eps) return s;
           p=-p;
           four=four*4;
           five=five*5;
     }
}

int main()
{
    printf("result: %lf\n", sum(0.2));
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...