Given 3 integer line lengths. Determine if you
can construct a triangle from these lengths. If you can construct a
triangle, is it equilateral, isosceles or scalene? Is it a right triangle?
#include <iostream>
using namespace std;
void triangle (int, int, int);
bool is_valid (int, int, int);
bool is_triangle (int, int, int);
bool is_equilateral (int, int, int);
bool is_isosceles (int, int, int);
bool is_right (int, int, int);
int max (int, int);
int main()
{
triangle(0,1,2);
triangle(1,2,3);
triangle(3,3,3);
triangle(4,4,6);
triangle(2,3,4);
triangle(3,4,5);
system("pause");
return 0;
}
void triangle(int side1, int side2, int side3)
{
cout << "For sides "
<< side1 << ", "
<< side2 << ", "
<< side3 << " ";
if(is_valid(side1,side2,side3))
if(is_triangle(side1, side2, side3))
if(is_equilateral(side1, side2, side3))
cout << "Equilateral Triangle " << endl;
else if(is_isosceles(side1, side2, side3))
cout << "Isosceles Triangle" << endl;
else {
cout << "Scalene Triangle " << endl;
if(is_right(side1, side2, side3))
cout << "Also a right triangle" << endl;
}
else cout << "not a triangle" << endl;
else cout << "invalid input data " << endl;
}
bool is_valid(int a, int b, int c)
{
// true if input valid
bool result = a > 0 && b > 0 && c > 0;
return result;
}
bool is_triangle(int a, int b, int c)
{
// true if valid triangle line lengths
bool result = a + b > c && a + c > b && b + c > a;
return result;
}
bool is_equilateral(int a, int b, int c)
{
// true if equilateral triangle
bool result = a == b && b == c;
return result;
}
bool is_isosceles(int a, int b, int c)
{
// true is isosceles triangle
bool result = a == b || a == c || b == c;
result = result && !(is_equilateral(a,b,c));
return result;
}
bool is_right(int a, int b, int c)
{
// true if right triangle
int h; // hypotenuse
bool result;
h = max(a, max(b,c));
result = h * h == a * a + b * b + c * c - h * h;
return result;
}
int max(int x, int y)
{
// return the max of x and y
return x > y ? x : y;
}
For sides 0, 1, 2 invalid input data
For sides 1, 2, 3 not a triangle
For sides 3, 3, 3 Equilateral Triangle
For sides 4, 4, 6 Isosceles Triangle
For sides 2, 3, 4 Scalene Triangle
For sides 3, 4, 5 Scalene Triangle
Also a right triangle
Press any key to continue . . .
Zeller's Congruence.
To calculate the day of the week for a given date, Zeller's congruence
can be applied. A date is expressed as follows:
- "m"
month number, with January and February taken as months 11 and 12 of
the preceding year. Then March is 1, April is 2.. December is 10.
- "k"
day of the month
- "C"
The century
- "D"
The year in the century.
- "f"
The day of the week where 0 = Sunday, 1 = Monday ... 6 = Saturday
The congruence is
f={[2.6m-0.2]+k+D+[D/4]+[C/4]-2C} % 7
[] denotes the greatest integer in.
The formula relies on the mathematician's definition of modulo division,
which means that -2 mod 7 is equal to positive 5.
Unfortunately, the way most computer languages implement the remainder
function, -2 mod 7 returns a result of -2.
#include <iostream>
using namespace std;
void zeller(int, int, int);
int main()
{
zeller( 9,11, 2001); // 9-11
zeller(12,25, 2010); // Christmas
zeller( 7, 4, 1776); // 1st 4th
zeller(12, 7, 1941); // Pearl harbor
zeller( 1, 1, 2000); // Y2K
zeller( 6, 6, 1944); // D-Day
zeller( 7,20, 1969); // Apollo 11 landing
system("pause");
return 0;
}
void zeller(int month, int day, int year)
{
int century ,dow;
cout << month << "/"
<< day << "/"
<< year << " occurs on ";
month -= 2; // Subtract two from month
if (month < = 0) { // Prior year?
month += 12; // adjust month
year--; // adjust year
}
century = year / 100; // Extract century
year %= 100; // extract year in century
dow = ((int)(2.6 * month - 0.2) + // Zeller congruence
day + year + year / 4 +
century / 4 - 2 * century
) % 7;
dow += dow < 0 ? 7 : 0; // fix for negative number.
switch(dow) {
case 0 : cout << "Sunday" << endl; break;
case 1 : cout << "Monday" << endl; break;
case 2 : cout << "Tuesday" << endl; break;
case 3 : cout << "Wednesday" << endl; break;
case 4 : cout << "Thursday" << endl; break;
case 5 : cout << "Friday" << endl; break;
case 6 : cout << "Saturday" << endl;
}
}
9/11/2001 occurs on Tuesday
12/25/2010 occurs on Saturday
7/4/1776 occurs on Thursday
12/7/1941 occurs on Sunday
1/1/2000 occurs on Saturday
6/6/1944 occurs on Tuesday
7/20/1969 occurs on Sunday
Press any key to continue . . .