18126

HW2ex1

Mar 13th, 2022 (edited)
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. double setVars(double* a, double* b, double* c, double* S, double* P) {
  5.     double semi;
  6.     if(*a > 0 && *b > 0 && *c > 0) {
  7.         *P = *a + *b + *c;
  8.         semi = *P / 2;
  9.         *S = sqrt(semi*(semi-*a)*(semi-*b)*(semi-*c));
  10.         return 0;
  11.     }
  12.     return 2;
  13. }
  14.  
  15. int main() {
  16.     double a, b, c;
  17.     double S = 0;
  18.     double P = 0;
  19.  
  20.     while(scanf("%lf %lf %lf", &a, &b, &c) != EOF) {
  21.         if(setVars(&a, &b, &c, &S, &P) == 0) {
  22.             printf("S: %lf cm2\n", S);
  23.             printf("P: %lf cm\n", P);
  24.         } else {
  25.             fprintf(stderr, "Invalid triangle sizes\n");
  26.         }
  27.     }
  28.  
  29.     return 0;
  30.  
  31. }
Add Comment
Please, Sign In to add comment