Skip to content

Instantly share code, notes, and snippets.

@sunho
Created January 14, 2022 02:24
Show Gist options
  • Save sunho/22d01938a6a4d5ba9de0ce42f5d7b3ec to your computer and use it in GitHub Desktop.
Save sunho/22d01938a6a4d5ba9de0ce42f5d7b3ec to your computer and use it in GitHub Desktop.
q4
/***************************************************
Exercise 1.4
Rewrite the following program using optimal C coding style.
Pay attention to:
- white spaces and indentation
- short-cut boolean expressions in if or loop statements
- use the conditional operator
- code redundancy
- proper use of the relational expression in a return statement
- use of the comma operator in a loop
- use of && and || to prevent if nesting
Name: Sunho Kim
***************************************************/
#include <stdio.h>
int checkTriangle(double a, double b, double c);
int main (void)
{
double a = 10, b = 2, c = 3;
printf("%.1f %.1f %.1f are ",a,b,c);
if (checkTriangle(a,b,c)==0)
printf("not ");
printf("the sides of a triangle!\n");
return 0;
}
/***************************************************
Checks if a, b, and c are the sides of
a triangle
PRE a, b, c are sides of triangle
POST return 0 if not triangle, 1 if triangle
*/
int checkTriangle(double a, double b, double c)
{
if (a <= 0)
return 0;
if (b <= 0)
return 0;
if (c <= 0)
return 0;
if (a >= b + c)
return 0;
if (b >= a + c)
return 0;
if (c >= a + b)
return 0;
return 1;
}
/** SAVE THE OUTPUT BELOW
10.0 2.0 3.0 are not the sides of a triangle!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment