Question

In: Computer Science

Write a function that takes a list of integers as input and returns a list with...

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.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
void bubbleSort(vector<int> &arr){
    int n = arr.size();

    for(int i=0;i<n;i++){ // loop for the bubble sort
        for(int j=0;j<n-i-1;j++){
            if(arr[j]<arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    return;
}
vector<int> solve(int n, vector<int> &v){
    vector<int> res; // forming the result list which is going to be returned

    for(int i=0;i<n;i++){ // iterating on all the elements of given list of elements
        if(v[i]%2 ==0){ // checking the condition for even elements
            res.push_back(v[i]);
        }
    }

    bubbleSort(res); // calling the bubble sort function to sort in descending order

    return res; // returning the list of even elements arranged in descending order
}
int main() {
    int n; // n is the variable 
    cin>>n; // taking input -  no of elements present in the list

    vector<int> v(n); // forming the list of n elements
    for(int i=0;i<n;i++) cin>>v[i]; // taking input - the elements present in the list

    vector<int> res = solve(n,v); // calling the required function solve - to seperate the even elements
    

    for(int i=0;i<res.size();i++) cout<<res[i]<<" "; // printing the resulted list 
    cout<<endl;




}


Related Solutions

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.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT