Answer to Question #295746 in C++ for Shan

Question #295746

C++ program) that will accept an integer value in the range of 5-95 inclusive. Your solution should also ensure that input values ar in multiples of 5. Based on valid input, your solution should calculate how many coins of each denomination should be returned, and display this to the user. Coin values are 50, 20, 10 and 5. The solution should aim to give as much of the higher valued coins as possible. For example, a poor solution for an input of 30 cents would give six 5 cent coins. A correct solution would give a 20 cent coin and a 10 cent coin. After each output, the user should be asked whether they wish to continue or exit/terminate the program. Your solution (algorithm and program) should be designed using a modular approach. This requires the submission of a structure chart, a high level algorithm, and subsequent decompositions of each step (i.e. low-level algorithms). Hint 1- multple of 5 condition. 2- do while loop in switch to terminate the question. 3- no of inp is divisible of 5


1
Expert's answer
2022-02-09T15:58:40-0500
#include <iostream>
#include <cstdlib>
using namespace std;


int main() {
    int change;


    cout << "Enter a value in the range 5 - 95: ";
    cin >> change;
    if ( change < 5 || change >95 ) {
        cout << "The value out of the range" << endl;
        exit(1);
    }
    if (change % 5 != 0) {
        cout << "The value must by multilies of 5" << endl;
        exit(1);
    }


    int values[] = {50, 20, 10, 5};
    const int n = sizeof(values) / sizeof(values[0]);
    int coins[n] = {0};


    for(int i=0; i<n; i++) {
        while (change >= values[i]) {
            change -= values[i];
            coins[i]++;
        }
    }


    for (int i=0; i<n; i++) {
        if (coins[i]) {
            cout << values[i] << " cent coins: " << coins[i] << endl;
        }
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog