Write a function count_odd that takes two arguments: low_bound and high_bound. The function counts how many odd values are there between the low_bound and the high_bound. Last it returns the number of odd numbers between these two values.

Here is what I have. All help is appreciated.
function count_odd(low_bound, high_bound)
% promt user to enter low_bound and high_bound
low_bound=input('Enter low bound: ')
high_bound=input('Enter high bound: ')
end

3 comentarios

You are passing low_bound and high_bound into the function so you shouldn't then be asking for them as input.
Since it is your question though you need to show a little more effort that that on the actual question at hand - i.e. counting odd values.
doc rem
doc sum
would both be helpful for doing that.
How do I get it to display how many odd values there are? That is what I am not understanding. Say I enter 12 and 20, how do I get it to display 4 because that's how many odd values are between there.
The questions asks you to return it, not display it, so just calculate it in a variable and return that variable from the function.

Iniciar sesión para comentar.

 Respuesta aceptada

l=1 %lower bound
u = 9 %upper bound
boundary_vector=l:u
counter = 0;
for i = 1:length(boundary_vector)
if rem(boundary_vector(i),2)==0
counter = counter + 1;
end
end
odds = length(boundary_vector) - counter

4 comentarios

When I do this I get an error that say undefined function or variable n.
That's what often happens when you just copy someone's answer rather than work out how to do it yourself!
I didn't just copy it. I have my own work down and used this as help. My code is a function because that is what the question asked for.
Updated the code, you can try it now

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Jul. de 2018

Editada:

el 26 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by