Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help coding this fragment in C++ for this question below, just need to add this into the code without screwing everything else up, currently

Need help coding this fragment in C++ for this question below, just need to add this into the code without screwing everything else up, currently the code works fine and outputs fine but I need to add this if statement in for invalid index, I tried but it kept giving me an error please help, I have highlighted where the code should be placed as well:

Question:

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;

Sample Run:

$ ./a2.exe 0 0 0 black 0 128 0  green 255 255 0 yellow 240 240 240 white $

/////////////////////////////////////////////////////////////////////Code Below//////////////////////////////////////////////////////////////////////////////////

#include // for std::sqrt #include // for std::array #include // for std::vector #include // for std::numeric_limits #include // for std::string #include // for std::istream #include // for std::ostream #include // for std::cin, std::cout #include // for std::transform

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(colour.red) << " " << static_cast(colour.green) << " " << static_cast(colour.blue); return os; } double distance(rgb const& a, rgb const& b) { double A = 0.0; double x =(a.red - b.red); double y =(a.green - b.green); double z =(a.blue - b.blue); A = x*x + y*y +z*z; A = sqrt(A); return A; } int main() { array const colours{{ { 0x00, 0x00, 0x00 }, // 0: black { 0x80, 0x00, 0x00 }, // 1: maroon { 0x00, 0x80, 0x00 }, // 2: green { 0x80, 0x80, 0x00 }, // 3: olive { 0x00, 0x00, 0x80 }, // 4: navy { 0x80, 0x00, 0x80 }, // 5: purple { 0x00, 0x80, 0x80 }, // 6: teal { 0xC0, 0xC0, 0xC0 }, // 7: silver { 0x80, 0x80, 0x80 }, // 8: grey { 0xFF, 0x00, 0x00 }, // 9: red { 0x00, 0xFF, 0x00 }, // 10: lime { 0xFF, 0xFF, 0x00 }, // 11: yellow { 0x00, 0x00, 0xFF }, // 12: blue { 0xFF, 0x00, 0xFF }, // 13: fushsia { 0x00, 0xFF, 0xFF }, // 14: aqua { 0xFF, 0xFF, 0xFF } // 15: white }};

array const colour_names{ "black", "maroon", "green", "olive", "navy", "purple", "teal", "silver", "gray", "red", "lime", "yellow", "blue", "fushsia", "aqua", "white" };

for(rgb value{}; cin >> value;) { vector distances; distances.reserve(colours.size()); transform(

begin(colours), end(colours), back_inserter(distances), [&value](auto const& colour) { return distance(colour, value); } ); size_t index = std::numeric_limits::max(); double smallest_distance = std::numeric_limits::max(); for(auto i=begin(distances), iEnd=end(distances); i != iEnd; ++i) { if (*i < smallest_distance) { smallest_distance = *i; index = distance(distances.begin(), i); } } cout << colour_names[index] << ' '; //CODE HERE } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions