CSC 212 Problems For Chapter 2, Spring 2006

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

All CSC 212 files
CSC 212 Problems For Chapter 2, Spring 2006, page 1

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

CSC 212 Problems For Chapter 2, Spring 2006, page 2

More CSC 212 files

Show the text of this chapter
2.44 Write a program that calculates and prints the product of the odd integers from 1 to 15. // Exercise 2.44 solution 1 2 #4nclude <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 dint main() 8 { 9 long product = 1; 10 1] for ( long i = 3; i <= 15; 1 += 2 ) 12 product *= i; 13 14 cout << "Product of the odd integers from 1 to 15 is: " 15 << product << endl; 16 17 return 0; 18 } Product of the odd integers from 1 to 15 is: 2027025 2.45 — The factorial function is used frequently in probability problems. The factorial of a positive integer n (written n/ and pro- nounced “‘n factorial”) is equal to the product of the positive integers from | to n. Write a program that evaluates the factorials of the integers from 1 to 5. Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20? // Exercise 2.45 solution #include <iostream> 1 2 3 4 using std::cout; 5 using std::endl; 6 7 8 9 int main() { int factorial; 10 11 cout << "X\tFactorial of X\n"; 12 13 for ( int i = 1; 1 <= 5; +4+1 ) { 14 factorial = 1; 15 16 for ( int j = 1; j <= i; +45 ) 17 factorial *= J; 18 19 cout << i << '\t' << factorial << '\n'; 20 } 21 22 cout << endl; 23 return 0; 24 } x Factorial of x 1 1 2 2 3 6 4 24 5 120 2.48 — One interesting application of computers is drawing graphs and bar charts (sometimes called “*histograms”). Write a pro- gram that reads five numbers (each between | and 30). For each number read, your program should print a line containing that num- ber of adjacent asterisks. For example, if your program reads the number seven, it should print **** ***. // Exercise 2.48 solution #include <iostream> using std::cout; using std::endl; using std::cin; On Rwnr — 7 8 int main() 9 { i int number; 12 cout << "Enter 5 numbers between 1 and 30: "; 13 14 for ( int i = 1; i <= 5; +41) { 15 cin >> number; 16 17 for ( int j = 1; j <= number; ++j ) 18 cout << '*'; 19 20 cout << '\n'; 21 } 22 23 cout << endl; 24 25 return 0; 26 + Enter 5 numbers between 1 and 30: 16 12 8 27 9 peererrrrttt ttt? Prrerrrrtrr sy orerrere Pretttttitetetttittitireresy FRR R ERE