Question

Write a function named squareAndSort(). This function should take a reference to a vector of <double>...

Write a function named squareAndSort(). This function should take a reference to a vector of <double> values. The function should square each of the values in the vector and then sort the vector in ascending order. Since the vector is passed by reference, this function does not need to return anything, so it will be a void function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>

using namespace std;

void squareAndSort(vector<double> &v) {
    for (int i = 0; i < v.size(); ++i) {
        v[i] *= v[i];
    }
    double temp;
    for (int i = 0; i < v.size(); ++i) {
        for (int j = 0; j < v.size()-1; ++j) {
            if (v[j] > v[j+1]) {
                temp = v[j];
                v[j] = v[j+1];
                v[j+1] = temp;
            }
        }
    }
}

int main() {
    vector<double> v = {2, 3, 9, 1, 6};
    squareAndSort(v);
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a function named squareAndSort(). This function should take a reference to a vector of <double>...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #C++ Write a function, named sortByCourseNumber, that takes a reference to a vector of string (like...

    #C++ Write a function, named sortByCourseNumber, that takes a reference to a vector of string (like {"CSE 232", "ECE 100", "CSE 450", "CSE 232"}) and returns nothing. The function should reorder the vector so that the courses are sorted by number in ascending order. You can assume that the department code is separated from the number by a space and that all course numbers are 3 characters long. You should be using the STL algorithms to achieve this, full credit...

  • Use C Create a function that will take in a vector (three double variables) representing a...

    Use C Create a function that will take in a vector (three double variables) representing a position in meters. Calculate the magnitude of the vector in meters: sqrt(x * x + y * y + z * z) Calculate the magnitude of the vector in feet: magnitudeInMeters * 3.28084 Using passing by reference, return both outputs from the same function. Input: Three unique doubles, each one representing a component of the vector. Output: Magnitude of the vector in meters, magnitude...

  • Written in C++ (On CodeStepbyStep) Write a function named mean that accepts as a parameter a...

    Written in C++ (On CodeStepbyStep) Write a function named mean that accepts as a parameter a reference to a Vector of real numbers, and returns the arithmetic mean (average) of the integers in the vector as a real number. For example, if the vector passed contains {2.0, 4.5, 6.5, 1.0}, your function should return 3.5. If the vector is empty, return 0.0. Do not modify the vector that is passed in.

  • Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x...

    Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x is a vector containing numbers. and Type can be 'Ascending' or Descending'. This function should sort the given vector x in the ascending or descending order based on the user's request and return the desired vector as the output. Do not use MATLAB built-in function sort. Hint: When you call this function you need to use for Ascending or Descending since they are strings.

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • 1. Write a virtual member function named length , which specifies that the function is constant...

    1. Write a virtual member function named length , which specifies that the function is constant (that is does not modify the member data) and returns the square root of x squared plus y squared. 2.write the operator << function to have a member of the ostream class as its left hand operand and a member of the Two Dimensions class as its right hand operand. This function shall take the left hand operand by reference, This function shall return...

  • can anyone help me with this 6 function as c++ language. It is not required to return in main. In addition to these...

    can anyone help me with this 6 function as c++ language. It is not required to return in main. In addition to these 10 functions, you'll have 6 more functions void incDouble(double al], unsigned els); incDouble's job is to sort the array so that the values are in increasing order void decDouble(double al], unsigned els); decDouble's job is to sort the array so that the values are in decreasing order void inc Vec(vector<double> & v); incVec's job is to sort...

  • can anyone help me with this 6 function as c++ language. It is not required to...

    can anyone help me with this 6 function as c++ language. It is not required to return in main. In addition to these 10 functions, you'll have 6 more functions void incDouble(double al], unsigned els); incDouble's job is to sort the array so that the values are in increasing order void decDouble(double al], unsigned els); decDouble's job is to sort the array so that the values are in decreasing order void inc Vec(vector<double> & v); incVec's job is to sort...

  • Write a function curve that accepts a vector v of double and returns vector after processing...

    Write a function curve that accepts a vector v of double and returns vector after processing it. First the function calculates the average of the vector and the function should then process the vector by adding the average to each element of the vector and then dividing by 2 the function should then return processed vector.

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT