Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing a code fragment in C++ for the question below: The distances vector is currently empty (i.e., distance.size() == 0). To populate the

Need help writing a code fragment in C++ for the question below:

The distances vector is currently empty (i.e., distance.size() == 0). To populate the vector with distance values, you can use the std::transform algorithm to iterate through all rgb values in the colours array, appending the computed distance values to the distances vector by calling a (lambda) function.

  • Start by writing the call to transform, i.e., write transform(.
  • The first two arguments of transform() are the half-open input range for the colours array.
    • e.g., the first argument is the "beginning" of colours, the second argument is the "end" of colours (i.e., one-past-the-end).
  • The third argument to transform() must be an output iterator. Since you want to append elements and such is done using vector's push_back() function since a vector is a sequence container, this argument must be back_inserter(distances), i.e., note distances is the distances vector.
  • The fourth argument to transform() is the function that transforms an input element (from colours, i.e., an rgb value) to an output element (i.e., a double value that will be written to distances). The easiest way to do this is to write a lambda function that accepts an input value (i.e., an rgb value) and returns the compute output value, i.e., write this:
    • [&value](auto const& colour) { return distance(colour, value); }
    • where &value "captures" (by reference) the rgb value variable declared in the for loop above
    • NOTE: A lambda function cannot use local variables unless they are captured.

Finally you will want to determine the smallest distance between the rgb value read in to the variable value and the computed distances in the distances vector. Start by declaring a std::size_t (which is an unsigned int) integer to store the index of the colour with the smallest distance. This value should be invalid to start. An invalid-in-this-program value can be obtained from std::numeric_limits, i.e., the maximum value possible for the integer.

  • ASIDE: This is an "excuse" to make you aware of std::numeric_limits. Normally one would initialize index to colours.size() --which would be an invalid index.

This declaration should be written like this:

size_t index = std::numeric_limits::max(); 

Similarly, a variable is needed to store the current smallest distance value (which has to be found by iterating through the distances array). Start by setting the smallest distance value to the largest value that can be stored, e.g., assuming you are using double values to store your distances, this declaration would be:

double smallest_distance = std::numeric_limits::max(); 

Now, 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().

//////////////////////////////////////////////////////////////////////////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; } 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; distance.reserve(colours.size()); //code goes here }

}

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

Step: 3

blur-text-image

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions

Question

1. What is game theory?

Answered: 1 week ago