Home > Net >  if statement doesn't work on a triangle type function
if statement doesn't work on a triangle type function

Time:01-10

I'm a complete newbie, so please excuse me. I tried using online compiler but they unresponsive, and I get no return value (or return 0 for whatever I enter)

I tried to write a function that check if triangle is right, isosceles or both, and return 1,2,3 respectively, all other cases should return 0.

int main() {
    int TriangleType(unsigned angle1, unsigned angle2) {
        unsigned angleSum = angle1   angle2;
        if (angleSum >= 180) {
            return 0;
        } 
    
        /* if triangle is right ---> */ 
        if (angle1==90 || angle2==90 || angleSum==90) {
            /*if it is also  an isosceles --->*/
            if (angle2==45 || angle1==45) {
                return 3;
            }  
            return 1;
        }
           
        /*check if it only a isosceles*/
        if (angle1==(180-angle2)/2 || 
            angle2== (180-angle1)/2 || 
            angle1==angle2) {
            return 2;
        } 
                
        return 0;
    }
    
    TriangleType(110, 111);
}

CodePudding user response:

First, don't try to use nested functions in C. Pulling that function out of main.

int TriangleType(unsigned angle1, unsigned angle2) {
    unsigned angleSum = angle1   angle2;
    if (angleSum >= 180) {
        return 0;
    } 

    /* if triangle is right ---> */ 
    if (angle1==90 || angle2==90 || angleSum==90) {
        /*if it is also  an isosceles --->*/
        if (angle2==45 || angle1==45) {
            return 3;
        }  
        return 1;
    }
       
    /*check if it only a isosceles*/
    if (angle1==(180-angle2)/2 || 
        angle2== (180-angle1)/2 || 
        angle1==angle2) {
        return 2;
    } 
            
    return 0;
}

int main() {  
    TriangleType(110, 111);
}

Second, this doesn't do anything with the return value from the function, so of course you see no output.

int main(void) {  
    switch (TriangleType(110, 111)) {
        case 1:
        printf("Right triangle\n");
        break;
        case 2:
        printf("Isosceles triangle\n");
        break;
        case 3:
        printf("Both types\n");
        break;
        default:
        printf("None of the above\n");
    }

    return 0;
}

CodePudding user response:

Code is functionally wrong

Even after re-organizing and moving the nested function, code attempts to be clever and not perform simple tests.

  • if (angle1 == (180 - angle2) / 2 || angle2 == (180 - angle1) / 2 || angle1 == angle2) fails in cases like TriangleType(45,89) --> 2 as (180 - angle2) / 2 rounds down. angle1 * 2 == (180 - angle2) would make more sense.

  • TriangleType(90, 90), where the 3rd angle is 0, returns 0. That is asymmetric. TriangleType(0, 90), TriangleType(90, 0) do not return 0.

  • TriangleType(0, 0) returns 2. I would expect that to return 0, a rejected triangle.

Below is a test harness for OP to use and test TriangleType().

#include <stdio.h>

int TriangleType(unsigned angle1, unsigned angle2) {
  unsigned angleSum = angle1   angle2;
  if (angleSum >= 180) {
    return 0;
  }

  /* if triangle is right ---> */
  if (angle1 == 90 || angle2 == 90 || angleSum == 90) {
    /*if it is also  an isosceles --->*/
    if (angle2 == 45 || angle1 == 45) {
      return 3;
    }
    return 1;
  }

  /*check if it only a isosceles */
  if (angle1 == (180 - angle2) / 2 || angle2 == (180 - angle1) / 2
      || angle1 == angle2) {
    return 2;
  }

  return 0;
}

int my_TriangleType(unsigned angle1, unsigned angle2) {
  unsigned angle3 = 180 - angle1 - angle2;
  if (angle1 >= 180 || angle2 >= 180 || angle3 >= 180) {
    return 0;
  }
  if (angle1 == 0 || angle2 == 0 || angle3 == 0) {
    return 0;
  }
  int retval = angle1 == 90 || angle2 == 90 || angle3 == 90 ? 1 : 0;
  if (angle1 == angle2 || angle2 == angle3 || angle3 == angle1)
    retval  = 2;
  return retval;
}

#include <string.h>
#define MAX_LIMIT   127
int main() {
  printf("score:%d  my_score:%d\n",  TriangleType(45,89), my_TriangleType(45,89));
  int error_count = 0;
  int flags[4][4] = {0};
  for (unsigned angle1 = 0; angle1 <= 181; angle1  ) {
    for (unsigned angle2 = 0; angle2 <= 181; angle2  ) {
      int score = TriangleType(angle1, angle2);
      int my_score = my_TriangleType(angle1, angle2);
      if (score != my_score) {
        error_count  ;
        if (flags[score][my_score] == 0) {
          flags[score][my_score] = 1;
          printf("= angle1:%2u angle2:%2u  score:%d  my_score:%d\n", //
              error_count, angle1, angle2, score, my_score);
        }
      }
    }

  }
  printf("total errors:%d\n", error_count);
  return 0;
}

Output:

score:2  my_score:0
  1 angle1: 0 angle2: 0  score:2  my_score:0
  2 angle1: 0 angle2:90  score:1  my_score:0
total errors:181

another_TriangleType() could be made that is more efficient than my_TriangleType(), yet it would needed to functionally match.

CodePudding user response:

Well your questions is lacking some information, but I assume you got as inputs of your program, two angles.

THe fisrt thing we need to think is:

  • If you got a right triangle, than there is one of those values equals 90 degrees, instead the sum of them must be 90. On the other hand, if you got an isosceles triangle you should have 2 equal values, instead you must have the 180 deg - sum of both angles should result in one of those two.

Let's try to code this out:

#include <stdio.h>
#include <stdlib.h>

#define TRUE    1
#define FALSE   0

#define RIGHT     0x01
#define ISOSCELES 0x02
#define BOTH      0x03

// If you got a right triangle, than there is one of those values equals 90 degrees, instead the sum of them must be 90.
int bIsRightTriangle(int iAngDeg1, int iAngDeg2){

  if ( iAngDeg1 == 90 || iAngDeg2 == 90 )
    return TRUE;

  if ( (iAngDeg1   iAngDeg2) == 90 )
    return TRUE;
  
  return FALSE;
}
// On the other hand, if you got an isosceles triangle you should have 2 equal values,
// instead you must have the 180 deg - sum of both angles should result in one of those two.
int bIs_IsoscelesTriangle(int iAngDeg1, int iAngDeg2){
  int iAngDeg3 = 180 - iAngDeg1   iAngDeg2;
  if ( iAngDeg1 == iAngDeg2 )
    return TRUE;

  if ( (iAngDeg3 == iAngDeg1) || (iAngDeg3 == iAngDeg2) )
    return TRUE;
  
  return FALSE;
}
int iGetTriangleTypeByAngles(int iAngDeg1, int iAngDeg2){
  int iReturnType = 0;

  iReturnType = bIsRightTriangle(iAngDeg1, iAngDeg2)      ? (iReturnType | RIGHT)     : iReturnType;
  iReturnType = bIs_IsoscelesTriangle(iAngDeg1, iAngDeg2) ? (iReturnType | ISOSCELES) : iReturnType;

  return iReturnType;

}

int main(int argc, char *argv[]){

  if ( argc < 3 ){
    return -1;
  }

  // In case you want an output
  // printf("%d\n", iGetTriangleTypeByAngles(atoi(argv[1]), atoi(argv[2])));

  return iGetTriangleTypeByAngles(atoi(argv[1]), atoi(argv[2]));
}
  •  Tags:  
  • c
  • Related