SI 204 Spring 2017 / HWs


This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 17: Pointer arguments

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Monday, February 27
  • This homework contains code to be submitted electronically. Put your code in a folder called hw17 and submit using the 204sub command.
  • This is an electronic homework and you do not need to print out anything to turn in during class.

Assignment

  1. Required reading: Section 3 (passing pointers to functions) from the Unit 5 notes,
  2. Below is a `main` function for a program that reads in a dollar amount like `$3.50` and converts it into dollars and cents:
    int main() {
      printf("Enter amount: ");
      fflush(stdout);
    
      double amt;
      if (scanf(" $%lg", &amt) != 1) {
        printf("ERROR: must enter something like $3.50\n");
        return 1;
      }
    
      // FUNCTION CALL HERE!
      int dollars;
      int cents;
      split(amt, &dollars, &cents);
    
      printf("That's %i dollars and %i cents.\n", dollars, cents);
    
      return 0;
    }
    What's missing from this program is the prototype and definition for the function split which takes in a total amount as a double and splits it into two ints for dollars and cents.
    Complete the program by providing a prototype and definition for split and submit your code in a file called cents.c.

    Examples:

    roche@ubuntu$ ./cents
    Enter amount: $3.50
    That's 3 dollars and 50 cents.
    
    roche@ubuntu$ ./cents
    Enter amount: $2
    That's 2 dollars and 0 cents.
    
    roche@ubuntu$ ./cents
    Enter amount: $81.67
    That's 81 dollars and 67 cents.