C++ Programming Basics

 

Chapter 2

C++ Programming Basics


EXERCISES:

Answers to these questions can be found in Appendix G.

1. Dividing a program into functions 
        a. is the key to object-oriented programming. 
        b. makes the program easier to conceptualize. 
        c. may reduce the size of the program. 
        d. makes the program run faster.

 2. A function name must be followed by ________. 

3. A function body is delimited by ________. 

4. Why is the main() function special? 

5. A C++ instruction that tells the computer to do something is called a ________.

6. Write an example of a normal C++ comment and an example of an old-fashioned /* comment.

7. An expression 
        a. usually evaluates to a numerical value.
        b. indicates the emotional state of the program.
        c. always occurs outside a function.
        d. may be part of a statement.

8. Specify how many bytes are occupied by the following data types in a 32-bit system :
        a. Type int 
        b. Type long double
        c. Type float
        d. Type long

9. True or false : A variable of type char can hold the value 301.

10. What kind of program elements are the following?
        a.12                        b. 'a'
        c. 4.28915             d. Junglejim
        e. Junglejim()

11. Write statement that display on the screen
        a. the character 'x'
        b. the name Jim
        c. the number 509

12. True or false: In an assignment statement, the value on the left of the equal sign is always equal to the value on the right.

13. Write a statement that display the variable george in a field 10 characters wide.

14. What header file must you #include with your source file to use cout and cin ?

15. Write a statement that gets a numerical value from the keyboard and places it in the variable temp.

16. What header file must you #include with your program to use setw? 

17. Two exceptions to the rule that the compiler ignores whitespace are ________ and ________.

18. True or false: It’s perfectly all right to use variables of different data types in the same arithmetic expression. 

19. The expression 11%3 evaluates to ________. 

20. An arithmetic assignment operator combines the effect of what two operators? 

21. Write a statement that uses an arithmetic assignment operator to increase the value of the variable temp by 23. Write the same statement without the arithmetic assignment operator. 

22. The increment operator increases the value of a variable by how much? 

23. Assuming var1 starts with the value 20, what will the following code fragment print out? 
        cout << var1--; 
        cout << ++var1; 

24. In the examples we’ve seen so far, header files have been used for what purpose? 

25. The actual code for library functions is contained in a ________ file.



Answers to the starred exercises can be found in Appendix G.

Question No 1 : 


Assuming there are 7.481 gallons in a cubic foot write a program that asks the user to enter a number of gallons and then displays the equivalent in cubic feet.



Q No 2: 


Write a program that generates the following table:

1990135
19917290
199211300
199316200

Use a single cout Statement for all output. Go to the editor .



Q No 3 : 

Write a program that generates the following output :
  • 10
  • 20
  • 19
Use an integer contant for the 10, an arithmetic assignment operator to generate the 20, and a decrement operator to generate the 19Go to the editor.



Q No 4 : 


Write a program that display your favourite poem. Use an appropriate escape sequence for the line breaks. If you don't have a favorite poem, you can borrow this one by Ogden Nash:
Candy is dandy, But liquor is quicker. Go to the editor.


Q No 5 : 

A library function, islower(), takes a single character (a latter) as an argument and return a nonero integer if the letter is lowercase or Zero if it is uppercase. This function requires the header file CTYPE.H. Write a rogram that allows the user to enter a letter, and then display either Zero or nonZero depending on whether a lowercase or uppercase letter was entered. Go to the editor.


Q No 6 : 

On a certain day the Birtish pound was equivalent to $1.487 U.S, the French franc was $0.172, the German deutschemark was $0.584, and the Japanese Yen was $0.00955. Write a program tahat allowa the user to enter an amount in dollars, and then displays this value converted to these four other montary units. Go to the editor.


Q No 7 : 

You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5 and adding 32. Write a program that allows the user to enter a floating-point number representing degrees Clsius, and then display the corresponding degrees Fahrenhit. Go to the editor



Q No 8 : 

When a value is smaller than a field specified eith setw(),the unused locations are,, by default filled in with spaces. The manipulator setfiil () takes a single character as an argument and causes this character to be substituted for spaces in the empty parts of a field. Reewrite the WIDTH program so that the characters onn each line between the location name and the population number are filled in with periods instead of spaces, as in
       Portcity.....2425785
 Go to the editor.


Q No 9 : 

If you have two fractions, a/b and c/d, their sum can be obtained from the formula 
     a / b        c / d        a*d + b*c / b*d
For example, 1/4 plus 2/3 is 
    1 / 4  +   2 / 3  =  1*3+4*2 / 4*3  =   3+8 / 12  =   11 / 12
write a program that encourages the user to enter two fraction, and then displays their sum in fractional form. (You don't need to reduce it to lowest terms.) The interaction with the user might look like this :
    Enter second fracttion : 2/5
    sum = 9/10
   Enter first fraction : 1/2
you can take advantage of the fact that the extraction operator (>>) can be chained to read in more than one quantity at once :
    cin >> a >> dummychar >> b;  Go to the editor.



Q No 10 :  

In the heyday of the British empire, Great Britain used a monetary system based on pounds, shiings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, and two decimal points, so that, for example, £5.28 meant 5 pound, 2 shillings, and 8 pence. (pence is the plural of penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S. dollars and cents). We’ll call this new system decimal pounds. Thus £5.28 in the old notation is £5.13 in decimal pounds (actually £5.1333333). write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user’s interaction with the program would be

        Enter pounds : 7

        Enter shillings : 17

        Enter pence : 9

Decimal pounds = £7.89

In most compilers you can use the decimal number 156 (hex character constant ‘\x9c’) to represent the pound sign (£). In some compilers, you can put the pound sign into your program directly by pasting it form the windows Character Map acceccory. Go to the editor.






Q No 11 :

By default, output is right-justified in its field. You can left-justify text output using the manipulator setiosflags(ios::left). (For now, don’t worry about what this new notation means.) Use this manipulator, along with setw(), to help generate the following output: 

        Last name        First name        Street        address        Town                        State
 ----------------------------------------------------------------------------------------------------------------
        Jones                  Bernard           109          Pine               Lane Littletown       MI 
        O’Brian              Coleen            42 E.        99th               Ave. Bigcity             NY 
        Wong                  Harry              121-A     Alabama        St. Lakeville             IL



Q No 12 :

Write the inverse of Exercise 10, so that the user enters an amount in Great Britain’s new decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-shillings-pence notation. An example of interaction with the program might be

        Enter decimal pounds: 3.51 
        Equivalent in old notation = £3.10.2.

Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler warning. You can use statements like

        float decpounds; // input from user (new-style pounds) 
        int pounds; // old-style (integer) pounds 
        float decfrac; // decimal fraction (smaller than 1.0)

        pounds = static_cast(decpounds); // remove decimal fraction 
        decfrac = decpounds - pounds; // regain decimal fraction 

You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence. Go to the editor.


   Exercise 1                                                                                           Exercise 3