Question

In: Computer Science

develop an algorithm and then a C program that accomplishes the following. determines the minimum number...

  • develop an algorithm and then a C program that accomplishes the following.
    • determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive;
    • produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input;
    • terminate if 0 or a negative number is entered.
    • Here is possible example of the program running (remember that there could be any number of inputs (perhaps 1,000,000) but all inputs are integers)

Hints: use if / else if / else... use % and /... use a while loop or a do while loop

example:

Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  31
             Quarters: 1   Dimes: 0   Nickels: 1   Pennies: 1

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  89
               
Quarters: 3 Dimes: 1 Nickels: 0 Pennies: 4
               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  121
   Your entered a value outside the range.

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  0
   Your entered a value outside the range.
               
Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  18
               Quarters: 0 Dimes: 1 Nickels: 1 Pennies: 3
   Your entered a value outside the range.

               Please enter the number of cents, from 1 through 99, or enter a negative number to quit:  -666   
               Thanks for using my program. Good bye.

Solutions

Expert Solution

Steps to follow:

  1. Define an array for value of Quarter as 25, Dimes as 10, Nickels as 5 and Pennies as 1.
  2. Define an array of size 4 to store count of each coin type used
  3. Run a for loop and get minimum coin number of each coin type by dividing the cents value (from the user) with the coin value, and also updating the cents value (from user) by taking the modulus with the coin value.
  4. Check for negative value input from user to exit the program and the values that are not in range.

Code:

#include <stdio.h>

void minCoins(int value)
{       
        // coin value array representing indices 0 - quarters, 1 - dimes,  2 - nickels, 3 - pennies.
        int coin_value[4] = {25, 10, 5, 1};
        
        // result array to store number of coins for each coin type in same order
        int coin_num[4] = {0, 0, 0, 0};
                
                // using for loop and divide & modulus operator to get number of coins & remaining value respectively 
                // for every coin type
                
                for (int i = 0; i < 4; i++) { 
                    coin_num[i] = value / coin_value[i];
                    value = value % coin_value[i];
                } 
                
        // Output of total no. of coins with each coin type number
                printf("Quarters: %d ", coin_num[0]);
                printf("Dimes: %d ", coin_num[1]);
                printf("Nickels: %d ", coin_num[2]);
                printf("Pennies: %d \n", coin_num[3]);
                
}

int main() {
    int num;
    while (1) {
        printf("Please enter the number of cents, from 1 through 99, or enter a negative number to quit: ");
        scanf("%d", &num);
        if (num < 0) {
            printf("Thanks for using my program. Good bye.");
            break;
        }
        else {
            if (num > 0 && num < 100)
                minCoins(num);
            else
                printf("Your entered a value outside the range.\n");
        }
    }
    return 0;
}

Output:


Related Solutions

develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
Prime Number Determines if a number is prime or not I also need the algorithm and...
Prime Number Determines if a number is prime or not I also need the algorithm and pseudocode in java.
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop) 1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a =...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT