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 the204sub
command. - This is an electronic homework and you do not need to print out anything to turn in during class.
Assignment
- Required reading: Section 3 (passing pointers to functions) from the Unit 5 notes,
-
Below is a `main` function for a program that reads in a dollar
amount like `$3.50` and converts it into dollars and cents:
What's missing from this program is the prototype and definition for the functionint 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, ¢s); printf("That's %i dollars and %i cents.\n", dollars, cents); return 0; }
split
which takes in a total amount as a double and splits it into twoint
s for dollars and cents.
Complete the program by providing a prototype and definition forsplit
and submit your code in a file calledcents.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.