CIS 22B - Notes for Tuesday 2/13

  Announcements and Reminders

Structs

A struct is 
  • an abstract data type
  • a composite type
  • a user-defined type
  • almost the exact same thing as a class in C++ (but we'll get to that later)
Structs contain members, accessible through the . (dot) operator, and maybe through the -> (arrow) operator.

Example:

struct something        // struct declaration
{
   int a;               // members
   float b;
   char c;
};

...

something var;          // you can also say "struct something var;"

// assign the members of the something variable (object)
var.a = 19;
var.b = 3.14;
var.c = 'x';

cout << var.a << ' ' << var.b << ' ' << var.c;    // prints 19 3.14 x
...

something* ps;         // ps is a pointer to something
ps = &var;             // assign the address of a struct variable to a pointer

cout << (*ps).a;       // prints 19
cout << ps->a;         // prints 19

struct initialization

Arrays of structs

Structs with arrays

The tm struct

Example

Midterm - What to expect

Topics

  • file I/O
  • arrays - 1D, 2D
  • sorting, searching
  • passing an array to a function, passing one row of a 2D array to a function
  • pointers
  • dynamic memory allocation
  • c-strings
  • cctype functions
  • string class
  • structs

Midterm format

  • 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 Exercise #6 in a comment at the top of your program. This lab exercise is also due Thursday at 11:30.

Write a program to read the input file below, consisting of 9 records and produce the output shown.  The input data must be stored in a 9 element array of structs with 3 members.  Include 2 functions besides main().  One function should read in the data and the second function should write it out.

Input file

Barry    12345    67.23
Cary    543210    19.27
Gary    654321    777.32
Harry    123456    500
Jerry   19      100.02
Larry    234567    999.99
Mary    345678    234.56
Perry   789     1.24
Terry   123456  876.3

Output 

Barry   12,345   $67.23
Cary    543,210  $19.27
Gary    654,321  $777.32
Harry   123,456  $500.00
Jerry   19       $100.02
Larry   234,567  $999.99
Mary    345,678  $234.56
Perry   789      $1.24
Terry   123,456  $876.30