0

The value K is set to increment depending on i in order to evaluate l1, l2, l3, l4 until k<= g. What am I doing wrong????

#include <stdio.h>
#include <math.h>

int main(void)  {

    float  c=0, h=0, x=0, i=0;

    printf("Proj. #2 - Juan Perez\n");
    printf("Power of lamps (in watts)?  ");
    scanf("%f" , &c);

    printf("Height of lamp (in meters)?  ");
    scanf("%f" , &h);

    printf("Distance apart? (in meters)?  ");
    scanf("%f" , &x);

    printf("Interval? (in meters)? ");
    scanf("%f" , &i);


    printf("Power: %f\n", c);
    printf("Height: %f\n",  h);
    printf("Distance apart: %f\n",  x);
    printf("Interval: %f\n",  i);

    float  k=75, d=0, e=x, f=2*x, g=3*x;
    float  l1=((c*h)/(powf((h*h)+((k-d)*(k-d)), 1.5)));
    float  l2=((c*h)/(powf((h*h)+((k-e)*(k-e)), 1.5)));
    float  l3=((c*h)/(powf((h*h)+((k-f)*(k-f)), 1.5)));
    float  l4=((c*h)/(powf((h*h)+((k-g)*(k-g)), 1.5)));
    float j=l1+l2+l3+l4;

    while (k<=g){
        printf("%f\n", j);
        k+=i;
    }

}
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • You say "until k<=g" but your code has "while k<=g". I'm assuming your code is correct here but you should be consistent. – lc. Feb 18 '14 at 01:48

2 Answers2

2

since k = 75, and g = 0, since the condition is never true, that loop will never execute, and the print statement will not work, and the incrementation of k will never happen either.

change

while (k<=g){

to

while (k>=g){

To watch it fly out of control (run in an infinite loop), since k will always be greater than g.

Geremy
  • 2,415
  • 1
  • 23
  • 27
0

I don't recommend incrementing a while loop with floats.

Here is a link to why: Any risk of using float variables as loop counters and their fractional increment/decrement for non "==" conditions?

Also g is set to zero and never updated.

Community
  • 1
  • 1
cogle
  • 997
  • 1
  • 12
  • 25