Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a function that takes inputs of a number of quarters, dimes, and nickel

ID: 3628082 • Letter: 2

Question

2. Write a function that takes inputs of a number of quarters, dimes, and nickels (whole numbers), then calculates and returns an output of the value of the total amount of money in dollars (a floating-point value). Use appropriate parameter passing and return mechanisms. Use appropriate datatypes. For example, with inputs of 4 quarters, 3 dimes, and 3 nickels, the result would be 1.45 dollars. Use proper code formatting techniques. Do not write a main routine. Your function does not do cin or cout.

Explanation / Answer

Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.

With that being said, I am following the directions in the assignment, so don't tell me to write this 5 other ways.

My issue is this. I have written the program, as shown below, and it all works EXCEPT that when I enter in a value of .24 and lower, the change count is incorrect.

#include <stdio.h> int change (float total, int *quarters, int *dimes, int *nickels, int *pennies); int main () { int quarters, dimes, nickels, pennies; float total; {     total = 1.88;               change (total, &quarters, &dimes, &nickels, &pennies);         printf("TOTAL VALUE ENTERED: $%.2f", total);         printf(" %3d quarters ", quarters);         printf(" %3d dimes ", dimes);         printf(" %3d nickels ", nickels);         printf(" %3d pennies ", pennies); } {     total=0.32;                  change (total, &quarters, &dimes, &nickels, &pennies);         printf(" TOTAL VALUE ENTERED: $%.2f",total);         printf(" %3d quarters ",quarters);         printf(" %3d dimes ", dimes);         printf(" %3d nickels ", nickels);         printf(" %3d pennies ",pennies); }      {               printf(" Please enter an amount of money: ");     scanf("%f", &total);                  change(total, &quarters, &dimes, &nickels, &pennies);         printf("TOTAL VALUE ENTERED: $%0.2f",total);         printf(" %3d quarters ",quarters);         printf(" %3d dimes ",dimes);         printf(" %3d nickels ",nickels);         printf(" %3d pennies ",pennies); }    fflush(stdin);   /* clear input area so you can pause */     getchar();   /* force the computer to pause until you press a key on the keyboard */ }       int change(float total, int *quarters, int *dimes, int *nickels, int *pennies) {         if( total >= 0.25 )       *quarters = (total/0.25);       if( total >= 0.10 )       *dimes = (total - (*quarters * 0.25))/0.10;       if( total >= 0.05 )       *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10))/0.05;       if( total >= 0.01)       *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01 + .005;                                   return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote