Question

In: Computer Science

Note: Present results using fprintf()and comment your code throughout. Write a function that will compute the...

Note: Present results using fprintf()and comment your code throughout.

  1. Write a function that will compute the volume and surface area of a rectangular prism when provided with the object’s 3 numerical dimensions. In other words, the function should be given three inputs (height, width and depth) and return two outputs (the volume and surface area, in that order). Show the following test cases:
    1. height: 5   width:3    depth: 2
    2. heignt:1    width:8    depth:4
    3. height: 2   width:2    depth:10

The script should call the function 3 times and store the results of the three test cases in two row vectors called vol and sa.

Function specifications:

Input arguments: length, width, height - all scalars. (Order does not matter)

Output arguments: volume, area - both scalars. (Must be in this order)

OPTIONAL: If you prefer, you can write the function such that the input and output arguments are arrays; in that case, only one call to the function is necessary.

Solutions

Expert Solution

% Matlab function

function [volume, surface_area] = prism_volume_surfacearea(length, width, height)
% Matlab function prism_volume_surfacearea to calculate the volume and
% surface area of a rectangular prism for the given input length, width and height
% The input arguments can be scalars
% In case the input arguments is scalar, output values of volume and
% surface_area is also scalar
% calculate the volume of the rectangular prism
volume = length*width*height;
% calculate the surface area of the rectangular prism
surface_area = 2*(length * width + length * height+ width * height);
end
%end of function


% Matlab main script to calculate the volume and surface area of the
% rectangular prism for 3 different values of height, width and depth
% create row vectors of size for volume and surface_area
volume = zeros(1,3);
surface_area = zeros(1,3);
% define the height, width and depth variables
height = 5;
width = 3;
depth = 2;
% calculate and insert the volume and surface area into the corresponding vectors
[volume(1),surface_area(1)] = prism_volume_surfacearea(depth, width,height);
height = 1;
width = 8;
depth = 4;
[volume(2),surface_area(2)] = prism_volume_surfacearea(depth, width,height);
height = 2;
width = 2;
depth = 10;
[volume(3),surface_area(3)] = prism_volume_surfacearea(depth, width,height);
% loop to display the volume and surface area from the vectors
fprintf('%20s%20s\n','Volume','Surface Area');
for i=1:length(volume)
fprintf('%20.2f%20.2f\n',volume(i),surface_area(i));
end
%end of script

Output:


Related Solutions

Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Write this program using an IDE. Comment and style the code according to the CS 200...
Write this program using an IDE. Comment and style the code according to the CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. Monster collector is a game of chance, where the user tries to collect monsters by guessing the correct numbers between 1 and 5. If the user doesn't guess the incorrect number, you catch...
using math lab, Write a code to compute the change in dollars (no cents) to be...
using math lab, Write a code to compute the change in dollars (no cents) to be given back to a restaurant patron who pays the bill in cash. That is, find and enumerate the various combinations (of common currency notes only) that can amount to the change due.
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
public class Sum2 { // TODO - write your code below this comment. } Download the...
public class Sum2 { // TODO - write your code below this comment. } Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4: Sum: 7
public class FirstChar { // TODO - write your code below this comment. } Download the...
public class FirstChar { // TODO - write your code below this comment. } Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line argument foo is shown below: First...
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Write code that would go where the comment "#your line will go here" so that the...
Write code that would go where the comment "#your line will go here" so that the code would successfully extract all names of dishes from the dictionary popular_foods that have at least one of the following ingredients: popular_foods = {"Clam Chowder": {"Country": "United States", "Main Ingredients": ["clam", "onion", "celery", "potato", "carrot", "butter", "flour", "cream", "red wine vinegar"]}, "Xiaolongbao": {"Country": "China", "Main Ingredients": ["flour", "pork", "crab meat", "roe", "soy sauce", "ginger", "rice wine", "green onions"]}, "Feijoada": {"Country": "Brazil", "Main Ingredients": ["black...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var triangle.js Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT