www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1102 (continued)]

Self-Review Exercises

22.1

Fill in the blanks in each of the following:

  1. A(n)__________is a collection of related variables under one name.

  2. The bits in the result of an expression using the__________operator are set to one if the corresponding bits in each operand are set to one. Otherwise, the bits are set to zero.

  3. The variables declared in a structure definition are called its__________.

  4. The bits in the result of an expression using the__________operator are set to one if at least one of the corresponding bits in either operand is set to one. Otherwise, the bits are set to zero.

  5. Keyword__________introduces a structure declaration.

  6. Keyword__________is used to create a synonym for a previously defined data type.

  7. Each bit in the result of an expression using the__________operator is set to one if exactly one of the corresponding bits in either operand is set to one. Otherwise, the bit is set to zero.

  8. The bitwise AND operator & is often used to__________bits (i.e., to select certain bits from a bit string while zeroing others).

  9. A structure member is accessed with either operator__________or__________.

  10. The__________and__________operators are used to shift the bits of a value to the left or to the right, respectively.

22.2

State whether each of the following is true or false. If false, explain why.

  1. Structures may contain only one data type.

  2. Members of different structures must have unique names.

  3. Keyword typedef is used to define new data types.

  4. Structures are always passed to functions by reference.

22.3

Write a single statement or a set of statements to accomplish each of the following:

  1. Define a structure called Part containing int variable partNumber and char array partName, whose values may be as long as 25 characters.

  2. Define PartPtr to be a synonym for the type Part *.

  3. Use separate statements to declare variable a to be of type Part, array b[ 10 ] to be of type Part and variable ptr to be of type pointer to Part.

  4. Read a part number and a part name from the keyboard into the members of variable a.

  5. Assign the member values of variable a to element three of array b.

  6. Assign the address of array b to the pointer variable ptr.

  7. Print the member values of element three of array b, using the variable ptr and the structure pointer operator to refer to the members.


[Page 1103]
22.4

Find the error in each of the following:

  1. Assume that struct Card has been defined as containing two pointers to type charnamely, face and suit. Also, the variable c has been declared to be of type Card, and the variable cPtr has been declared to be of type pointer to Card. Variable cPtr has been assigned the address of c.

    cout << *cPtr.face << endl;
    
  2. Assume that struct Card has been defined as containing two pointers to type charnamely, face and suit. Also, the array hearts[ 13 ] has been declared to be of type Card. The following statement should print the member face of element 10 of the array.

    cout << hearts.face << endl;
    
  3. struct Person
    {
       char lastName[ 15 ];
       char firstName[ 15 ];
       int age;
    }
    

  4. Assume that variable p has been declared as type Person and that variable c has been declared as type Card.

    p = c;
    
22.5

Write a single statement to accomplish each of the following. Assume that variables c (which stores a character), x, y and z are of type int; variables d, e and f are of type double; variable ptr is of type char * and arrays s1[ 100 ] and s2[ 100 ] are of type char.

  1. Convert the character stored in variable c to an uppercase letter. Assign the result to variable c.

  2. Determine if the value of variable c is a digit. Use the conditional operator as shown in Figs. 22.1822.20 to print " is a " or " is not a " when the result is displayed.

  3. Convert the string "1234567" to long, and print the value.

  4. Determine whether the value of variable c is a control character. Use the conditional operator to print " is a " or " is not a " when the result is displayed.

  5. Assign to ptr the location of the last occurrence of c in s1.

  6. Convert the string "8.63582" to double, and print the value.

  7. Determine whether the value of c is a letter. Use the conditional operator to print " is a " or " is not a " when the result is displayed.

  8. Assign to ptr the location of the first occurrence of s2 in s1.

  9. Determine whether the value of variable c is a printing character. Use the conditional operator to print " is a " or " is not a " when the result is displayed.

  10. Assign to ptr the location of the first occurrence in s1 of any character from s2.

  11. Assign to ptr the location of the first occurrence of c in s1.

  12. Convert the string "-21" to int, and print the value.


Previous Page
Next Page