/** * ch6sam2.c * written by Derrick Stolee */ #include double buyCoffee(int *numCups, int* coupon); int main(void) { int numCoffees; int bought = 0; int coupon = 0; double totalAmount = 0.0; double cost; printf("How many coffees would you like to buy? "); scanf("%d", &numCoffees); int i; for ( i = 0; i < numCoffees; i++ ) { cost = buyCoffee(&bought, &coupon); printf("Bought a coffee for $%1.2lf\n",cost); totalAmount = totalAmount + cost; } printf("Bought %d coffees for $%1.2lf", bought, totalAmount); if ( coupon ) { printf(" and I have a coupon!\n"); } else { printf("\n"); } return 0; } /* request to buy some coffee, with a possible coupon */ double buyCoffee(int *numCups, int* coupon) { /* modify the number of cups so far */ (*numCups) = (*numCups)+1; /* did they have a coupon? */ if ( *coupon == 1 ) { /* use the coupon */ (*coupon) = 0; return 0.0; } else if ( *numCups % 5 == 0 ) { /* add a coupon with this purchace? */ (*coupon) = 1; } /* charge normal price */ return 1.75; }