CIS 22B - Notes for Tuesday 2/13

  Announcements and Reminders


  Segmentation Fault Example


  enum (enumerated type)

  • is a type, an integer type
  • can be converted to an int
  • does not automatically act like an int, not int arithmetic
  • an int can be cast to an enum
  • can be used as an array index
  • adheres to scoping rules
  • can be anonymous
Example

enum comparison

  unions

  • like a struct, it contains members
  • members occupy the same area of memory
  • the size of the union is the size of its largest member
  • unions are used to represent mutually exclusive characteristics
  • adheres to scoping rules
  • can be anonymous
Example 1
Example 2

  Midterm - What to expect

  • One pages of notes (8 1/2 x 11, both sides).  No electronic devices.  No phones, calculators, laptops, etc
  • Written problems
  • Examples
  1. Write a function that converts a string into an int.  Assume the int is between 10 and 99.  Do not use the atoi() or the stoi() function.
  2. Write a function prototype for problem 1.
  3. Write a function call for the function you defined in problem 1.
  4. Write a function that converts an int between 10 and 99 into a string.
  5. Write a function prototype for problem 4.
  6. Write a function call for function you defined in problem 4.
  7. Declare a 2-dimensional array with 30 rows and 40 columns
  8. Write a function that takes the 2-dimensional array argument from problem 7 and assigns the even rows random even numbers between 1 and 100 and assigns the odd rows random odd numbers between 1 and 100.
  9. Write a function prototype for problem 8.
  10. Write a function call for function you defined in problem 8.
  11. What's the output from the following ...
  • How to study for the midterm.

  Lab Exercise #6

  Put your name, the compiler and operating system used, and Lab Exercise #6 in a comment at the top of your program. Email your source code. This lab exercise is due Thursday at 11:30.

  Complete this program using the following declarations, the TypeString array, and the main().

enum Type{Even, Odd};

union Value
{
    int i;
    double d;
};

struct Number
{
    Type type;
    Value value;
};

const string TypeString[2] = {"even","odd"};

int main()
{
    Number var[10];
    for (int i= 0; i < 10; i++)
        assign(var[i]);
    for (int i= 0; i < 10; i++)
        print(var[i]);
}

You will need to add assign() and print() functions.  The assign() function generates a random int between 1 and 100.  If the int is even, it is stored in the Value union and Even in the Type.  If the int is odd, then store the square root of the random number is stored in the Value union and Odd in the Type.  The output from the print function should look like the following.  Make use of the TypeString array in your print() function.

Number is even 42
Number is even 68
Number is odd 5.91608
Number is odd 1
Number is even 70
Number is odd 5
Number is odd 8.88819
Number is odd 7.68115
Number is odd 7.93725
Number is odd 8.06226