Generative AI
Follow


Kali

Help

Kali on 30 Nov 2023 (Edited on 30 Nov 2023)
Latest activity Edit by David on 21 Dec 2023

Write a matlab script that will print the odd numbers, 1 through 20, in reverse.
I cannot figure out how to do this correctly, please help.
Walter Roberson
Walter Roberson on 30 Nov 2023 (Edited on 21 Dec 2023)
David
David on 30 Nov 2023
Walter's right, this question is easily answered in the Playground
for i = 19:-2:1
disp(i+1);
end
Walter Roberson
Walter Roberson on 30 Nov 2023
My prompt:
Certainly! Here's a MATLAB script that will print the odd numbers from 2 to 20 in reverse:
Response:
Thank you for bringing that to my attention. I apologize for the mistake in my previous response. Here's the correct MATLAB script that will print the odd numbers from 2 to 20 in reverse:
for i = 19:-2:1
disp(i*2)
end
This code uses a for loop to iterate over the odd numbers from 19 to 1, decrementing by 2 each time. The disp function is used to print each number to the command window, but we multiply i by 2 to get the correct odd number.
Walter Roberson
Walter Roberson on 30 Nov 2023
"Write a matlab script that will print the odd numbers, up to 20, in reverse." produces valid code. But
"Write a matlab script that will print the odd numbers, from 1 to 20, in reverse."
fails, printing even numbers instead.
David
David on 21 Dec 2023
Replying to the AI assistant to correct it yeilded the right code. I said "That code prints the even numbers, I asked you to print the odd numbers."
for i = 19:-2:1
disp(i);
end

See Also

Tags

No tags entered yet.