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,...
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
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 a code that takes the initial velocity and angle as an input and outputs...
MATLAB Write a code that takes the initial velocity and angle as an input and outputs the maximum height of the projectile and its air time. Follow the pseudo code below. This will not be provided in as much detail in the future so you’ll have to provide pseudocode or a flowchart to showcase your logic. For this first assignment though, the logic is laid out below with some indented more detailed instructions. PROGRAM Trajectory: Establish the User Interface (Typically...
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):
Using Matlab, write code that carries out the following instructions: **Within your Live Script, create a...
Using Matlab, write code that carries out the following instructions: **Within your Live Script, create a matrix A by starting with eye(5) and performing the following sequence of elementary row-operations: first, replace (row4) with [(row4) + (row2) ⋅3], then, interchange rows 1 and 3 of the obtained matrix, and, finally, scale row5 of the matrix from the previous step by 6 to get the output matrix A. Display A. Note: To complete this part, you, should, first, create (and output)...
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
I need to write a MATLAB code for this question, but I couldn't figure how to...
I need to write a MATLAB code for this question, but I couldn't figure how to do it. 7. Engineers often have to convert from one unit of measurement to another; this can be tricky sometimes. You need to think through the process carefully. For example, convert 5 acres to hectares, given that an acre is 4840 square yards, a yard is 36 inches, an inch is 2.54 cm, and a hectare is 10000 m2.
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it...
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it simple, but there is no other rules. Thank you.
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT