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.
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,...
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,...
Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
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(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT