Self-Review Exercises

  1. 22.1 Fill in the blanks in each of the following:

    1. 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.

    2. 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.

    3. Keyword            introduces a structure declaration.

    4. Keyword            is used to create a synonym for a previously defined data type.

    5. 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.

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

    7. The            and            operators are used to shift the bits of a value to the left or to the right, respectively.

  2. 22.2 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.

  3. 22.3 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 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.

Answers to Self-Review Exercises

  1. 22.1

    1. bitwise AND (&).

    2. bitwise inclusive OR (|).

    3. struct.

    4. typedef.

    5. bitwise exclusive OR (^).

    6. mask.

    7. left-shift operator (<<), right-shift operator (>>).

  2. 22.2

    1.  

      
      struct Part {
         int partNumber;
         char partName[26];
      };
      
    2. typedef Part* PartPtr;

    3.  

      
      Part a;
      Part b[10];
      Part* ptr;
      
    4. cin >> a.partNumber >> a.partName;

    5. b[3] = a;

    6. ptr = b;

    7.  

      
      cout << (ptr + 3)->partNumber << ' '
           << (ptr + 3)->partName << endl;
      
  3. 22.3

    1. c = toupper(c);

    2.  

      
      cout << ''' << c << "' "
           << (isdigit(c) ? "is a" : "is not a")
            << " digit" << endl;
      
    3. cout << atol("1234567") << endl;

    4.  

      
      cout << ''' << c << "' "
           << (iscntrl(c) ? "is a" : "is not a")
            << " control character" << endl;
      
    5. ptr = strrchr(s1, c);

    6. out << atof("8.63582") << endl;

    7.  

      
      cout << ''' << c << "' "
           << (isalpha(c) ? "is a" : "is not a")
            << " letter" << endl;
      
    8. ptr = strstr(s1, s2);

    9.  

      
      cout << ''' << c << "' "
           << (isprint(c) ? "is a" : "is not a")
            << " printing character" << endl;
      
    10. ptr = strpbrk(s1, s2);

    11. ptr = strchr(s1, c);

    12. cout << atoi("-21") << endl;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset