-3

Here is my code:

#include <stdio.h>
#include <math.h>
int main(void) 
{
    double x, y, z;
    double numerator;
    double denominator;
    printf("This program will solve (x^2+y^2)/(x/y)^3\n");
    printf("Enter the value for x:\n");
    scanf("%lf", x);
    printf("Enter the value for y:\n");
    scanf("%lf", y);
    numerator = sqrt(x) + sqrt(y);
    denominator = pow((x/y),3);
    z = (numerator/denominator);
    printf("The solution is: %f\n", z);
    return(0);

}

Can anyone give me a (hopefully) quick pointer to fix my infinite loop?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 1
    You should read in the variables via `scanf("%lf", &x);`. scanf will modify the value of the variables in your program, thus you always need to pass a reference to the variable in your function. – Jack Ryan Sep 27 '15 at 18:31
  • 1
    `sqrt(x)` returns square root, not square. Use `pow(x,2)`. – AdminXVII Sep 27 '15 at 18:34
  • 6
    There are no infinite loops in the code. If you have problems handling the *input*, say so... – Karoly Horvath Sep 27 '15 at 18:35

3 Answers3

0

There's no loop in your function, so I think it's your calls to scanf() that are causing the error:

You need to pass reference to scanf(), i.e. use scanf("%lf",&x) instead of scanf("%lf",x).

BTW, according to your fonction definition, you should use pow(x,2) instead of sqrt(x) which returns the square root.

AdminXVII
  • 1,319
  • 11
  • 22
0

Since this is your first question

**Welcome to stack overflow**

Your code doesnt go into an infinite loop,there is a runtime error. Your scanf Code is flawed use this:

scanf("%lf",&x); 
scanf("%lf",&y);

you want scanf to modify the value contained at the address field of your value.Please read tutorials.

Also use

numerator=pow(x,2) + pow(y,2);//numerator=x^2+y^2
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
0

It's not infinite loop, your code just returns infinity. And that is because scanf() needs a pointer to variable where it should put the read number. To get an address of variable you can use & operator like this:

scanf("%lf", &x);
scanf("%lf", &y);
Łukasz
  • 8,555
  • 2
  • 28
  • 51