130 likes | 231 Views
Chapter Four Exercises. Chapter 4. 1. a. false; b. false; c. false; d. true; e. false; f. false; g. false; h. false; i. false; j. true. Chapter 4. 2. a. i b. i c. ii d. i e. iii 3. 100 200 0 4. if (gender == 'M') cout << "Male" << endl;
E N D
Chapter 4 • 1. a. false; • b. false; • c. false; • d. true; • e. false; • f. false; • g. false; • h. false; • i. false; • j. true
Chapter 4 • 2. a. i • b. i • c. ii • d. i • e. iii • 3. 100 200 0 • 4. • if (gender == 'M') • cout << "Male" << endl; • else if (gender == 'F') • cout << "Female" << endl; • else • cout << "Invalid gender code." << endl;
Chapter 4 • 5. Omit the semicolon after else. The correct statement is: • if (score >= 60) • cout << "You pass." << endl; • else • cout << "You fail." << endl; • 6. a, c, and d are valid. b is invalid; a case value cannot appear more than once and there should be a colon after a case value. • 7. 15 • 8. 7 • 9. 96
Chapter 4 • 10. • #include <iostream> • using namespace std; • int main () • { • int a, b, c found; • cout << "Enter two integers: "; • cin >> a >> b; • if (a > a * b && 10 < b) • found = 2 * a > b; • else • { • found = 2 * a < b; • if (found) • a = 3; • c = 15; • if (b) • { • b = 0; • a = 1; • } • } • return 0; • }
Chapter 4 • 11. • #include <iostream> • using namespace std; • const int one = 5; • int main() • { • int x, y, w, z; • z = 9; • if (z > 10) • { • x = 12; • y = 5; • w = x + y + one; • } • else • { • x = 12; • y = 4; • w = x + y + one; • } • cout << "w = " << w << endl; • return 0; • }
Chapter 4 • //Chapter 4 Programming Exercise 1 • #include <iostream> • using namespace std; • int main() • { • double num; • cout << "Enter a number: "; • cin >> num; • cout << endl; • cout << "The number you entered is " << num • << ", and this is a "; • if (num == 0) • cout << "zero." << endl; • else if (num > 0) • cout << "positive number." << endl; • else • cout << "negative number." << endl; • return 0; • }
Chapter 4 • //Chapter 4 Programming Exercise 2 • #include <iostream> • using namespace std; • int main() • { • double num1, num2, num3; • double temp; • cout << "Enter three numbers: "; • cin >> num1 >> num2 >> num3; • cout << endl; • if (num1 > num2) • { • temp = num1; • num1 = num2; • num2 = temp; • } • //Now num1 is less than or equal to num2 • cout << "The numbers in the ascending order are: "; • if (num3 <= num1) • cout << num3 << " " << num1 << " " << num2 << endl; • else if (num1 <= num3 && num3 <= num2) • cout << num1 << " " << num3 << " " << num2 << endl; • else • cout << num1 << " " << num2 << " " << num3 << endl; • return 0; • }
Chapter 4 • //Chapter 4 Programming Exercise 5 • #include <iostream> • #include <iomanip> • using namespace std; • int main() • { • double side1, side2, side3; • cout << "Enter the lengths of the sides a triangle: "; • cin >> side1 >> side2 >> side3; • cout << endl; • if ((side1 * side1 == (side2 * side2 + side3 * side3)) || • (side2 * side2 == (side1 * side1 + side3 * side3)) || • (side3 * side3 == (side1 * side1 + side2 * side2))) • cout << "It is a right angled triangle" << endl; • else • cout << "It is not a right angled triangle" << endl; • return 0; • }
Chapter 4 • //Chapter 4: Programming Exercise 6 • #include <iostream> • #include <iomanip> • using namespace std; • const int NUMBER_OF_COOKIES_IN_BOX = 24; • const int CONTAINER_CAPACITY = 75; • int main() • { • int numberOfCookies; • int numberOfBoxes; • int numberOfContainers; • int leftoverCookies; • int leftoverBoxes; • cout << "Enter the total number of cookies: "; • cin >> numberOfCookies; • cout << endl;
Chapter 4 • numberOfBoxes = numberOfCookies / NUMBER_OF_COOKIES_IN_BOX; • leftoverCookies = numberOfCookies % NUMBER_OF_COOKIES_IN_BOX; • numberOfContainers = numberOfBoxes / CONTAINER_CAPACITY; • leftoverBoxes = numberOfBoxes % CONTAINER_CAPACITY; • cout << "The number of cookie boxes needed to hold the cookies: " • << numberOfBoxes << endl; • if (leftoverCookies > 0) • cout << "Leftover cookies: " << leftoverCookies << endl; • cout << "The number of containers needed to store the cookie boxes: " • << numberOfContainers << endl; • if (leftoverBoxes > 0) • cout << "Leftover boxes: " << leftoverBoxes << endl; • return 0; • }
Chapter 4 • //Chapter 4: Programming Exercise 9 • #include <iostream> • #include <iomanip> • using namespace std; • int main() • { • int num1, num2; • char opr; • cout << "Enter two integers: "; • cin >> num1 >> num2; • cout << endl; • cout << "Enter operator: + (addition), - (subtraction)," • << " * (multiplication), / (division): "; • cin >> opr; • cout << endl; • cout << num1 << " " << opr << " " << num2 << " = ";
Chapter 4 • switch (opr) • { • case '+': • cout << num1 + num2 << endl; • break; • case '-': • cout << num1 - num2 << endl; • break; • case '*': • cout << num1 * num2 << endl; • break; • case '/': • if (num2 != 0) • cout << num1 / num2 << endl; • else • cout << "ERROR \nCannot divide by zero" << endl; • break; • default: • cout << "Illegal operation" << endl; • } • return 0; • }