1

I am trying to write a program that will find all acute triangle solutions after the user enters a min and max value (dmin and dmax). Right now I think I have the program working using only for() loops, but I need to change the first for() loop to a do{}while loop, which is confusing me. I'm not able to figure out how to write the do{}while loop so that is also includes these nested for() loops and the if statements. Everything I've tried either tells me b and c aren't being used or it just runs and provides no output. Here is my code with the for() loops.

double a = 0, b = 0, c = 0;
printf("Here are the acute triangle solutions the program found:\n");
  for (c = dmin; c <= dmax, c++)
  {
      for (b = dmin; b <= dmax; b++)
      {
          for (a = dmin; a <= dmax; a++)
          {
              if (a * a + b * b - c == c * c ){ //
                  if (((a + b) > c) && ((b + c) > a) && ((b + a) > b))
                  {printf("(%lf %lf %lf)\n", a, b, c);}  
                  //sum of two sides must be greater than the third
                  //and angle across from c must be <90 degrees
              }
          }
      }
  }
1
  • There is no one-to-one translation from a for-loop to a do while-loop without additional, redundant statements, as those are different by concept. A while-loop however could replace a for-loop seamlessly. Commented Oct 15, 2015 at 20:05

2 Answers 2

2

a for (e1;e2;e3) something loop can be transformed in:

e1;
while (e2) {
  something;
  e3;
}

or:

e1;
if (e2) {
  do {
    something;
    e3;
  } while (e2);
}
Sign up to request clarification or add additional context in comments.

Comments

1

In a do while, you simply do an initialization before and the check after. It is really somewhat similar to what the system does for a for loop.

The following code:

  for (c = dmin; c <= dmax, c++)
  {
      for (b = dmin; b <= dmax; b++)
      {
          for (a = dmin; a <= dmax; a++)
          {
              if (a * a + b * b - c == c * c ){ //
                  if (((a + b) > c) && ((b + c) > a) && ((c + a) > c))
                  {printf("(%lf %lf %lf)\n", a, b, c);}  
                  //sum of two sides must be greater than the third
                  //and angle across from c must be <90 degrees
              }
          }
      }
  }

Becomes:

  c = dmin;
  if(c < dmax) { //Make sure not to run once if c is greater
    do
    {
      for (b = dmin; b <= dmax; b++)
      {
          for (a = dmin; a <= dmax; a++)
          {
              if (a * a + b * b - c == c * c ){ //
                  if (((a + b) > c) && ((b + c) > a) && ((c + a) > b))
                  {printf("(%lf %lf %lf)\n", a, b, c);}  
                  //sum of two sides must be greater than the third
                  //and angle across from c must be <90 degrees
              }
          }
      }
    } while( ++c <= dmax );
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.