CSC 212 Problems For Chapter 3, Spring 2010

Notre Dame University–Louaize · Program Design and Data Abstraction I · 10 pages

All CSC 212 files
CSC 212 Problems For Chapter 3, Spring 2010, page 1

Stuck on a question? A tutor can walk you through it.

CSC 212 Problems For Chapter 3, Spring 2010, page 2 CSC 212 Problems For Chapter 3, Spring 2010, page 3 CSC 212 Problems For Chapter 3, Spring 2010, page 4 CSC 212 Problems For Chapter 3, Spring 2010, page 5 CSC 212 Problems For Chapter 3, Spring 2010, page 6 CSC 212 Problems For Chapter 3, Spring 2010, page 7 CSC 212 Problems For Chapter 3, Spring 2010, page 8 CSC 212 Problems For Chapter 3, Spring 2010, page 9 CSC 212 Problems For Chapter 3, Spring 2010, page 10

More CSC 212 files

Show the text of this chapter
Problem Exercises Chapter 03 1 Problem Exercises 1. Page 232: Exercise 28. Write a function that returns the smallest of three double-precision, floating-points numbers. #include <iostream.h> double smallest(double, double, double); int main() { double x, y, z; cout << "Enter three numbers: "; cin >> x >> y >> z; cout << "The smallest value is: " << smallest(x, y, z) << endl << endl; return 0; } double smallest(double smallest, double b, double c) { if (b < smallest) smallest = b; if (c < smallest) smallest = c; return smallest; } Sample Output: Problem Exercises Chapter 03 2 Problem Exercises Chapter 03 3 2. Page 232: Exercise 29. An integer is said to be a perfect number if the sum of its factors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function that determines whether a number is perfect. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. #include <iostream.h> bool perfect(int); int main() { cout << "For the integers from 1 to 1000: \n"; for (int j = 2; j <= 1000; ++j) if ( perfect(j) ) cout << j << " is perfect \n"; cout << endl; return 0; } bool perfect(int value) { int factorSum = 1; for (int i = 2; i <= value / 2; ++i) if (value % i == 0) factorSum += i; return factorSum == value ? true : false; } Sample Output: Problem Exercises Chapter 03 4 Problem Exercises Chapter 03 5 3. Page 232: Exercise 30. An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a) Write a function to determine if a number is prime. b) Use this function in a program that determines and prints all the prime numbers between 1 and 1000. #include <iostream.h> #include <iomanip.h> bool prime(int); int main() { int count = 0; cout << "The prime numbers from 1 to 1000 are: \n"; for (int loop = 2; loop <= 1000; ++loop) { if ( prime(loop) ) { ++count; cout << setw(6) << loop; if ( (count % 10) == 0 ) cout << '\n'; } } cout << endl << endl; return 0; } bool prime(int n) { for (int loop2 = 2; loop2 <= n/2; loop2++) { if ( (n % loop2) == 0 ) return false; } return true; Problem Exercises Chapter 03 6 } Sample Output: Problem Exercises Chapter 03 7 4. Page 232: Exercise 32. The Greatest Common Divider (GCD) of two integers is the largest integer that evenly divides each of the number. Write a function that returns the Greatest Common Divider of two integers. #include <iostream.h> int gcd(int, int); int main() { int a, b; for (int j = 1; j <= 3; ++j) { cout << "Enter two integers: "; cin >> a >> b; cout << "The Greatest Common Divisor of " << a << " and " << b << " is " << gcd(a, b) << "\n\n"; } return 0; } int gcd(int x, int y) { int greatest = 1; for (int i = 2; i <= ((x < y) ? x: y); ++i) { if ( (x % i == 0) && (y % i == 0) ) greatest = i; } return greatest; } Sample Output: Problem Exercises Chapter 03 8 Problem Exercises Chapter 03 9 5. Page 232: Exercise 34. Write a program that simulates coin tossing. For each toss of the coin, the program should print Heads or Tails. Let the program toss the coin 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. #include <iostream.h> #include <stdlib.h> #include <time.h> int flip(void); int main() { int headCount = 0, tailCount = 0; srand(time(0)); for (int loop = 1; loop <= 100; loop++) { if ( flip() == 0 ) { tailCount++; cout << "Tails "; } else { headCount++; cout << "Heads "; } if ( (loop % 10) == 0 ) cout << '\n'; } cout << "\n Number of Heads: " << headCount << "\n Number of Tails: " << tailCount << "\n\n"; return 0; } int flip(void) { return rand() % 2; Problem Exercises Chapter 03 10 } Sample Output: