Question

In: Computer Science

Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...

Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector
that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against
probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function
round(). Check your code with the following arrays:


>> y1 = [18, -5, 89, -7, 4, 10, 12, 34, -2, 45];
>> y2 = [34, 67, 89, 0, 12, 87, 0, 5];
>> y3 = [ 8, 3, 11, 4, 7, 12, 19, 3, 5, 6]


>> z1 = sort1(y1)
z1 =
NaN
>> z2 = sort1(y2)
z2 =
NaN
>> z3 = sort(y3)
z3 =
3 11 7 19 3 5

** You cannot use any built in Matlab functions other than round(). I don't need all the test runs. I'm just stuck halfway through the code and can't finish it.

Solutions

Expert Solution

function odd_array = sort1(array)
% Matlab function that takes in an array of non-zero positive integers as input and returns a second vector
% that contains only the odd numbers
% It will return zero if all elements are even.
% In case of an error, it will return NaN
odd_array = 0;
k=0;
% loop over the array
for i =1:length(array)
% if array contains any negative or zeros , set odd_aray to NaN and
% end the loop
if(array(i) <= 0)
odd_array = NaN;
break;
% if array contains any decimal values, set odd_array to NaN and end the loop   
elseif((round(array(i),1) - round(array(i))) > 0)
odd_array = NaN;
break;
% if ith element is odd, add ith element to odd_array
elseif((round(array(i)/2,1)-round(array(i)/2)) ~= 0)
k = k +1;
odd_array(k) = array(i);
end
end

end

%end of function

Output:


Related Solutions

Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
In the code cell below, use two input() commands to collect two positive, non-zero integers from...
In the code cell below, use two input() commands to collect two positive, non-zero integers from the user. These values represent the starting and ending values of a range (you may assume that the first value is always strictly less than the second value). Next, use a loop to examine and count every integer in the range (including the starting and ending values) that contains at least one 7 among its digits. When your loop ends, print the total number...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT