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
  • 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...

  • 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...

  • 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...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 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 I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • 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.

  • Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...

    Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have a default constructor that initializes the height to 10 and the radius to 2. It should have a method named surfaceArea that returns the the surface area (in square inches) of a cylinder with that...

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
ADVERTISEMENT