Question

In: Computer Science

MATLAB CODE Let’s say you need to write a small script that takes in the total...

MATLAB CODE

Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies.

  • Initialize counter variables for quarters, dimes, nickels and pennies to 0. Counter variables are used to keep track of how many of each coin are to be returned.
  • Calculate the amount of change to be given using the ‘total’ and ‘cost’ variables.
  • While there is still change to be given, find the largest coin that can be returned, increment the coin’s counter, and subtract the coin value from the amount of change to be given.
  • Display the total change, as well as how many of each coin are being returned.

function value = get_coin_value(coin)

%Set value to be the correct number based on coin.

%For example, if coin == 'q', value = 25

THIS IS THE CODE SO FAR FOR THE PARTS BEFORE THE ONE I NEED HELP WITH

if coin == 'q'

value = 25;

elseif coin == 'd'

value = 10;

elseif coin == 'n'

value = 5;

elseif coin == 'p'

value = 1;

else

value = 0;

end

%}

function total = insert_coins

%your code here

total = 0;

%Loop to keep asking user for coin till total is less than 115 cents

while(total<115)

coin = input('Enter q for quarter, d for dime, n for nickel,p for penny:','s');

total = total + get_coin_value(coin);

end

disp("Your NAU power juice has been dispensed")

  

end

THIS IS THE CODE I NEED HELP WITH GETTING TO WORK

function [quarters, dimes, nickels, pennies] = get_change(total)

%your code here

cost = 115;

total = insert_coins;

change = total - cost;

while change >= 25

   quarters = 'q' + 1;

   change = change - 25;

end

while change >= 10

   dimes = 'd' + 1;

   change = change - 10;

end

while change >= 5

   nickels = 'n' + 1;

   change = change - 5;

end

while change >= 1

   pennies = 'p' + 1;

   change = change - 1;

end

disp('Total change: ' + (total - cost))

disp('Quarters: ' + quarters)

disp('Dimes: ' + dimes)

disp('Nickels: ' + nickels)

disp('Pennies: ' + pennies)

end

Solutions

Expert Solution

ANSWER:

I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE TEXT (function get_change)

function [quarters, dimes, nickels, pennies] = get_change(total,cost)
    %your code here
    change = total - cost;
    % defining quarters, dimes , nickels and pennies counter
    quarters = 0; dimes= 0; nickels = 0; pennies = 0;
    while change >= 25
        quarters = quarters + 1;
        change = change - 25;
    end
    
    while change >= 10
        dimes = dimes + 1;
        change = change - 10;
    end
    
    while change >= 5
        nickels = nickels + 1;
        change = change - 5;
    end
    
    while change >= 1
        pennies = pennies + 1;
        change = change - 1;
    end
    
    fprintf('Total change: %d\n',(total - cost));
    fprintf('Quarters: %d\n',quarters);
    fprintf('Dimes: %d\n',dimes);
    fprintf('Nickels: %d\n',nickels);
    fprintf('Pennies: %d\n',pennies);

end

CODE IMAGE (function get_change)

OUTPUT IMAGE


Related Solutions

MATLAB CODE Let’s say you need to write a small script that takes in the total...
MATLAB CODE Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies. Initialize counter variables for quarters, dimes, nickels and pennies to 0. Counter variables are used to keep track of how many of each coin are to be returned. Calculate the amount of change to be given using the ‘total’ and ‘cost’ variables. While there is still change to be given,...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
Matlab: How would I write a script that calculates a. 654.23+345.73 b. 3984566/5.5 (need to use...
Matlab: How would I write a script that calculates a. 654.23+345.73 b. 3984566/5.5 (need to use the  format long command)
write a matlab script for double mass spring system with animation.
write a matlab script for double mass spring system with animation.
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as...
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as well as the tank dimensions in feet. Assume that the initial measurements are 7 meters in diameter and 11 meters tall. Display your final answers to the screen using disp and a statement without a semicolon, e.g. write the following into your script disp(‘The capacity in U.S. gallons is:’), capacity, where capacity is a variable that you defined in preceding calculations. Part B: In...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
3. Write a Matlab script that describes the dynamics of Pressure and Flow in the systemic...
3. Write a Matlab script that describes the dynamics of Pressure and Flow in the systemic arteries and Left Ventricle. Assume that : 1) Pressure in the Left atrium (PLA) and in the Systemic Veins (Psv) remain constant. 2) A time dependent LV compliance 3) Opening/Closing of the heart valves instantaneously with the direction of flow (i.e. valves are at an open or closed states. Use parameter values from the handout.
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT