Solution Manual for Engineering Problem Solving With C++, 4th Edition
- Information Technology
- 212 pages
- Shared February 2025
- 80 downloads
Strengthen your problem-solving skills with Solution Manual for Engineering Problem Solving With C++, 4th Edition, your essential study tool.
Preview 31 of 212 pages
See the first 31 pages exactly as they appear in the document.
Page 1 Read page 1 as text
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
Page 2 Page 3 Read page 3 as text
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. */
Page 4 Read page 4 as text
#include <iostream> using namespace std; int main() { /* Declare variables. */ double miles, meters; /* Enter numbe r of meters from the keyboard. */ c out << "Enter the number of meters: \ n"; cin >> meters; /* Compute the number of m iles equal to the specified meters. */ miles = meters/ 1 609 . 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>
Page 5 Read page 5 as text
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>
Page 6 Read page 6 as text
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; } /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
Page 7 Read page 7 as text
/* 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; } /* -------------------------------------------------------------------- */
Page 8 Read page 8 as text
/* -------------------------------------------------------------------- */ /* 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;
Page 9 Read page 9 as text
/* 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. */
Page 10 Read page 10 as text
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>
Page 11 Read page 11 as text
#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; } /* -------------------------------------------------------------------- */
Page 12 Read page 12 as text
/* -------------------------------------------------------------------- */ /* 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) +
Page 13 Read page 13 as text
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);
Page 14 Read page 14 as text
/* 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;
Page 15 Read page 15 as text
} /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /* 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);
Page 16 Read page 16 as text
/* Print the answer. */ cout << "The logarithm of " << x << " to the base 8 is " << answer << endl; /* Exit program. */ return 0; } /* -------------------------------------------------------------------- */
Page 17 Read page 17 as text
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 => 15 00 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;
Page 18 Read page 18 as text
} /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /* 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
Page 19 Read page 19 as text
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; //R epeat 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; }
Page 20 Read page 20 as text
/* ------------------------------------------------------------------- */ /* Program chapter3_24 generates a truth table for the condition: */ /* A && B || B && C */ /* and determines if the cond i tion 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;
Page 21 Read page 21 as text
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 cond i tion 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
Page 22 Read page 22 as text
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)
Page 23 Read page 23 as text
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;
Page 24 Read page 24 as text
} /*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;
Page 25 Read page 25 as text
} /*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 p rompts 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)
Page 26 Read page 26 as text
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. */
Page 27 Read page 27 as text
#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;
Page 28 Read page 28 as text
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)
Page 29 Read page 29 as text
{ 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:
Page 30 Read page 30 as text
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);
Page 31 Read page 31 as text
} 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;
181 more pages in the full document
Unlock it once and it stays in your library, ready to chat with or turn into flashcards.
Unlock the full documentMore Information Technology study materials
See all
Exam GuidesTest Bank for Managing and Using Information Systems: A Strategic Approach, 7th Edition (Chapters 1-13)
Information Technology
452 pages149downloadsFeb 2025
Exam GuidesTest Bank for Essentials of MIS, 12th Edition
Information Technology
342 pages146downloadsFeb 2025
Exam GuidesA+ Guide to Hardware, 9th Edition Test Bank
Information Technology
116 pages142downloadsFeb 2025
Textbook GuidesSolution Manual for Essentials of System Analysis and Design, 4th Edition
Information Technology
282 pages140downloadsFeb 2025
Textbook GuidesGuide To Networking Essentials, 7th Edition Solution Manual
Information Technology
48 pages139downloadsFeb 2025
Past Exams2023-2024 SOPHOS FIREWALL V19.5 Practice Exam with Answers (75 Solved Questions)
Information Technology
7 pages134downloadsFeb 2025
Class NotesLecture Notes for CCNP Enterprise: Core Networking (ENCOR) v8 Lab Manual, 2nd Edition
Information Technology
1089 pages123downloadsFeb 2025
Class NotesLecture Notes for Adobe XD Classroom in a Book (2020 release)
Information Technology
66 pages118downloadsFeb 2025
Study this document with CramX
Once it is in your library, every tool can work from it.

