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...
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
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 an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart to compute the largest and smallest of 4 numbers : Write a Java Program for the above flowchart, use methods(functions), and nested if-else statements. Take input from the user using the Scanner object. Use separate methods to compute the Largest and Smallest of the numbers. Method 1 Name: findLargest(param 1, param 2, param 3, param 4) Method 2 Name: findSmallest(param 1, param 2, param...
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...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT