Using FOR or WHILE statement to find measurements

1 visualización (últimos 30 días)
Bryce
Bryce el 21 de Mzo. de 2023
Editada: Shuba Nandini el 27 de Mzo. de 2023
I was given the following prompt:
  1. Ask the user to enter an array where each row contains the information for a single bolt, with the first column always giving the diameter of the bolt (in inches) and the second column always giving the length of the corresponding bolt (in inches). Store this array in a variable called bolts. For example, if the user wants to test four bolts, one with diameter 1.1 and length 14.5, one with diameter 1.2 and diameter 15, one with diameter 1 and length 15, and one with diameter 1.5 and length 15.1, he should enter [ 1.1, 14.5; 1.2, 15; 1, 15; 1.5, 15.1].
  1. Create the following variables, and give them all an initial value of 0.
  2. out_of_spec: will track how many bolts are outside of specifications in either diameter or length (or both)
  3. too_long: will track how many bolts have a length longer than 15.075 inches
  4. too_short: will track how many bolts have a length shorter than 14.925 inches
  5. smaller_diameter: will track how many bolts have a diameter smaller than 0.995 inches
  6. larger_diameter: will track how many bolts have a diameter larger than 1.005 inches
  7. both_out_of_spec: will track how many bolts have both their length and diameters outside of the acceptable ranges of values.
  1. Use a FOR or WHILE loop to analyze the bolts array row by row (i.e. bolt by bolt) to check to see if the length and diameter of each bolt fall within specifications or not, and if not, then update the value of the appropriate variable(s) from above to keep running totals of how many bolts have been outside of specifications.
  2. Note: You may find it useful to know that you can determine the number of rows in bolts using the command “size(bolts,1)” and you can determine the number of columns in bolts using the command “size(bolts,2)”.
I wrote up the code however it is not tracking the bolts sizes inputed
Heres the code:
bolts = input('please input specs of bolt in array form');
diameter = bolts(:,1);
length = bolts(:,2);
nbolts = size(bolts,1);
for i=1:1:nbolts
if length(i) > 15.075
disp('bolt is too long')
elseif length(i) < 14.925
disp('bolt is too short')
elseif diameter(i) < 0.995
disp('bolts diamter is too small')
elseif diameter(i) > 1.005
disp('bolts diameter is too big')
end
end
  3 comentarios
Bryce
Bryce el 21 de Mzo. de 2023
That would just be out_of_spec=0 etc. correct? And if that is the case how do I put into my code for these to change based on the array inputted?
Walter Roberson
Walter Roberson el 21 de Mzo. de 2023
this_is_a_variable = 0;
if some_condition
this_is_a_variable = this_is_a_variable + 1;
end

Iniciar sesión para comentar.

Respuestas (1)

Shuba Nandini
Shuba Nandini el 27 de Mzo. de 2023
Editada: Shuba Nandini el 27 de Mzo. de 2023
Hi Bryce,
I understand that you are unable to track the bolt sizes when are you are giving the input. Here is the way which might help you to take the bolt size as input using string array and track the number of out_of_spec bolts:
input_str = input('Enter the bolt information in the format [diameter1, length1; diameter2, length2; ...]: ', 's');
% Convert the string input to a numeric array
bolts = eval(input_str);
% Initialize the variables to 0
out_of_spec = 0;
too_long = 0;
too_short = 0;
smaller_diameter = 0;
larger_diameter = 0;
both_out_of_spec = 0;
% Loop through each bolt
for i = 1:size(bolts, 1)
diameter = bolts(i, 1);
length = bolts(i, 2);
% Check if the bolt falls outside the specifications
if diameter < 0.995 || diameter > 1.005 || length < 14.925 || length > 15.075
out_of_spec = out_of_spec + 1;
if diameter < 0.995
smaller_diameter = smaller_diameter + 1;
elseif diameter > 1.005
larger_diameter = larger_diameter + 1;
end
if length < 14.925
too_short = too_short + 1;
elseif length > 15.075
too_long = too_long + 1;
end
if (diameter < 0.995 || diameter > 1.005) && (length < 14.925 || length > 15.075)
both_out_of_spec = both_out_of_spec + 1;
end
end
end
% Print the results
fprintf('Number of bolts outside specifications: %d\n', out_of_spec);
fprintf('Number of bolts with length longer than 15.075 inches: %d\n', too_long);
fprintf('Number of bolts with length shorter than 14.925 inches: %d\n', too_short);
fprintf('Number of bolts with diameter smaller than 0.995 inches: %d\n', smaller_diameter);
fprintf('Number of bolts with diameter larger than 1.005 inches: %d\n', larger_diameter);
fprintf('Number of bolts with both length and diameter outside of the acceptable ranges: %d\n', both_out_of_spec);
To know more about "eval" function, please refer to this link: Evaluate MATLAB expression - MATLAB eval (mathworks.com)
Hope this helps.
Regards,
Nandini

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by