CramX Logo
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Document preview page 1

Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 1

Document preview content for Solution Manual for Engineering Problem Solving With C++, 4th Edition

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.3
80
over 1 year ago
Preview (31 of 212 Pages)
100%
Log in to unlock
Page 1 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 1 preview imageExam Practice!1.F2.T3.F4.TMultiple Choice Problems5.(d)6.(b)7.(c)8.(a)9.(a)Base ConversionsBinary to:10.Octala.665b.75157c.1271d.1001e.777Binary to:11.Hexadecimala.86Bb.E7F5c.2B9d.8001e.FFFDecimal to:12.Octala.2414b.1137c.22075d.3641112e.373570713.Decimala.213b.2546c.237810d.528e.279935
Page 2 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 2 preview image
Page 3 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 3 preview imageExam Practice!1.T2.F3.T4.T5.F6.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 Snapshots16.x1=>2, z=>2, x=>217.x=>2, y=>1, a=>3.8, n=>2Output18.value_1 = 5.7826319.Missing ; (value_4 = 6.645832e+01)20.value_5 = 7750Programming 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 convertsmeters to miles.*/
Page 4 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 4 preview image#include <iostream>using namespace std;int main(){/*Declare variables.*/double miles,meters;/*Enter number ofmeters from the keyboard.*/cout << "Enter the number ofmeters:\n";cin >>meters;/*Compute the number of miles equal to the specifiedmeters. */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>
Page 5 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 5 preview imageusing 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 << "degreesRankin\n";/*Exit program.*/return 0;}/*--------------------------------------------------------------------*//*--------------------------------------------------------------------*//*Problem chapter2_26*//**//*This program converts degrees Celsius to degrees Rankin.*/#include <iostream>
Page 6 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 6 preview imageusing 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 << " degreesRankin\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 << " degreesFahrenheit\n";/*Exit program.*/return 0;}/*--------------------------------------------------------------------*//*--------------------------------------------------------------------*/
Page 7 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 7 preview image/*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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 8 preview image/*--------------------------------------------------------------------*//*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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 9 preview image/*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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 10 preview imagedouble 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 11 preview image#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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 12 preview image/*--------------------------------------------------------------------*//*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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 13 preview imageNITROGEN + (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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 14 preview image/*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 atomsfor*//*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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 15 preview image}/*--------------------------------------------------------------------*//*--------------------------------------------------------------------*//*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.*/doublex, 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.*/doublex, 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 16 preview image/*Print the answer.*/cout << "The logarithm of " << x << " to the base 8 is " << answer<< endl;/*Exit program.*/return 0;}/*--------------------------------------------------------------------*/
Page 17 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 17 preview imageExam Practice!1.T2.F3.T4.F5.TSyntax Problems6.switch condition (sqrt(x)) is not an integer.Multiple Choice7.(c)8.(b)9.(a)10.(a)11.(b)12.(b)13.(b)Memory Snapshot14.n => 0, count => 215.n =>-1, count => 116.mon => 1, ch => J17.mon => 1, ch => F18.n => 0, count =>-119.a => 150020.ch => *Programming ExercisesBoolean Expressions/*--------------------------------------------------------------------*//*Program chapter3_21This program receives three boolean values from standard inputthendetermines if the condition ! (a&&b&&c) && !(a||b||c) is true or false.*/#include<iostream>using namespace std;int main(){//Declare local objectsbool 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 conditioncout << "The condition:\n" << endl;cout << "! (" << a << "&&" << b << "&&" << c <<" ) && !("<< a << "||" << b << "||" << c << ")\n\n";//Determine if condition is true of falseif(! (a&&b&&c) && !(a||b||c) ){cout << "is true." << endl;}else{cout << "is false." << endl;}return 0;
Page 18 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 18 preview image}/*--------------------------------------------------------------------*//*--------------------------------------------------------------------*//*Program chapter3_22This program receives three boolean values from standard inputthendetermines if the condition !(xx||xx)&&xxis true or false.*/#include<iostream>using namespace std;int main(){//Declare local objectsbool 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 conditioncout << "The condition:\n" << endl;cout << "! (" << a << "||" << b << ")&&" << c << "\n\n";//Determine if condition is true of falseif( !(a||b)&&c ){cout << "is true." << endl;}else{cout << "is false." << endl;}}return 0;}/*--------------------------------------------------------------------*//*-------------------------------------------------------------------*//*Program chapter3_23generates 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 objectsbool A(false), B(false), C(false);bool tautology(true);//assume true
Page 19 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 19 preview imagebool condition = (A && B || B && C);//Print table headercout << "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 CC = !C;condition = (A && B || B && C);cout << A << '\t' << B << '\t' << C <<"\t\t\t" << condition << endl;if(!condition) tautology = false;//Toggle B and CB = !B;C = !C;condition = (A && B || B && C);if(!condition) tautology = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;//Toggle CC = !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 CA = !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 CC = !C;condition = (A && B || B && C);if(!condition) tautology = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;//Toggle B and CB = !B;C = !C;condition = (A && B || B && C);if(!condition) tautology = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;//Toggle CC = !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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 20 preview image/*-------------------------------------------------------------------*//*Program chapter3_24generates 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 objectsbool A(false), B(false), C(false);bool contradiction(true);//assume truebool condition = (A && B || B && C);//Print table headercout << "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 CC = !C;condition = (A && B || B && C);cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;if(condition) contradiction = false;//Toggle B and CB = !B;C = !C;condition = (A && B || B && C);if(condition) contradiction = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" <<condition << endl;//Toggle CC = !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 CA = !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 CC = !C;
Page 21 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 21 preview imagecondition = (A && B || B && C);if(condition) contradiction = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;//Toggle B and CB = !B;C = !C;condition = (A && B || B && C);if(condition) contradiction = false;cout << A << '\t' << B << '\t' << C << "\t\t\t" << condition << endl;//Toggle CC = !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_25generates 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 objectsbool A(false), B(false), C(false);bool condition = (A && B || B && C);bool first(condition), contingency(false);//set to first value//Print table headercout << "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 CC = !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 CB = !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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 22 preview imageC = !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 CA = !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 CC = !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 CB = !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 CC = !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. ifStatements/*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, coutusing namespace std;int main(){double n1;cout << "Enter a value ";cin >> n1;cout << "You entered " << n1 << endl;if(n1 > 0)
Page 23 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 23 preview imagecout << 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, coutusing 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, coutusing 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 24 preview image}/*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, coutusing 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;elsecout << 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, coutusing 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;elsecout << n1 << " is evenly divisible by " << n2 << endl;return 0;
Page 25 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 25 preview image}/*Program chapter3_31*//* This programprompts the user to enter twocharacters*//* The input characters are printed in alphabetical order.*/#include<iostream>//required for cin, coutusing 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;elsecout << 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, coutusing 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;elsecout << ch3 << " "<< ch2;}else if(ch2 <= ch1 && ch2 <= ch3) //ch2 is the smallest{cout << ch2 << " ";if(ch3 < ch1)
Page 26 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 26 preview imagecout << ch3 << " "<< ch1;elsecout << 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, coutusing 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 27 preview image#include<iostream>//required for cin, coutusing 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, coutusing 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 28 preview imagebreak;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, coutusing 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 29 preview image{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 theseason.*//* 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, coutusing 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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 30 preview imagecout << "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 of 31
Solution Manual for Engineering Problem Solving With C++, 4th Edition - Page 31 preview image}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;
Preview Mode

This document has 212 pages. Sign in to access the full document!