How to use fprintf function to display information properly?

Given: Radius of 10,15,20 ft and height of 6, 12, 18, 24, 30 ft; Volume of a cylinder is pi*r^2*h
Find: Display the various heights and radii, prompt user to enter minimum acceptable volume in ft^3 and display acceptable designs (which will be a logical) that result in a 1.
...
I just want to double check my code here. For some reason my radius and logical are not being displayed side by side, (like a [3x6] matrix), but instead are displaying like a list. It lists in the problem that I MAY need to create an additional matrix that combines the radius column and the logical array but my attemps were futile.
clear; clc; close all;
r=10:5:20;
h=6:6:30;
dvolume=pi.*r.^2.*h';
minvol=input('Enter the minimum acceptable volume in ft^3: ');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
req=dvolume>minvol;
%matreq=r;req % This is my attempt to create the matrix putting them in a [3x6] together
fprintf('Acceptable designs include those with a 1.\n')
fprintf('Heights (ft)'); fprintf('%6i',h); fprintf('\n')
fprintf('Radius (ft) \n')
fprintf('%i\n %1.1i\n',r,req)

2 Comments

Make life easier. Put the values in a table and then display the table.
Is that not what I did? I am super nooby at this coding gig, I have only been doing it for 1 month with minimal guidance.

Sign in to comment.

 Accepted Answer

clear; clc; close all;
r=10:5:20
r = 1x3
10 15 20
h=6:6:30;
dvolume=pi.*r.^2.*h';
minvol= 10; %input('Enter the minimum acceptable volume in ft^3: ');
req=dvolume>minvol
req = 5x3 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
%matreq=r;req % This is my attempt to create the matrix putting them in a [3x6] together
fprintf('Acceptable designs include those with a 1.\n')
Acceptable designs include those with a 1.
fprintf('Heights (ft)'); fprintf('%6i',h); fprintf('\n')
Heights (ft) 6 12 18 24 30
fprintf('Radius (ft) \n')
Radius (ft)
fprintf('%i %i %i\n',r,req)
10 15 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

9 Comments

use additional format specifiers acc to matrix size you want to print
This is extremely close! However formatting wise I need it to look more like this example:
Enter the minimum acceptable volume in ft^3: 10
Heights (ft) 6 12 18 24 30
Radius (ft)
10 1 1 1 1 1
15 1 1 1 1 1
20 1 1 1 1 1
clear; clc; close all;
r=10:5:20
r = 1x3
10 15 20
h=6:6:30;
dvolume=pi.*r.^2.*h';
minvol= 10; %input('Enter the minimum acceptable volume in ft^3: ');
req=dvolume>minvol;
%matreq=r;req % This is my attempt to create the matrix putting them in a [3x6] together
fprintf('Acceptable designs include those with a 1.\n')
Acceptable designs include those with a 1.
fprintf('Heights (ft)'); fprintf('%6i',h); fprintf('\n')
Heights (ft) 6 12 18 24 30
fprintf('Radius (ft) \n')
Radius (ft)
for k = 1:length(r)
fprintf('%i %i %i %i %i %i\n',r(k),req(:,k))
end
10 1 1 1 1 1 15 1 1 1 1 1 20 1 1 1 1 1
I am pulling my hair our with trial and error. I have the radius displaying properly:
fprintf(' %i\n %i\n %i\n',r,req)
But now the logical is displayed as a list under the radius not side by side like a matrix as shown in my example.
You need to reformat the code as shown previously adding more format specifiers, if you want a 3x 6 matrix as output
for k = 1:length(r)
fprintf('%i %i %i %i %i %i\n',r(k),req(:,k))
end
This worked beautifully! I haven't learned the "for" and "end" commands yet so I am not sure how they work and don't think I would be able to use it, sadly... The specific prompt states:
Use the fprintf function to display the matrix in a manner that the user can determine which designs fit her needs. See below for an example code output, where the user chose a minimum volume of 8800ft^3. This might take some trial and error. Keep in mind what you learned in the previous practice exercise. The design table can be completed with 3 lines of code: One for the row of heights, one for the word radius as the column header for the radii, and one line for the rest of the table. You may need to make one matrix that combines the radius column and the logical array.
The design table is formatted as shown:
Enter the minimum acceptable volume in ft^3: 8800
Heights (ft) 6 12 18 24 30
Radius (ft)
10 1 1 1 1 1
15 1 1 1 1 1
20 1 1 1 1 1
for input value of 8800 the logical array output is shown as below
clear; clc; close all;
r=10:5:20;
h=6:6:30;
dvolume=pi.*r.^2.*h';
minvol= 8800; %input('Enter the minimum acceptable volume in ft^3: ');
req=dvolume>minvol;
%matreq=r;req % This is my attempt to create the matrix putting them in a [3x6] together
fprintf('Acceptable designs include those with a 1.\n')
Acceptable designs include those with a 1.
fprintf('Heights (ft)'); fprintf('%6i',h); fprintf('\n')
Heights (ft) 6 12 18 24 30
fprintf('Radius (ft) \n')
Radius (ft)
fprintf('%i %i %i %i %i %i\n',[r;req]) % concatenate vertically
10 0 0 0 0 1 15 0 0 1 1 1 20 0 1 1 1 1
EUREKA! I swore I had already tried this but I guess I wasn't putting it all together properly. Thank you.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Tags

Asked:

on 23 Feb 2024

Commented:

on 25 Feb 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!