using a while loop statement, individually display the apples element in the "fruits" array.

1 visualizzazione (ultimi 30 giorni)
fruits = ["apricot", "pears", "pineapples", "apples"];
  1 Commento
DGM
DGM il 30 Ago 2023
Modificato: DGM il 30 Ago 2023
If we take "the fruits array" to mean the specific example given, then there is only one valid candidate. Given that, it's not clear that a loop is needed, let alone a while loop.
If we're talking about any list of fruit names, then like Walter said, it's important to be specific about what you want when you say "apples elements". It's also still unclear why a loop is needed.
% a list of fruit names
fruits = ["apricot", "pears", "pineapples", "apples", "Apple"];
% true for "apple" or "apples" (case-insensitive)
isapples = ismember(lower(fruits),{'apple','apples'});
% display the elements of the fruits array which meet the simple test
fprintf('%s\n',fruits(isapples))
apples Apple

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 29 Ago 2023
N2014671 = 0;
while N2014671 < numel(fruits)
N2014671 = N2014671 + 1;
T2014671 = fruits(N2014671);
if F2014671(T2014671)
fprintf('At index %d there was: ', N2014671); disp(T2014671);
end
end
function R2014671 = F2014671(V2014671)
R2014671 = false;
if V2014671 is an example of what you are searching for
R2014671 = true;
end
end
.. You need to define how to know whether a value is an example of what you are searching for. For example, if "apples" is acceptable, how about "Apples" or "apple", or "crabapple", or "McIntosh" or "mcintosh" or "pineapple" or "pomme" ?

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by