fork download
  1. % Prompt user for input vector AM
  2. AM = input('Please enter a vector (e.g., [1, 2, 3, 4]): ');
  3.  
  4. % Check if the number of elements in AM is even
  5. if mod(length(AM), 2) ~= 0
  6. fprintf('Error: The vector must contain an even number of elements.\n');
  7. else
  8. % Initialize variables for product of even elements and sum of odd elements
  9. product_even = 1; % Start at 1 for multiplication
  10. sum_odd = 0; % Start at 0 for addition
  11.  
  12. % Iterate through each element in AM
  13. for i = 1:length(AM)
  14. if mod(AM(i), 2) == 0 % Check if the element is even
  15. product_even = product_even * AM(i);
  16. else % The element is odd
  17. sum_odd = sum_odd + AM(i);
  18. end
  19. end
  20.  
  21. % Calculate PDS
  22. if sum_odd ~= 0 % Prevent division by zero
  23. PDS = product_even / sum_odd;
  24. fprintf('The result PDS is: %.4f\n', PDS); % Display the result
  25. else
  26. fprintf('Error: Sum of odd elements is zero, cannot compute PDS.\n');
  27. end
  28. end
  29.  
Success #stdin #stdout #stderr 0.12s 47160KB
stdin
Standard input is empty
stdout
Please enter a vector (e.g., [1, 2, 3, 4]): 
stderr
error: input: reading user-input failed!
error: called from
    /home/zt1LzH/prog.octave at line 2 column 4