Solution Manual for Engineering Problem Solving With C++, 4th Edition
Strengthen your problem-solving skills with Solution Manual for Engineering Problem Solving With C++, 4th Edition, your essential study tool.
Sophia Lee
Contributor
4.1
80
about 2 months ago
Preview (31 of 212)
Sign in to access the full document!
Exam Practice!
1. F
2. T
3. F
4. T
Multiple Choice Problems
5. (d)
6. (b)
7. (c)
8. (a)
9. (a)
Base Conversions
Binary to:
10. Octal
a. 665
b. 75157
c. 1271
d. 1001
e. 777
Binary to:
11. Hexadecimal
a. 86B
b. E7F5
c. 2B9
d. 8001
e. FFF
Decimal to:
12. Octal
a. 2414
b. 1137
c. 22075
d. 3641112
e. 3735707
13. Decimal
a. 213
b. 2546
c. 237810
d. 528
e. 279935
1. F
2. T
3. F
4. T
Multiple Choice Problems
5. (d)
6. (b)
7. (c)
8. (a)
9. (a)
Base Conversions
Binary to:
10. Octal
a. 665
b. 75157
c. 1271
d. 1001
e. 777
Binary to:
11. Hexadecimal
a. 86B
b. E7F5
c. 2B9
d. 8001
e. FFF
Decimal to:
12. Octal
a. 2414
b. 1137
c. 22075
d. 3641112
e. 3735707
13. Decimal
a. 213
b. 2546
c. 237810
d. 528
e. 279935
Exam Practice!
1. F
2. T
3. F
4. T
Multiple Choice Problems
5. (d)
6. (b)
7. (c)
8. (a)
9. (a)
Base Conversions
Binary to:
10. Octal
a. 665
b. 75157
c. 1271
d. 1001
e. 777
Binary to:
11. Hexadecimal
a. 86B
b. E7F5
c. 2B9
d. 8001
e. FFF
Decimal to:
12. Octal
a. 2414
b. 1137
c. 22075
d. 3641112
e. 3735707
13. Decimal
a. 213
b. 2546
c. 237810
d. 528
e. 279935
1. F
2. T
3. F
4. T
Multiple Choice Problems
5. (d)
6. (b)
7. (c)
8. (a)
9. (a)
Base Conversions
Binary to:
10. Octal
a. 665
b. 75157
c. 1271
d. 1001
e. 777
Binary to:
11. Hexadecimal
a. 86B
b. E7F5
c. 2B9
d. 8001
e. FFF
Decimal to:
12. Octal
a. 2414
b. 1137
c. 22075
d. 3641112
e. 3735707
13. Decimal
a. 213
b. 2546
c. 237810
d. 528
e. 279935
Exam Practice!
1. T
2. F
3. T
4. T
5. F
6. Not Correct. int i, j, k;
7. Correct.
8. Incorrect. double D1, D2, D3;
9. Correct.
10. Correct.
11. (d)
12. (b)
13. (a)
14. (c)
15. (e)
Memory Snapshots
16. x1=>2, z=>2, x=>2
17. x=>2, y=>1, a=>3.8, n=>2
Output
18. value_1 = 5.78263
19. Missing ; (value_4 = 6.645832e+01)
20. value_5 = 7750
Programming Exercises
/*--------------------------------------------------------------------*/
/* Problem chapter2_21 */
/* */
/* This program converts miles to kilometers. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, kilometers;
/* Enter number of miles from the keyboard. */
cout << "Enter the number of miles: \n";
cin >> miles;
/* Compute the number of kilometers equal to the specified miles. */
kilometers = 1.6093440*miles;
/* Print the number of kilometers. */
cout << miles << " miles = " << kilometers << " kilometers \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_22 */
/* */
/* This program converts meters to miles. */
1. T
2. F
3. T
4. T
5. F
6. Not Correct. int i, j, k;
7. Correct.
8. Incorrect. double D1, D2, D3;
9. Correct.
10. Correct.
11. (d)
12. (b)
13. (a)
14. (c)
15. (e)
Memory Snapshots
16. x1=>2, z=>2, x=>2
17. x=>2, y=>1, a=>3.8, n=>2
Output
18. value_1 = 5.78263
19. Missing ; (value_4 = 6.645832e+01)
20. value_5 = 7750
Programming Exercises
/*--------------------------------------------------------------------*/
/* Problem chapter2_21 */
/* */
/* This program converts miles to kilometers. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, kilometers;
/* Enter number of miles from the keyboard. */
cout << "Enter the number of miles: \n";
cin >> miles;
/* Compute the number of kilometers equal to the specified miles. */
kilometers = 1.6093440*miles;
/* Print the number of kilometers. */
cout << miles << " miles = " << kilometers << " kilometers \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_22 */
/* */
/* This program converts meters to miles. */
Exam Practice!
1. T
2. F
3. T
4. T
5. F
6. Not Correct. int i, j, k;
7. Correct.
8. Incorrect. double D1, D2, D3;
9. Correct.
10. Correct.
11. (d)
12. (b)
13. (a)
14. (c)
15. (e)
Memory Snapshots
16. x1=>2, z=>2, x=>2
17. x=>2, y=>1, a=>3.8, n=>2
Output
18. value_1 = 5.78263
19. Missing ; (value_4 = 6.645832e+01)
20. value_5 = 7750
Programming Exercises
/*--------------------------------------------------------------------*/
/* Problem chapter2_21 */
/* */
/* This program converts miles to kilometers. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, kilometers;
/* Enter number of miles from the keyboard. */
cout << "Enter the number of miles: \n";
cin >> miles;
/* Compute the number of kilometers equal to the specified miles. */
kilometers = 1.6093440*miles;
/* Print the number of kilometers. */
cout << miles << " miles = " << kilometers << " kilometers \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_22 */
/* */
/* This program converts meters to miles. */
1. T
2. F
3. T
4. T
5. F
6. Not Correct. int i, j, k;
7. Correct.
8. Incorrect. double D1, D2, D3;
9. Correct.
10. Correct.
11. (d)
12. (b)
13. (a)
14. (c)
15. (e)
Memory Snapshots
16. x1=>2, z=>2, x=>2
17. x=>2, y=>1, a=>3.8, n=>2
Output
18. value_1 = 5.78263
19. Missing ; (value_4 = 6.645832e+01)
20. value_5 = 7750
Programming Exercises
/*--------------------------------------------------------------------*/
/* Problem chapter2_21 */
/* */
/* This program converts miles to kilometers. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, kilometers;
/* Enter number of miles from the keyboard. */
cout << "Enter the number of miles: \n";
cin >> miles;
/* Compute the number of kilometers equal to the specified miles. */
kilometers = 1.6093440*miles;
/* Print the number of kilometers. */
cout << miles << " miles = " << kilometers << " kilometers \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_22 */
/* */
/* This program converts meters to miles. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, meters;
/* Enter number of meters from the keyboard. */
cout << "Enter the number of meters: \n";
cin >> meters;
/* Compute the number of miles equal to the specified meters. */
miles = meters/1609.3440;
/* Print the number of miles. */
cout << meters << " meters = "<< miles << " miles \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_23 */
/* */
/* This program converts pounds to kilograms. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double pounds, kilograms;
/* Enter number of pounds from the keyboard. */
cout << "Enter the number of pounds: ";
cin >> pounds;
/* Compute number of kilograms equal to the specified pounds. */
kilograms = pounds/2.205;
/* Print the number of kilograms. */
cout << pounds << " pounds = " << kilograms << " kilograms \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_24 */
/* */
/* This program converts newtons to pounds. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double miles, meters;
/* Enter number of meters from the keyboard. */
cout << "Enter the number of meters: \n";
cin >> meters;
/* Compute the number of miles equal to the specified meters. */
miles = meters/1609.3440;
/* Print the number of miles. */
cout << meters << " meters = "<< miles << " miles \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_23 */
/* */
/* This program converts pounds to kilograms. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double pounds, kilograms;
/* Enter number of pounds from the keyboard. */
cout << "Enter the number of pounds: ";
cin >> pounds;
/* Compute number of kilograms equal to the specified pounds. */
kilograms = pounds/2.205;
/* Print the number of kilograms. */
cout << pounds << " pounds = " << kilograms << " kilograms \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_24 */
/* */
/* This program converts newtons to pounds. */
#include <iostream>
Loading page 4...
using namespace std;
int main()
{
/* Declare variables. */
double pounds, newtons;
/* Enter number of newtons from the keyboard. */
cout << "Enter the number of newtons: ";
cin >> newtons;
/* Compute number of pounds equal to the specified newtons. */
pounds = newtons/4.448;
/* Print the number of pounds. */
cout << newtons << " newtons = " << pounds << " pounds \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_25 */
/* */
/* This program converts degrees Fahrenheit to degrees Rankin. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double degrees_F, degrees_R;
/* Enter temperture in degrees Fahrenheit from the keyboard. */
cout << "Enter the temperature in degrees Fahrenheit: ";
cin >> degrees_F;
/* Compute the equivalent temperature in degrees Rankin */
/* from the given temperature. */
degrees_R = degrees_F + 459.67;
/* Print the temperatures. */
cout << degrees_F << " degrees Fahrenheit = " << degrees_R << " degrees
Rankin \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_26 */
/* */
/* This program converts degrees Celsius to degrees Rankin. */
#include <iostream>
int main()
{
/* Declare variables. */
double pounds, newtons;
/* Enter number of newtons from the keyboard. */
cout << "Enter the number of newtons: ";
cin >> newtons;
/* Compute number of pounds equal to the specified newtons. */
pounds = newtons/4.448;
/* Print the number of pounds. */
cout << newtons << " newtons = " << pounds << " pounds \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_25 */
/* */
/* This program converts degrees Fahrenheit to degrees Rankin. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double degrees_F, degrees_R;
/* Enter temperture in degrees Fahrenheit from the keyboard. */
cout << "Enter the temperature in degrees Fahrenheit: ";
cin >> degrees_F;
/* Compute the equivalent temperature in degrees Rankin */
/* from the given temperature. */
degrees_R = degrees_F + 459.67;
/* Print the temperatures. */
cout << degrees_F << " degrees Fahrenheit = " << degrees_R << " degrees
Rankin \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_26 */
/* */
/* This program converts degrees Celsius to degrees Rankin. */
#include <iostream>
Loading page 5...
using namespace std;
int main()
{
/* Declare variables. */
double degrees_C, degrees_F, degrees_R;
/* Enter temperture in degrees Celsius from the keyboard. */
cout << "Enter the temperature in degrees Celsius: \n";
cin >> degrees_C;
/* Compute the equivalent temperature in degrees Rankin */
/* from the given temperature. */
degrees_F = (9.0/5.0)*degrees_C + 32;
degrees_R = degrees_F + 459.67;
/* Print the temperatures. */
cout << degrees_C << " degrees Celsius = " << degrees_R << " degrees
Rankin \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_27 */
/* */
/* This program converts degrees Kelvin to degrees Fahrenheit. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double degrees_R, degrees_K, degrees_F;
/* Enter temperture in degrees Kelvin from the keyboard. */
cout << "Enter the temperature in degrees Kelvin: \n";
cin >> degrees_K;
/* Compute the equivalent temperature in degrees Fahrenheit */
/* from the given temperature. */
degrees_R = (9.0/5.0)*degrees_K;
degrees_F = degrees_R - 459.67;
/* Print the temperatures. */
cout << degrees_K << " degrees Kelvin = " << degrees_F << " degrees
Fahrenheit \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
int main()
{
/* Declare variables. */
double degrees_C, degrees_F, degrees_R;
/* Enter temperture in degrees Celsius from the keyboard. */
cout << "Enter the temperature in degrees Celsius: \n";
cin >> degrees_C;
/* Compute the equivalent temperature in degrees Rankin */
/* from the given temperature. */
degrees_F = (9.0/5.0)*degrees_C + 32;
degrees_R = degrees_F + 459.67;
/* Print the temperatures. */
cout << degrees_C << " degrees Celsius = " << degrees_R << " degrees
Rankin \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_27 */
/* */
/* This program converts degrees Kelvin to degrees Fahrenheit. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double degrees_R, degrees_K, degrees_F;
/* Enter temperture in degrees Kelvin from the keyboard. */
cout << "Enter the temperature in degrees Kelvin: \n";
cin >> degrees_K;
/* Compute the equivalent temperature in degrees Fahrenheit */
/* from the given temperature. */
degrees_R = (9.0/5.0)*degrees_K;
degrees_F = degrees_R - 459.67;
/* Print the temperatures. */
cout << degrees_K << " degrees Kelvin = " << degrees_F << " degrees
Fahrenheit \n";
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
Loading page 6...
/* Problem chapter2_28 */
/* */
/* This program finds the area of a rectangle. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double a, b, area;
/* Enter the lengths of sides of the rectangle. */
cout << "Enter the lengths of the sides of the rectangle: ";
cin >> a >> b;
/* Compute the area of the rectangle. */
area = a*b;
/* Print the value of the area. */
cout << "The area of a rectangle with sides " << a << " and " << b
<< " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_29 */
/* */
/* This program finds the area of a triangle. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double h, b, area;
/* Enter the base and the height of the triangle. */
cout << "Enter the base and the height of the triangle: ";
cin >> b >> h;
/* Compute the area of the triangle. */
area = 0.5*b*h;
/* Print the value of the area. */
cout << "The area of a triangle with base " << b << " and height " << h
<< "is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* */
/* This program finds the area of a rectangle. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double a, b, area;
/* Enter the lengths of sides of the rectangle. */
cout << "Enter the lengths of the sides of the rectangle: ";
cin >> a >> b;
/* Compute the area of the rectangle. */
area = a*b;
/* Print the value of the area. */
cout << "The area of a rectangle with sides " << a << " and " << b
<< " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_29 */
/* */
/* This program finds the area of a triangle. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double h, b, area;
/* Enter the base and the height of the triangle. */
cout << "Enter the base and the height of the triangle: ";
cin >> b >> h;
/* Compute the area of the triangle. */
area = 0.5*b*h;
/* Print the value of the area. */
cout << "The area of a triangle with base " << b << " and height " << h
<< "is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Loading page 7...
/*--------------------------------------------------------------------*/
/* Problem chapter2_30 */
/* */
/* This program finds the area of a circle. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, area;
/* Enter the radius. */
cout << "Enter the radius of the circle: ";
cin >> r;
/* Compute the area of the circle. */
area = PI*r*r;
/* Print the value of the area. */
cout << "The area of a circle with radius " << r << " is "
<< area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_31 */
/* */
/* This program computes the area of a sector of a circle when */
/* theta (u) is the angle in radians between the radii. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double u, r, area;
/* Enter the lengths of the radii and */
/* the angle between them. */
cout << "Enter the length of the radii and the angle "
<< "(in radians) between them: ";
cin >> r >> u;
/* Compute the area of the sector. */
area = (r*r*u)/2.0;
/* Print the value of the area. */
cout << "The area of sector is " << area << endl;
/* Problem chapter2_30 */
/* */
/* This program finds the area of a circle. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, area;
/* Enter the radius. */
cout << "Enter the radius of the circle: ";
cin >> r;
/* Compute the area of the circle. */
area = PI*r*r;
/* Print the value of the area. */
cout << "The area of a circle with radius " << r << " is "
<< area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_31 */
/* */
/* This program computes the area of a sector of a circle when */
/* theta (u) is the angle in radians between the radii. */
#include <iostream>
using namespace std;
int main()
{
/* Declare variables. */
double u, r, area;
/* Enter the lengths of the radii and */
/* the angle between them. */
cout << "Enter the length of the radii and the angle "
<< "(in radians) between them: ";
cin >> r >> u;
/* Compute the area of the sector. */
area = (r*r*u)/2.0;
/* Print the value of the area. */
cout << "The area of sector is " << area << endl;
Loading page 8...
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_32 */
/* */
/* This program computes the area of a sector of a circle when */
/* the input (d) is the angle in degrees between the radii. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double d, r, area, theta;
/* Enter the lengths of the radii and */
/* the angle between them. */
cout << "Enter the length of the radii and the angle "
<< "(in degrees) between them: ";
cin >> r >> d;
/* Compute the value of the angle in radians. */
theta = d * PI / 180;
/* Compute the area of the sector. */
area = (r*r*theta)/2.0;
/* Print the value of the area. */
cout << "The area of sector is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_33 */
/* */
/* This program computes the area of an */
/* ellipse with semiaxes a and b. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_32 */
/* */
/* This program computes the area of a sector of a circle when */
/* the input (d) is the angle in degrees between the radii. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double d, r, area, theta;
/* Enter the lengths of the radii and */
/* the angle between them. */
cout << "Enter the length of the radii and the angle "
<< "(in degrees) between them: ";
cin >> r >> d;
/* Compute the value of the angle in radians. */
theta = d * PI / 180;
/* Compute the area of the sector. */
area = (r*r*theta)/2.0;
/* Print the value of the area. */
cout << "The area of sector is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_33 */
/* */
/* This program computes the area of an */
/* ellipse with semiaxes a and b. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
Loading page 9...
double a, b, area;
/* Enter the length of the semiaxes. */
cout << "Enter the length of the semiaxes: ";
cin >> a >> b;
/* Compute the area of the ellipse. */
area = PI*a*b;
/* Print the value of the area. */
cout << "The area of an ellipse with semiaxes " << a << " and "
<< b << " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_34 */
/* */
/* This program computes the area of the surface */
/* of a sphere of radius r. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, area;
/* Enter the radius of the sphere. */
cout << "Enter the radius of the sphere: ";
cin >> r;
/* Compute the area of the sphere. */
area = 4.0*PI*r*r;
/* Print the value of the area. */
cout << "The area of a sphere with radius " << r
<< " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_35 */
/* */
/* This program computes the volume */
/* of a sphere of radius r. */
#include <iostream>
/* Enter the length of the semiaxes. */
cout << "Enter the length of the semiaxes: ";
cin >> a >> b;
/* Compute the area of the ellipse. */
area = PI*a*b;
/* Print the value of the area. */
cout << "The area of an ellipse with semiaxes " << a << " and "
<< b << " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_34 */
/* */
/* This program computes the area of the surface */
/* of a sphere of radius r. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, area;
/* Enter the radius of the sphere. */
cout << "Enter the radius of the sphere: ";
cin >> r;
/* Compute the area of the sphere. */
area = 4.0*PI*r*r;
/* Print the value of the area. */
cout << "The area of a sphere with radius " << r
<< " is " << area << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_35 */
/* */
/* This program computes the volume */
/* of a sphere of radius r. */
#include <iostream>
Loading page 10...
#include <cmath>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, volume;
/* Enter the radius of the sphere. */
cout << "Enter the radius of the sphere: ";
cin >> r;
/* Compute the volume of the sphere. */
volume = (4.0/3)*PI*pow(r,3);
/* Print the value of the volume. */
cout << "The volume of a sphere with radius " << r
<< " is " << volume << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_36 */
/* */
/* This program computes the volume of a cylinder */
/* of radius r and height h. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, h, volume;
/* Enter the radius and height of the cylinder. */
cout << "Enter the radius and the height of the cylinder: ";
cin >> r >> h;
/* Compute the volume of the cylinder. */
volume = PI*r*r*h;
/* Print the volume. */
cout << "The volume of a cylinder of radius " << r << " and "
<< "height " << h << " is " << volume << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, volume;
/* Enter the radius of the sphere. */
cout << "Enter the radius of the sphere: ";
cin >> r;
/* Compute the volume of the sphere. */
volume = (4.0/3)*PI*pow(r,3);
/* Print the value of the volume. */
cout << "The volume of a sphere with radius " << r
<< " is " << volume << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_36 */
/* */
/* This program computes the volume of a cylinder */
/* of radius r and height h. */
#include <iostream>
using namespace std;
const double PI = 3.141593;
int main()
{
/* Declare variables. */
double r, h, volume;
/* Enter the radius and height of the cylinder. */
cout << "Enter the radius and the height of the cylinder: ";
cin >> r >> h;
/* Compute the volume of the cylinder. */
volume = PI*r*r*h;
/* Print the volume. */
cout << "The volume of a cylinder of radius " << r << " and "
<< "height " << h << " is " << volume << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Loading page 11...
/*--------------------------------------------------------------------*/
/* Problem chapter2_37 */
/* */
/* This program computes the molecular weight of the */
/* amino acid glycine. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
int main()
{
/* Declare variables. */
double molecular_weight;
/* Compute the molecular weight of glycine. */
molecular_weight = (2*OXYGEN) + (2*CARBON) +
NITROGEN + (5*HYDROGEN);
/* Print the molecular weight. */
cout << "The molecular weight of glycine is " << molecular_weight << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_38 */
/* */
/* This program computes the molecular weights of the */
/* amino acids glutamic and glutamine. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
int main()
{
/* Declare variables. */
double mol_weight_glutamic, mol_weight_glutamine;
/* Compute the molecular weights. */
mol_weight_glutamic = (4*OXYGEN) + (5*CARBON) +
/* Problem chapter2_37 */
/* */
/* This program computes the molecular weight of the */
/* amino acid glycine. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
int main()
{
/* Declare variables. */
double molecular_weight;
/* Compute the molecular weight of glycine. */
molecular_weight = (2*OXYGEN) + (2*CARBON) +
NITROGEN + (5*HYDROGEN);
/* Print the molecular weight. */
cout << "The molecular weight of glycine is " << molecular_weight << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_38 */
/* */
/* This program computes the molecular weights of the */
/* amino acids glutamic and glutamine. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
int main()
{
/* Declare variables. */
double mol_weight_glutamic, mol_weight_glutamine;
/* Compute the molecular weights. */
mol_weight_glutamic = (4*OXYGEN) + (5*CARBON) +
Loading page 12...
NITROGEN + (8*HYDROGEN);
mol_weight_glutamine = (3*OXYGEN) + (5*CARBON) +
(2*NITROGEN) + (10*HYDROGEN);
/* Print the molecular weights. */
cout << "The molecular weight of glutamic is " << mol_weight_glutamic
<< endl;
cout << "The molecular weight of glutamine is " << mol_weight_glutamine
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_39 */
/* */
/* This program computes the molecular weight of a particular */
/* amino acid given the number of atoms for each of the five */
/* elements found in the amino acid. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
const double SULFUR = 32.066;
int main()
{
/* Declare variable. */
int no_oxy, no_carbon, no_nitro, no_hydro, no_sulfur;
double molecular_weight;
/* Enter the number of atoms for each of the five elements. */
cout << "Enter the number of oxygen atoms found "
"in the amino acid. \n";
cin >> no_oxy;
cout << "Enter the number of carbon atoms. \n";
cin >> no_carbon;
cout << "Enter the number of nitrogen atoms. \n";
cin >> no_nitro;
cout << "Enter the number of sulfur atoms. \n";
cin >> no_sulfur;
cout << "Enter the number of hydrogen atoms. \n";
cin >> no_hydro;
/* Compute the molecular weight. */
molecular_weight = (no_oxy*OXYGEN) + (no_carbon*CARBON) +
(no_nitro*NITROGEN) + (no_sulfur*SULFUR) +
(no_hydro*HYDROGEN);
mol_weight_glutamine = (3*OXYGEN) + (5*CARBON) +
(2*NITROGEN) + (10*HYDROGEN);
/* Print the molecular weights. */
cout << "The molecular weight of glutamic is " << mol_weight_glutamic
<< endl;
cout << "The molecular weight of glutamine is " << mol_weight_glutamine
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_39 */
/* */
/* This program computes the molecular weight of a particular */
/* amino acid given the number of atoms for each of the five */
/* elements found in the amino acid. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
const double SULFUR = 32.066;
int main()
{
/* Declare variable. */
int no_oxy, no_carbon, no_nitro, no_hydro, no_sulfur;
double molecular_weight;
/* Enter the number of atoms for each of the five elements. */
cout << "Enter the number of oxygen atoms found "
"in the amino acid. \n";
cin >> no_oxy;
cout << "Enter the number of carbon atoms. \n";
cin >> no_carbon;
cout << "Enter the number of nitrogen atoms. \n";
cin >> no_nitro;
cout << "Enter the number of sulfur atoms. \n";
cin >> no_sulfur;
cout << "Enter the number of hydrogen atoms. \n";
cin >> no_hydro;
/* Compute the molecular weight. */
molecular_weight = (no_oxy*OXYGEN) + (no_carbon*CARBON) +
(no_nitro*NITROGEN) + (no_sulfur*SULFUR) +
(no_hydro*HYDROGEN);
Loading page 13...
/* Print the molecular weight. */
cout << "The molecular weight of this particular amino acid is "
<< molecular_weight << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_40 */
/* */
/* This program computes the average atomic weight of the atoms */
/* found in a particular amino acid given the number of atoms for */
/* each of the five elements found in amino acid. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
const double SULFUR = 32.066;
int main()
{
/* Declare variables. */
int no_oxy, no_carbon, no_nitro, no_hydro, no_sulfur, total_no;
double average_atomic_weight;
/* Enter the number of atoms for each of the five elements. */
cout << "Enter the number of oxygen atoms found "
<< " in the amino acid. \n";
cin >> no_oxy;
cout << "Enter the number of carbon atoms. \n";
cin >> no_carbon;
cout << "Enter the number of nitrogen atoms. \n";
cin >> no_nitro;
cout << "Enter the number of sulfur atoms. \n";
cin >> no_sulfur;
cout << "Enter the number of hydrogen atoms. \n";
cin >> no_hydro;
/* Compute the average weight of the atoms. */
total_no = no_oxy + no_carbon + no_nitro + no_sulfur + no_hydro;
average_atomic_weight = ((no_oxy*OXYGEN) + (no_carbon*CARBON) +
(no_nitro*NITROGEN) + (no_sulfur*SULFUR) +
(no_hydro*HYDROGEN))/total_no;
/* Print the average atomic weight. */
cout << "The average weight of the atoms in this particular amino "
<< "acid is " << average_atomic_weight << endl;
/* Exit program. */
return 0;
cout << "The molecular weight of this particular amino acid is "
<< molecular_weight << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_40 */
/* */
/* This program computes the average atomic weight of the atoms */
/* found in a particular amino acid given the number of atoms for */
/* each of the five elements found in amino acid. */
#include <iostream>
using namespace std;
/* Defines symbolic constants for the appropriate atomic weights. */
const double OXYGEN = 15.9994;
const double CARBON = 12.011;
const double NITROGEN = 14.00674;
const double HYDROGEN = 1.00794;
const double SULFUR = 32.066;
int main()
{
/* Declare variables. */
int no_oxy, no_carbon, no_nitro, no_hydro, no_sulfur, total_no;
double average_atomic_weight;
/* Enter the number of atoms for each of the five elements. */
cout << "Enter the number of oxygen atoms found "
<< " in the amino acid. \n";
cin >> no_oxy;
cout << "Enter the number of carbon atoms. \n";
cin >> no_carbon;
cout << "Enter the number of nitrogen atoms. \n";
cin >> no_nitro;
cout << "Enter the number of sulfur atoms. \n";
cin >> no_sulfur;
cout << "Enter the number of hydrogen atoms. \n";
cin >> no_hydro;
/* Compute the average weight of the atoms. */
total_no = no_oxy + no_carbon + no_nitro + no_sulfur + no_hydro;
average_atomic_weight = ((no_oxy*OXYGEN) + (no_carbon*CARBON) +
(no_nitro*NITROGEN) + (no_sulfur*SULFUR) +
(no_hydro*HYDROGEN))/total_no;
/* Print the average atomic weight. */
cout << "The average weight of the atoms in this particular amino "
<< "acid is " << average_atomic_weight << endl;
/* Exit program. */
return 0;
Loading page 14...
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_41 */
/* */
/* This program reads in a positive number and then computes */
/* the logarithm of that value to the base 2. */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
double x, answer;
/* Enter a positive number. */
cout << "Enter a positive number: ";
cin >> x;
/* Compute the logarithm to base 2. */
answer = log(x)/log(2.0);
/* Print the answer. */
cout << "The logarithm of " << x << " to the base 2 is " << answer
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_42 */
/* */
/* This program reads in a positive number and then computes */
/* the logarithm of that value to the base 8. */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
double x, answer;
/* Enter a positive number. */
cout << "Enter a positive number: ";
cin >> x;
/* Compute the logarithm to base 8. */
answer = log(x)/log(8.0);
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_41 */
/* */
/* This program reads in a positive number and then computes */
/* the logarithm of that value to the base 2. */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
double x, answer;
/* Enter a positive number. */
cout << "Enter a positive number: ";
cin >> x;
/* Compute the logarithm to base 2. */
answer = log(x)/log(2.0);
/* Print the answer. */
cout << "The logarithm of " << x << " to the base 2 is " << answer
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter2_42 */
/* */
/* This program reads in a positive number and then computes */
/* the logarithm of that value to the base 8. */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
double x, answer;
/* Enter a positive number. */
cout << "Enter a positive number: ";
cin >> x;
/* Compute the logarithm to base 8. */
answer = log(x)/log(8.0);
Loading page 15...
/* Print the answer. */
cout << "The logarithm of " << x << " to the base 8 is " << answer
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
cout << "The logarithm of " << x << " to the base 8 is " << answer
<< endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Loading page 16...
Exam Practice!
1. T
2. F
3. T
4. F
5. T
Syntax Problems
6. switch condition (sqrt(x)) is not an integer.
Multiple Choice
7. (c)
8. (b)
9. (a)
10. (a)
11. (b)
12. (b)
13. (b)
Memory Snapshot
14. n => 0, count => 2
15. n => -1, count => 1
16. mon => 1, ch => J
17. mon => 1, ch => F
18. n => 0, count => -1
19. a => 1500
20. ch => *
Programming Exercises
Boolean Expressions
/*--------------------------------------------------------------------*/
/* Program chapter3_21
This program receives three boolean values from standard input then
determines if the condition ! (a&&b&&c) && !(a||b||c) is true or false.
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
cout << "enter three boolean values (0 or 1)";
cin >> a >> b >> c;
cout << "you entered " << a << "," << b << "," << c << endl;
//Print heading and condition
cout << "The condition:\n" << endl;
cout << "! (" << a << "&&" << b << "&&" << c <<" ) && !("
<< a << "||" << b << "||" << c << ")\n\n";
//Determine if condition is true of false
if(! (a&&b&&c) && !(a||b||c) )
{
cout << "is true." << endl;
}
else
{
cout << "is false." << endl;
}
return 0;
1. T
2. F
3. T
4. F
5. T
Syntax Problems
6. switch condition (sqrt(x)) is not an integer.
Multiple Choice
7. (c)
8. (b)
9. (a)
10. (a)
11. (b)
12. (b)
13. (b)
Memory Snapshot
14. n => 0, count => 2
15. n => -1, count => 1
16. mon => 1, ch => J
17. mon => 1, ch => F
18. n => 0, count => -1
19. a => 1500
20. ch => *
Programming Exercises
Boolean Expressions
/*--------------------------------------------------------------------*/
/* Program chapter3_21
This program receives three boolean values from standard input then
determines if the condition ! (a&&b&&c) && !(a||b||c) is true or false.
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
cout << "enter three boolean values (0 or 1)";
cin >> a >> b >> c;
cout << "you entered " << a << "," << b << "," << c << endl;
//Print heading and condition
cout << "The condition:\n" << endl;
cout << "! (" << a << "&&" << b << "&&" << c <<" ) && !("
<< a << "||" << b << "||" << c << ")\n\n";
//Determine if condition is true of false
if(! (a&&b&&c) && !(a||b||c) )
{
cout << "is true." << endl;
}
else
{
cout << "is false." << endl;
}
return 0;
Loading page 17...
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Program chapter3_22
This program receives three boolean values from standard input then
determines if the condition !(xx||xx)&&xx
is true or false.
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
cout << "enter three boolean values (0 or 1)";
cin >> a >> b >> c;
if(!cin.fail())
{
cout << "you entered " << a << "," << b << "," << c << endl;
//Print heading and condition
cout << "The condition:\n" << endl;
cout << "! (" << a << "||" << b << ")&&" << c << "\n\n";
//Determine if condition is true of false
if( !(a||b)&&c )
{
cout << "is true." << endl;
}
else
{
cout << "is false." << endl;
}
}
return 0;
}
/*--------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Program chapter3_23 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condtion is a tautology */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool tautology(true); //assume true
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Program chapter3_22
This program receives three boolean values from standard input then
determines if the condition !(xx||xx)&&xx
is true or false.
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
cout << "enter three boolean values (0 or 1)";
cin >> a >> b >> c;
if(!cin.fail())
{
cout << "you entered " << a << "," << b << "," << c << endl;
//Print heading and condition
cout << "The condition:\n" << endl;
cout << "! (" << a << "||" << b << ")&&" << c << "\n\n";
//Determine if condition is true of false
if( !(a||b)&&c )
{
cout << "is true." << endl;
}
else
{
cout << "is false." << endl;
}
}
return 0;
}
/*--------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Program chapter3_23 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condtion is a tautology */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool tautology(true); //assume true
Loading page 18...
bool condition = (A && B || B && C);
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(!condition) tautology = false;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(tautology) cout << "This condition is a tautology." << endl;
else cout << "This condtion is not a tautology." << endl;
return 0;
}
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(!condition) tautology = false;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(tautology) cout << "This condition is a tautology." << endl;
else cout << "This condtion is not a tautology." << endl;
return 0;
}
Loading page 19...
/*-------------------------------------------------------------------*/
/* Program chapter3_24 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condition is a contradiction */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool contradiction(true); //assume true
bool condition = (A && B || B && C);
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition) contradiction = false;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition) contradiction = false;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
/* Program chapter3_24 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condition is a contradiction */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool contradiction(true); //assume true
bool condition = (A && B || B && C);
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition) contradiction = false;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition) contradiction = false;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
Loading page 20...
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(contradiction) cout << "This condition is a contradiction." << endl;
else cout << "This condtion is not a contradiction." << endl;
return 0;
}
/*--------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Program chapter3_25 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condition is a contingency */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool condition = (A && B || B && C);
bool first(condition), contingency(false); //set to first value
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition != first) contingency = true;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition) contradiction = false;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(contradiction) cout << "This condition is a contradiction." << endl;
else cout << "This condtion is not a contradiction." << endl;
return 0;
}
/*--------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Program chapter3_25 generates a truth table for the condition: */
/* A && B || B && C */
/* and determines if the condition is a contingency */
#include <iostream>
using namespace std;
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool condition = (A && B || B && C);
bool first(condition), contingency(false); //set to first value
//Print table header
cout << "TABLE 3.2\n A\tB\tC\t\tA && B || B && C" << endl;
cout << "_________________________________________________" << endl;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(condition != first) contingency = true;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;
//Toggle C
Loading page 21...
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(contingency) cout << "This condition is a contingency." << endl;
else cout << "This condtion is not a contingency." << endl;
return 0;
/*--------------------------------------------------------------------*/
Structured Programming. if Statements
/*Program chapter3_26 */
/*This program prompts the user to enter a value. */
/*The input value is printed along with a message. */
/*The message states states whether the input value is */
/* less than zero or greater than zero */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1;
cout << "Enter a value ";
cin >> n1;
cout << "You entered " << n1 << endl;
if(n1 > 0)
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle A, B and C
A = !A;
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Repeat the pattern for B and C..
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && C);
if(condition !=first) contingency = true;
cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;
if(contingency) cout << "This condition is a contingency." << endl;
else cout << "This condtion is not a contingency." << endl;
return 0;
/*--------------------------------------------------------------------*/
Structured Programming. if Statements
/*Program chapter3_26 */
/*This program prompts the user to enter a value. */
/*The input value is printed along with a message. */
/*The message states states whether the input value is */
/* less than zero or greater than zero */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1;
cout << "Enter a value ";
cin >> n1;
cout << "You entered " << n1 << endl;
if(n1 > 0)
Loading page 22...
cout << n1 << " is greater than zero." << endl;
else if(n1 < 0)
cout << n1 << " is less than zero." << endl;
return 0;
}
/*Program chapter3_27 */
/*This program prompts the user to enter two numbers. */
/*The input values are printed along with */
/*difference between the two numbers. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2,diff;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1 > n2)
{
diff = n1 - n2;
cout << n1 << " - " << n2 << " = " << diff << endl;
}
return 0;
}
/*Program chapter3_28 */
/*This program prompts the user to enter two numbers. */
/*The input values are printed followed by */
/*the value of the smaller of the two values. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1 < n2)
{
cout << n1 << " is less than " << n2 << endl;
}
else if(n1 > n2)
{
cout << n2 << " is less than " << n1 << endl;
}
return 0;
else if(n1 < 0)
cout << n1 << " is less than zero." << endl;
return 0;
}
/*Program chapter3_27 */
/*This program prompts the user to enter two numbers. */
/*The input values are printed along with */
/*difference between the two numbers. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2,diff;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1 > n2)
{
diff = n1 - n2;
cout << n1 << " - " << n2 << " = " << diff << endl;
}
return 0;
}
/*Program chapter3_28 */
/*This program prompts the user to enter two numbers. */
/*The input values are printed followed by */
/*the value of the smaller of the two values. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1 < n2)
{
cout << n1 << " is less than " << n2 << endl;
}
else if(n1 > n2)
{
cout << n2 << " is less than " << n1 << endl;
}
return 0;
Loading page 23...
}
/*Program chapter3_29 */
/*This program prompts the user to enter three numbers. */
/*The input values are printed followed by */
/*the value of the largest number. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2,n3;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
cout << "Enter third number ";
cin >> n3;
cout << "You entered " << n3 << endl;
if(n1 > n2)
{
if(n1 > n3)
cout << n1 << " is the largest value entered." << endl;
else
cout << n3 << " is the largest value entered." << endl;
}
else
{
cout << n3 << " is the largest value entered " << endl;
}
return 0;
}
/*Program chapter3_30 */
/*This program prompts the user to enter two integers */
/* The input values are printed followed by */
/* the value of true if the first number is */
/* evenly divisible by the second number */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int n1,n2;
cout << "Enter first integer ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second integer ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1%n2)
cout << n1 << " is not evenly divisible by " << n2 << endl;
else
cout << n1 << " is evenly divisible by " << n2 << endl;
return 0;
/*Program chapter3_29 */
/*This program prompts the user to enter three numbers. */
/*The input values are printed followed by */
/*the value of the largest number. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2,n3;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
cout << "Enter third number ";
cin >> n3;
cout << "You entered " << n3 << endl;
if(n1 > n2)
{
if(n1 > n3)
cout << n1 << " is the largest value entered." << endl;
else
cout << n3 << " is the largest value entered." << endl;
}
else
{
cout << n3 << " is the largest value entered " << endl;
}
return 0;
}
/*Program chapter3_30 */
/*This program prompts the user to enter two integers */
/* The input values are printed followed by */
/* the value of true if the first number is */
/* evenly divisible by the second number */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int n1,n2;
cout << "Enter first integer ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second integer ";
cin >> n2;
cout << "You entered " << n2 << endl;
if(n1%n2)
cout << n1 << " is not evenly divisible by " << n2 << endl;
else
cout << n1 << " is evenly divisible by " << n2 << endl;
return 0;
Loading page 24...
}
/*Program chapter3_31 */
/* This program prompts the user to enter two characters */
/* The input characters are printed in alphabetical order. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char ch1,ch2;
cout << "Enter first character ";
cin >> ch1;
cout << "You entered " << ch1 << endl;
cout << "Enter second character ";
cin >> ch2;
cout << "You entered " << ch2 << endl;
cout << "In alphabetical order: " << endl;
if(ch1 <= ch2)
cout << ch1 << " " << ch2 << endl;
else
cout << ch2 << " " << ch1 << endl;
return 0;
}
/*Program chapter3_32 */
/* This program prompts the user to enter three characters */
/* The input characters are printed in alphabetical order. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char ch1,ch2,ch3;
cout << "Enter first character ";
cin >> ch1;
cout << "You entered " << ch1 << endl;
cout << "Enter second character ";
cin >> ch2;
cout << "You entered " << ch2 << endl;
cout << "Enter third character ";
cin >> ch3;
cout << "You entered " << ch3 << endl;
cout << "In alphabetical order: " << endl;
if(ch1 <= ch2 && ch1 <= ch3) //ch1 is the smallest
{
cout << ch1 << " ";
if(ch2 < ch3)
cout << ch2 << " "<< ch3;
else
cout << ch3 << " "<< ch2;
}
else if(ch2 <= ch1 && ch2 <= ch3) //ch2 is the smallest
{
cout << ch2 << " ";
if(ch3 < ch1)
/*Program chapter3_31 */
/* This program prompts the user to enter two characters */
/* The input characters are printed in alphabetical order. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char ch1,ch2;
cout << "Enter first character ";
cin >> ch1;
cout << "You entered " << ch1 << endl;
cout << "Enter second character ";
cin >> ch2;
cout << "You entered " << ch2 << endl;
cout << "In alphabetical order: " << endl;
if(ch1 <= ch2)
cout << ch1 << " " << ch2 << endl;
else
cout << ch2 << " " << ch1 << endl;
return 0;
}
/*Program chapter3_32 */
/* This program prompts the user to enter three characters */
/* The input characters are printed in alphabetical order. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char ch1,ch2,ch3;
cout << "Enter first character ";
cin >> ch1;
cout << "You entered " << ch1 << endl;
cout << "Enter second character ";
cin >> ch2;
cout << "You entered " << ch2 << endl;
cout << "Enter third character ";
cin >> ch3;
cout << "You entered " << ch3 << endl;
cout << "In alphabetical order: " << endl;
if(ch1 <= ch2 && ch1 <= ch3) //ch1 is the smallest
{
cout << ch1 << " ";
if(ch2 < ch3)
cout << ch2 << " "<< ch3;
else
cout << ch3 << " "<< ch2;
}
else if(ch2 <= ch1 && ch2 <= ch3) //ch2 is the smallest
{
cout << ch2 << " ";
if(ch3 < ch1)
Loading page 25...
cout << ch3 << " "<< ch1;
else
cout << ch1 << " "<< ch3;
}
else //ch3 is the smallest
{
cout << ch3 << " ";
if(ch2 < ch1) cout << ch2 << " " << ch1 << endl;
else cout << ch1 << " " << ch2 << endl;
}
return 0;
}
/*Program chapter3_33 */
/*This program prompts the user to enter a character */
/*and determines if the input character is a vowel(a,e,i,o,u) */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char letter;
cout << "Enter letter ";
cin >> letter;
cout << "You entered " << letter << endl;
switch(letter)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
cout << letter << " is a vowel. " << endl;
break;
default:
cout << letter << " is not a vowel. " << endl;
break;
}
return 0;
}
/*Program chapter3_34 */
/*This program prompts the user to enter two numbers */
/*and a character that represents an arithmetic operator. */
/* * => multiply */
/* + => addition */
/* - => subtraction */
/* / => division */
/* The input values are printed followed by */
/* the result of the operation or an error message */
/* if the character is not one of the 4 defined operator. */
else
cout << ch1 << " "<< ch3;
}
else //ch3 is the smallest
{
cout << ch3 << " ";
if(ch2 < ch1) cout << ch2 << " " << ch1 << endl;
else cout << ch1 << " " << ch2 << endl;
}
return 0;
}
/*Program chapter3_33 */
/*This program prompts the user to enter a character */
/*and determines if the input character is a vowel(a,e,i,o,u) */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
char letter;
cout << "Enter letter ";
cin >> letter;
cout << "You entered " << letter << endl;
switch(letter)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
cout << letter << " is a vowel. " << endl;
break;
default:
cout << letter << " is not a vowel. " << endl;
break;
}
return 0;
}
/*Program chapter3_34 */
/*This program prompts the user to enter two numbers */
/*and a character that represents an arithmetic operator. */
/* * => multiply */
/* + => addition */
/* - => subtraction */
/* / => division */
/* The input values are printed followed by */
/* the result of the operation or an error message */
/* if the character is not one of the 4 defined operator. */
Loading page 26...
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2;
char op;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
cout << "Enter desired operation:(*,+,-,/) ";
cin >> op;
cout << "You entered " << op << endl;
switch(op)
{
case '*':
cout << n1 << " * " << n2 << " = " << n1*n2 << endl;
break;
case '+':
cout << n1 << " + " << n2 << " = " << n1+n2 << endl;
break;
case '-':
cout << n1 << " - " << n2 << " = " << n1-n2 << endl;
break;
case '/':
cout << n1 << " / " << n2 << " = " << n1/n2 << endl;
break;
default:
cout << "invalid operator.." << endl;
break;
}
return 0;
}
/* Program chapter3_35 */
/* This program prompts the user to enter a month, */
/* represented as an integer between 1 and 12 */
/* The input value is printed followed by */
/* the name of the corresponding month. */
/* 1=>January, 2=>February, etc. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int month;
cout << "Enter the month: (As integer between 1 and 12)";
cin >> month;
cout << "You entered " << month << endl;
switch(month)
{
case 1:
cout << "The month is January" << endl;
using namespace std;
int main()
{
double n1,n2;
char op;
cout << "Enter first number ";
cin >> n1;
cout << "You entered " << n1 << endl;
cout << "Enter second number ";
cin >> n2;
cout << "You entered " << n2 << endl;
cout << "Enter desired operation:(*,+,-,/) ";
cin >> op;
cout << "You entered " << op << endl;
switch(op)
{
case '*':
cout << n1 << " * " << n2 << " = " << n1*n2 << endl;
break;
case '+':
cout << n1 << " + " << n2 << " = " << n1+n2 << endl;
break;
case '-':
cout << n1 << " - " << n2 << " = " << n1-n2 << endl;
break;
case '/':
cout << n1 << " / " << n2 << " = " << n1/n2 << endl;
break;
default:
cout << "invalid operator.." << endl;
break;
}
return 0;
}
/* Program chapter3_35 */
/* This program prompts the user to enter a month, */
/* represented as an integer between 1 and 12 */
/* The input value is printed followed by */
/* the name of the corresponding month. */
/* 1=>January, 2=>February, etc. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int month;
cout << "Enter the month: (As integer between 1 and 12)";
cin >> month;
cout << "You entered " << month << endl;
switch(month)
{
case 1:
cout << "The month is January" << endl;
Loading page 27...
break;
case 2:
cout << "The month is February" << endl;
break;
case 3:
cout << "The month is March" << endl;
break;
case 4:
cout << "The month is April" << endl;
break;
case 5:
cout << "The month is May" << endl;
break;
case 6:
cout << "The month is June" << endl;
break;
case 7:
cout << "The month is July" << endl;
break;
case 8:
cout << "The month is August" << endl;
break;
case 9:
cout << "The month is September" << endl;
break;
case 10:
cout << "The month is October" << endl;
break;
case 11:
cout << "The month is November" << endl;
break;
case 12:
cout << "The month is December" << endl;
break;
default:
cout << "invalid integer input.." << endl;
break;
}
return 0;
}
/*Program chapter3_36 */
/* This program prompts the user to enter a day, */
/* represented as an integer between 1 and 7 */
/* The input value is printed followed by */
/* the name of the corresponding month. */
/* 1=>Sunday, 2=>Monday, etc. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int day;
cout << "Enter the day: (As integer between 1 and 7)";
cin >> day;
cout << "You entered " << day << endl;
switch(day)
case 2:
cout << "The month is February" << endl;
break;
case 3:
cout << "The month is March" << endl;
break;
case 4:
cout << "The month is April" << endl;
break;
case 5:
cout << "The month is May" << endl;
break;
case 6:
cout << "The month is June" << endl;
break;
case 7:
cout << "The month is July" << endl;
break;
case 8:
cout << "The month is August" << endl;
break;
case 9:
cout << "The month is September" << endl;
break;
case 10:
cout << "The month is October" << endl;
break;
case 11:
cout << "The month is November" << endl;
break;
case 12:
cout << "The month is December" << endl;
break;
default:
cout << "invalid integer input.." << endl;
break;
}
return 0;
}
/*Program chapter3_36 */
/* This program prompts the user to enter a day, */
/* represented as an integer between 1 and 7 */
/* The input value is printed followed by */
/* the name of the corresponding month. */
/* 1=>Sunday, 2=>Monday, etc. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int day;
cout << "Enter the day: (As integer between 1 and 7)";
cin >> day;
cout << "You entered " << day << endl;
switch(day)
Loading page 28...
{
case 1:
cout << "The day is Sunday" << endl;
break;
case 2:
cout << "The day is Monday" << endl;
break;
case 3:
cout << "The day is Tuesday" << endl;
break;
case 4:
cout << "The day is Wednesday" << endl;
break;
case 5:
cout << "The day is Thursday" << endl;
break;
case 6:
cout << "The day is Friday" << endl;
break;
case 7:
cout << "The day is Saturday" << endl;
break;
default:
cout << "invalid integer input.." << endl;
break;
}
return 0;
}
/*Program chapter3_37 */
/* This program prompts the user to enter a date with the format XX/XX, */
/* as in 10/01. The program will input the date and print the season. */
/* For example, if the input is 10/01 the output will be: */
/* Date entered is October 1. The season is Fall. */
/* If the input is invalid, an error message is printed. */
/* Assumptions: */
/* Seasons are based on Nothern Hemisphere time, year 2016. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int month(0), day(0);
string theMonth, theSeason;
char ch;
cout << "Enter the date: (with format xx/xx)";
cin >> month;
cin >> ch;
cin >> day;
if(!cin || day < 1)
{
cerr << "Bad input.." << endl;
exit(1);
}
switch(month)
{
case 1:
case 1:
cout << "The day is Sunday" << endl;
break;
case 2:
cout << "The day is Monday" << endl;
break;
case 3:
cout << "The day is Tuesday" << endl;
break;
case 4:
cout << "The day is Wednesday" << endl;
break;
case 5:
cout << "The day is Thursday" << endl;
break;
case 6:
cout << "The day is Friday" << endl;
break;
case 7:
cout << "The day is Saturday" << endl;
break;
default:
cout << "invalid integer input.." << endl;
break;
}
return 0;
}
/*Program chapter3_37 */
/* This program prompts the user to enter a date with the format XX/XX, */
/* as in 10/01. The program will input the date and print the season. */
/* For example, if the input is 10/01 the output will be: */
/* Date entered is October 1. The season is Fall. */
/* If the input is invalid, an error message is printed. */
/* Assumptions: */
/* Seasons are based on Nothern Hemisphere time, year 2016. */
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
int month(0), day(0);
string theMonth, theSeason;
char ch;
cout << "Enter the date: (with format xx/xx)";
cin >> month;
cin >> ch;
cin >> day;
if(!cin || day < 1)
{
cerr << "Bad input.." << endl;
exit(1);
}
switch(month)
{
case 1:
Loading page 29...
cout << "The month is January" << endl;
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theMonth = "January";
theSeason = "Winter";
break;
case 2:
cout << "The month is February" << endl;
if(day > 29)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theMonth = "February";
theSeason = "Winter";
break;
case 3:
cout << "The month is March" << endl;
theMonth = "March";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
if(day < 20) theSeason = "Winter";
else theSeason = "Spring";
break;
case 4:
cout << "The month is April" << endl;
theMonth = "April";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Spring";
break;
case 5:
cout << "The month is May" << endl;
theMonth = "May";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Spring";
break;
case 6:
cout << "The month is June" << endl;
theMonth = "June";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theMonth = "January";
theSeason = "Winter";
break;
case 2:
cout << "The month is February" << endl;
if(day > 29)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theMonth = "February";
theSeason = "Winter";
break;
case 3:
cout << "The month is March" << endl;
theMonth = "March";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
if(day < 20) theSeason = "Winter";
else theSeason = "Spring";
break;
case 4:
cout << "The month is April" << endl;
theMonth = "April";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Spring";
break;
case 5:
cout << "The month is May" << endl;
theMonth = "May";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Spring";
break;
case 6:
cout << "The month is June" << endl;
theMonth = "June";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
Loading page 30...
}
if(day < 20) theSeason = "Spring";
else theSeason = "Summer";
break;
case 7:
cout << "The month is July" << endl;
theMonth = "Juy";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Summer";
break;
case 8:
cout << "The month is August" << endl;
theMonth = "August";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Summer";
break;
case 9:
cout << "The month is September" << endl;
theMonth = "September";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
if(day < 22) theSeason = "Summer";
else theSeason = "Fall";
break;
case 10:
cout << "The month is October" << endl;
theMonth = "October";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Fall";
break;
case 11:
cout << "The month is November" << endl;
theMonth = "November";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Fall";
break;
case 12:
cout << "The month is December" << endl;
if(day < 20) theSeason = "Spring";
else theSeason = "Summer";
break;
case 7:
cout << "The month is July" << endl;
theMonth = "Juy";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Summer";
break;
case 8:
cout << "The month is August" << endl;
theMonth = "August";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Summer";
break;
case 9:
cout << "The month is September" << endl;
theMonth = "September";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
if(day < 22) theSeason = "Summer";
else theSeason = "Fall";
break;
case 10:
cout << "The month is October" << endl;
theMonth = "October";
if(day > 31)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Fall";
break;
case 11:
cout << "The month is November" << endl;
theMonth = "November";
if(day > 30)
{
cerr << "bad date entered.." << day << endl;
exit(1);
}
theSeason = "Fall";
break;
case 12:
cout << "The month is December" << endl;
Loading page 31...
28 more pages available. Scroll down to load them.
Preview Mode
Sign in to access the full document!
100%
Study Now!
XY-Copilot AI
Unlimited Access
Secure Payment
Instant Access
24/7 Support
AI Assistant
Document Details
Subject
Information Technology