Exam Practice!
1. T
Syntax Problems
Multiple Choice
7. (c)
Memory Snapshot
15. n => -1, count => 1
17. mon => 1, ch => F
19. a => 1500
20. ch => *
Programming Exercises
Boolean Expressions
/*——————————————————————–*/
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
//Determine if condition is true of false
if(! (a&&b&&c) && !(a||b||c) )
{
}
/*——————————————————————–*/
*/
#include<iostream>
using namespace std;
int main()
{
//Declare local objects
bool a, b, c;
//Determine if condition is true of false
if( !(a||b)&&c )
}
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
//Toggle C
C = !C;
condition = (A && B || B && C);
if(!condition) tautology = false;
cout << A << ‘\t’ << B << ‘\t’ << C << “\t\t\t” << condition << endl;
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;
else cout << “This condtion is not a tautology.” << endl;
return 0;
}
/*——————————————————————-*/
int main()
{
//Declare and intialize objects
bool A(false), B(false), C(false);
bool contradiction(true); //assume true
bool condition = (A && B || B && C);
//Toggle C
C = !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
if(condition) contradiction = false;
cout << A << ‘\t’ << B << ‘\t’ << C << “\t\t\t” << condition << endl;
//Toggle B and C
B = !B;
C = !C;
return 0;
}
/*——————————————————————–*/
/*——————————————————————-*/
/* Program chapter3_25 generates a truth table for the condition: */
/* A && B || B && C */
cout << “_________________________________________________” << endl;
cout << A << ‘\t’ << B << ‘\t’ << C << “\t\t\t” << condition << endl;
//Toggle C
C = !C;
condition = (A && B || B && 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;
//Toggle B and C
B = !B;
C = !C;
condition = (A && B || B && C);
/*——————————————————————–*/
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. */
else if(n1 < 0)
cout << n1 << ” is less than zero.” << endl;
return 0;
}
/*Program chapter3_27 */
int main()
{
double n1,n2,diff;
}
/*Program chapter3_28 */
int main()
{
double n1,n2;
cout << “Enter first number “;
cin >> n1;
cout << “You entered ” << n1 << endl;
#include<iostream> //required for cin, cout
using namespace std;
int main()
{
double n1,n2,n3;
cout << “Enter first number “;
}
else
{
cout << n3 << ” is the largest value entered ” << endl;
}
cout << “Enter first integer “;
cin >> n1;
cout << “You entered ” << n1 << endl;
cout << “Enter second integer “;
cin >> n2;
int main()
{
char ch1,ch2;
cout << “Enter first character “;
cin >> ch1;
}
/*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 << ch1 << ” “;
if(ch2 < ch3)
{
cout << ch3 << ” “;
if(ch2 < ch1) cout << ch2 << ” ” << ch1 << endl;
else cout << ch1 << ” ” << ch2 << endl;
}
return 0;
}
/*Program chapter3_33 */
{
case ‘A’:
case ‘a’:
case ‘E’:
}
return 0;
}
/*Program chapter3_34 */
/*This program prompts the user to enter two numbers */
/*and a character that represents an arithmetic operator. */
/* * => multiply */
char op;
cout << “Enter first number “;
cin >> n1;
cout << “You entered ” << n1 << endl;
cout << “Enter second number “;
cout << n1 << ” + ” << n2 << ” = ” << n1+n2 << endl;
break;
case ‘-‘:
cout << n1 << ” ” << n2 << ” = ” << n1-n2 << endl;
break;
return 0;
}
/* Program chapter3_35 */
/* This program prompts the user to enter a month, */
#include<iostream> //required for cin, cout
using namespace std;
{
case 1: