Question: I need help finishing this C++ code and getting it to produce the following sample outputs: $ ./assignment.exe 0 0 0 black 0 128 0
I need help finishing this C++ code and getting it to produce the following sample outputs:
$ ./assignment.exe 0 0 0 black 0 128 0 green 255 255 0 yellow 240 240 240 white $
The C++ code:
#include
using namespace std;
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
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
array
for(rgb value{}; cin >> value;) { vector
/**REST OF CODE GOES HERE
Following these instructions:
Starting with the input range [begin(distances), end(distances), write a for loop that iterates through all elements (explicitly) updating index and smallest_distance when a distance is found that is smaller than the smallest_distance variable's value. Some help for this:
- You can write the initialization condition of the for loop as: auto i=begin(distances), iEnd=end(distances); This is not unlike writing for (int i=1, j=2; etc.).
- The for loop will only execute while i and iEnd are not equal.
- The increment portion is ++i;
- To access the distance value pointed to by i write *i (remember iterators are based on pointer syntax).
- You can compute the current index from i by subtracting i from begin(distances), i.e., index = i - begin(distances);. This works because i is an random-access iterator (as are all iterators for vectors, arrays, and strings.)
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().
**/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
