Question

In C++ write a function object that compares two numbers and returns true if the first...

In C++ write a function object that compares two numbers and returns true if the first number is larger than the second number, and false otherwise. Use it to sort a vector of integers in descending order.

Homework Answers

Answer #1

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool compare (int a, int b)
{
if (a > b )
    return true;
else
    return false;
}

int main()
{
     vector<int> vect{ 1, 5, 8, 9, 3,25,7,11};        //Sample vector
     int vector_size = vect.size();       // storing the size of vector into vector_size
     int arr[vector_size], temp;
   
     for (int i=0; i<vector_size-1 ; i++)
     {
         for (int j=0; j< vector_size-i; j++)
         {
            bool comp = compare(vect[j],vect[j+1]); // assigning the output of function to variable comp
            if ( comp == false)                // if comp is false then
            {
                temp=vect[j];               //asigining the value of vect at j location to temp variable
                vect[j]=vect[j+1];           //swapping the values
                vect[j+1]=temp;               //swapping the values
            }
         
         }
     }
        for (int i=0; i <= vector_size-1 ; i++)
        {
            cout << vect[i] << endl;       //Printing the output of sorted vector
        }
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For Cpp Write the definition of a function that takes as input the three numbers. The...
For Cpp Write the definition of a function that takes as input the three numbers. The function returns true if the first number to the power of the second number equals the third number; otherwise, it returns false. (Assume that the three numbers are of type double).
Write an LC-3 program (machine code) that compares the two numbers located in x3600 and x3601...
Write an LC-3 program (machine code) that compares the two numbers located in x3600 and x3601 and puts the value 0 in R1 if the two numbers are equal, 1 if the first number is larger than the second number, and -1 if the first number is smaller than the second number.
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least...
Write a largestBelowValue(numbers, value) function that returns the largest number in the list numbers that is...
Write a largestBelowValue(numbers, value) function that returns the largest number in the list numbers that is smaller than value. Assume the numbers are always positive integers. Some example test cases (include these test cases in your program): >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 40)) 31 >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 41)) 40 returns None since no value is smaller than 2 in the list >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 2)) None
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Here's my JavaScript question: What is the best way to write a comparator function in JavaScript...
Here's my JavaScript question: What is the best way to write a comparator function in JavaScript that takes 2 strings s1 and s2 and returns true if s1 is alphabetically before s2, and false otherwise? You cannot use the built in sort function, you must create your own sort function. Input strings s1 and s2 are not case sensitive and might not be equal length. Here's two examples: compare('aaa', 'aab') returns true, and compare('aaa', 'aaab') returns true..
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...