c++ proposed exercises (chapter 9: the c++ programing language, fourth edition)

2
Proposed Exercises Chapter 9 In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition). Solution is provided. However, if you follow the book carefully, you will be able to solve the proposed problems easily. Introduction material is very dense and that’s why no exercise material is elaborated. Don’t get panic If you have difficulties trying to solve these exercises. The material provided in the Introduction section (Part I) and chapter nine, allow you to PROPOSE some solution. Some recommendations are: 1. Try to mimic the Programming Technique found in the chapter. 2. Make the program work. 3. First understand the problem. Paper and pencil examples comes before programming. 4. Test your code in main(). 5. Smile, you are becoming part of the C++ community. A. If Statement In the next six exercise, function characteristics are: Parameter input: Integer Parameter output: bool 1. Identify two different forms of implementing if else statement. Hint: Check max function in page 229. 2. Create a function that evaluate if a number is less than 5. 3. Create a function that evaluate if a number is in the following range: [5,10]. 4. Create a function that evaluate if a number is in the following range: (5,10]. 5. Create a function that evaluate if a number is in the following range: (5,10). 6. Create a function that evaluate if a number is greater than 5. 7. Create a function that evaluate if a number is in the following ranges: (2,10),[20,28]. 8. Create a function that evaluate if a number: Is in the range (2,10) and is even or, is in the range [20,28] and is odd. 9. (*)Imagine that we need to find the maximum and minimum value in a container(list, array, vector, etc). In page 229 we have a max function that evaluate two input parameter, however in a container we can have more than two data. How will you solve this issue ?

Upload: mauricio-bedoya

Post on 10-Sep-2015

14 views

Category:

Documents


2 download

DESCRIPTION

.

TRANSCRIPT

  • Proposed Exercises Chapter 9

    In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition).Solution is provided. However, if you follow the book carefully, you will be able to solve the proposed problemseasily.

    Introduction material is very dense and thats why no exercise material is elaborated.Dont get panic If you have difficulties trying to solve these exercises. The material provided in the Introduction

    section (Part I) and chapter nine, allow you to PROPOSE some solution. Some recommendations are:

    1. Try to mimic the Programming Technique found in the chapter.

    2. Make the program work.

    3. First understand the problem. Paper and pencil examples comes before programming.

    4. Test your code in main().

    5. Smile, you are becoming part of the C++ community.

    A. If Statement

    In the next six exercise, function characteristics are:

    Parameter input: Integer Parameter output: bool

    1. Identify two different forms of implementing if else statement. Hint: Check max function in page 229.

    2. Create a function that evaluate if a number is less than 5.

    3. Create a function that evaluate if a number is in the following range: [5,10].

    4. Create a function that evaluate if a number is in the following range: (5,10].

    5. Create a function that evaluate if a number is in the following range: (5,10).

    6. Create a function that evaluate if a number is greater than 5.

    7. Create a function that evaluate if a number is in the following ranges: (2,10),[20,28].

    8. Create a function that evaluate if a number:

    Is in the range (2,10) and is even or, is in the range [20,28] and is odd.

    9. (*)Imagine that we need to find the maximum and minimum value in a container(list, array, vector, etc). Inpage 229 we have a max function that evaluate two input parameter, however in a container we can havemore than two data. How will you solve this issue ?

  • B. Switch Statement

    1. Imagine that you can choose between a Ferrari, Corvette, and Maserati car. Car colors are: black, red,white. Create enum Car and enum Color. Next implement switch statement that allow you to choosebetween the different alternatives. Each alternative should prompt a message indicating the car and thecolor selected.

    2. Repeat the previous example but initialize each car and color value with non default values. Example: enumCar{Ferrari=10, Corvette=12, Maserati=20}.

    3. Compare the previous two implementations. Now imagine that two other cars and five more colors areavailable. Which code require less care (effort) in maintenance and future development.

    4. Try to add a new car alternative at the beginning of enum car. Compile and see what happen.

    5. Repeat exercise 1 but now use enum class Car and enum class Color

    C. For Statement

    1. (*) Create a function called Accum_Sum with the following characteristics:

    Input parameter: int. Output parameter: int. Description: user provides an integer value A and the function should make the accumulated sum from

    1 to A. Example: Accum_Sum(2) = 3.

    2. (*) Create a function called Fibonacci with the following characteristics:

    Input parameter: int. Output parameter: int. Description: user provides an integer value n and the function should generate the n Fibonacci num-

    bers and save them in a vector. Out put is the Accum_Sum of the elements in the vector.

    3. (*) Lets modify a little bit the previous example. Imagine that we call Fibonacci(10). Next, call Fibonacci(5)and Fibonacci(15). Cause 5 < 10 the numbers where already generate in Fibonacci(10). In Fibonacci(15),weneed to generate only 5 additional data to get the Accum_Sum. This mean that we need to make a test inthe input parameter and avoid generating unnecessary calculations. Try to implement this new feature ofFibonacci function (Hint: use static).

    4. (*) Implement a function that test if number(string) of n digits is a palindrome. Example: 1234321 is apalindrome. 1234521 is not a palindrome.

    5. (*) Identify that the solution of the previous item is a function of the number of digits that the palindrome has.Consider this and reimplement the code.

    In this page: https://projecteuler.net/ you can find mathematical problems that can be implemented in C++.Dont get panic and go for it.