Using the Star structure defined in file p1.cpp, write the function named magnit
ID: 3604972 • Letter: U
Question
Using the Star structure defined in file p1.cpp, write the function named magnitudeBetween(). The function takes three input parameters: a vector of Stars as well as a lower and upper bounds. The function returns a vector of size_t containing indexes of the stars whose magnitude falls between the two bounds (exclusive). Please explain your steps if possible . Thanks.
p1.cpp:
*/
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <cmath> // already included for you
using namespace std;
// The structure (don't change these)
struct Star {
double x, y;
double magnitude;
int draperNumber;
string names;
};
Explanation / Answer
Please finf the function definition of magnitideBetween() below:
//Definition of function magnitugeBetween
vector<size_t> magnitudeBetween(vector<Star> vStars,double upper,double lower)
{
//Declare a vector indexes
vector<size_t> indexes;
//Loop through all the elements in the vStars vector
for (int i=0;i< vStars.size(); i++)
{
//Check if magnitude is between upper & lower bound, if true add the index of the star to the vector indexes
if (vStars[i].magnitude > lower && vStars[i].magnitude < upper )
indexes.push_back(i);
}
//Return the vector indexes
return indexes;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.