Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the Star structure defined in file p1 .cpp, write the function named findS

ID: 3932348 • Letter: U

Question

Using the Star structure defined in file p1 .cpp, write the function named findStars(). The function takes two input parameters: a vector of Stars and a string representing a star name or part of a star name. The function returns a vector of int representing the Draper numbers of the stars where the name was found. Use string:: find() to search the star names. Remember that string::find() return string::npos when the search parameter is not found. vector numbers; numbers = findStars(vStars, "VEG");

Explanation / Answer

PROGRAM CODE:

//Modify the program ro suit your result.

#include <iostream>

#include <vector>

using namespace std;

//assuming this to be the structure for stars

struct Stars

{

   string name;

   int draperNum;

};

//function to find the star names in the vector

vector<int> findStars(vector<Stars> vStars, string part_star_name)

{

   vector<int> numbers;

   //iterating through the vector

   vector<Stars>::iterator itr = vStars.begin();

   while(itr != vStars.end())

   {

       Stars star = *(itr);

       string starname = star.name;

       //using find to look for the part of the string

       int found = starname.find(part_star_name);

       if(found != std::string::npos)

       {

           numbers.push_back(star.draperNum); // if found, then the corresponding draper number is pushed into the return vector

       }

       itr++;

   }

   return numbers;

}

int main()

{

   Stars star = {"Sirius", 134};

   Stars star2 = {"vega", 883};

   vector<Stars> vStars;

   vStars.push_back(star);

   vStars.push_back(star2);

   vector<int> numbers = findStars(vStars, "veg");

   //iterating to see the result

   vector<int>::iterator itr = numbers.begin();

   while(itr != numbers.end())

   {

       cout<<*itr;

       itr++;

   }

   return 0;

}

OUTPUT:

883

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote