Question
Just need help coding a fragment for this question in C++ below, I have highlighted the part that needs coding/fixing: Question: You can compute the
Just need help coding a fragment for this question in C++ below, I have highlighted the part that needs coding/fixing:
Question:
You can compute the current index from i by subtracting i from begin(distances), i.e., index = i - begin(distances);.
The last step is to output the name of the closest matching colour if it was found, i.e.,
cout << colour_names[index] << ' ';
But it is possible that index is invalid, so remember to write an if statement to check whether or not index is invalid first. If index is invalid, then do the following:
cout << "ERROR occurred. Aborting... ";
return 1;
Finally close the loop's brace and write the closing brace of main().
//////////////////////////////////////////////////////////////////////////Code Below/////////////////////////////////////////////////////////////////
#include
using namespace std; // place this after the #includes
struct rgb { unsigned char red; unsigned char green; unsigned char blue; };
istream& operator >>(istream& is, rgb& colour) { unsigned r, g, b; is >> r >> g >> b; if(r <= 255 && g <= 255 && b <= 255) { colour.red = r; colour.green = g; colour.blue = b; } return is; }
ostream& operator <<(ostream& os, rgb const& colour) { os << static_cast
array
for(rgb value{}; cin >> value;) { vector
begin(colours), end(colours), back_inserter(distances), [&value](auto const& colour) { return distance(colour, value); } ); size_t index = std::numeric_limits
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started