Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing this code fragment in C++ for question below: Now, starting with the input range [begin(distances), end(distances), write a for loop that iterates

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

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

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(); //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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

The Nature of Nonverbal Communication

Answered: 1 week ago

Question

Functions of Nonverbal Communication

Answered: 1 week ago

Question

Nonverbal Communication Codes

Answered: 1 week ago