CIS 22B - Notes for Mon Oct 30

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 used, and Lab Exercise #6 in a comment at the top of your program. Email your source code. This lab exercise is due at the beginning of the next lecture.  You may email your lab solution prior to that.

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 0 and 99.  If the int is odd, it is stored in the Value union and Odd in the Type.  If the int is even, then store the square root of the random number is stored in the Value union and Even 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 odd 41
Number is odd 67
Number is even 5.83095
Number is even 0
Number is odd 69
Number is even 4.89898
Number is even 8.83176
Number is even 7.61577
Number is even 7.87401
Number is even 8