Task
The task for this assignment is to have the following user-defined data type:
struct rgb
{
unsigned char red;
unsigned char green;
unsigned char blue;
};
be able to be:
The rgb type represents a colour (e.g., for a pixel) with the components red, green, and blue. By convention, these components are values between 0 and 255 inclusively --but such is not enforced by the rgb definition in this assignment.
NOTE: In this assignment nothing will be added, defined, declared, or removed from the rgb structure. Many often think writing a class requires writing constructors, etc. which can be a lot of (often unnecessary) code. This assignment will rely on C++'s default definitions for constructors, etc. Subsequent assignments will define constructors, etc.
As with everything in this course, you cannot use the C language and C Standard Library unless that feature has not suitable C++ implementation, e.g., printf() and scanf() are not allowed to be used in this assignment.
Required Header Files
The required header files for this assignment are:
#include <cmath> // for std::sqrt #include <array> // for std::array #include <vector> // for std::vector #include <limits> // for std::numeric_limits #include <string> // for std::string #include <istream> // for std::istream #include <ostream> // for std::ostream #include <iostream> // for std::cin, std::cout #include <algorithm> // for std::transform using namespace std; // place this after the #includes
Place these at the top of your program.
Defining The rgb Structure
Define the rgb structure mentioned at the top of this assignment next in your code, i.e.,
struct rgb
{
unsigned char red,
unsigned char green;
unsigned char blue;
};
Again, don't worry that there is (intentionally) nothing else declared or defined within this structure: C++ will automatically create default, copy, move, constructors, a destructor, copy and move assignment operators as is required.
Writing std::istream& operator >>(std::istream& is, rgb& colour)
Define the operator overload of >>, i.e., std::istream& operator >>(std::istream& is, rgb& colour), as follows:
Write std::ostream& operator <<(std::ostream& os, rgb const& colour)
Define the operator overload of <<, i.e., std::ostream& operator <<(std::ostream& os, rgb const& colour), as follows:
Testing Your Code So Far
You can now test your code, e.g., using a main() like this:
int main()
{
rgb value{}; // this declares an instance of the rgb structure
if (cin >> value)
cout << "\nRead successful.\nWriting: " << value << '\n';
}
You can delete this main() once your testing has been done. Sample input could be the numbers 5 34 130 --which should also be sucessfully output per the code example above.
Writing double distance(rgb const& a, rgb const& b)
This distance() function computes the distance between two colours, a and b. If you look at these two colours as a three-dimensional vector, computing the distance between the two colours is the same as computing the distance between two points:
<![CDATA[d=(ar−br)2+(ag−bg)2+(ab−bb)2−−−−−−−−−−−−−−−−−−−−−−−−−−−√]]><![CDATA[d=(ar−br)2+(ag−bg)2+(ab−bb)2]]>
It is easiest to compute the corresponding subtractions in three distinct variables first, then square those values, sum them, and finally compute the sqrt. You will need to use a floating-point type (e.g., double or float) for these values to properly compute the distance.
When done writing this function, test it to be sure it works properly.
Writing main()
In this assignment main() will be partially given to you and other parts of it you will have to write. Start by writing the following in main():
int main()
{
array<rgb,16> 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<string,16> const colour_names{
"black", "maroon", "green", "olive", "navy", "purple", "teal", "silver",
"gray", "red", "lime", "yellow", "blue", "fushsia", "aqua", "white"
};
Some notes about the above code fragment:
Earlier you wrote code to read in and write out rgb structure values. Start by writing a loop:
Within the loop, you will first want to:
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. This code is very similar to transform() uses presented in the lectures:
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<T>, i.e., the maximum value possible for the integer.
This declaration should be written like this:
size_t index = std::numeric_limits<size_t>::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<double>::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:
The last step is to output the name of the closest matching colour if it was found, i.e.,
cout << colour_names[index] << '\n';
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...\n";
return 1;
Finally close the loop's brace and write the closing brace of main().
Sample Program Run
Here is a sample completed program run:
$ ./a2.exe 0 0 0 black 0 128 0 green 255 255 0 yellow 240 240 240 white $
IMPORTANT READ*****CHEGG GUIDELINES - This explanation is not your answer. Anything you submit must be of your own work. We are here to provide explanation and directions ONLY. Hope you understand!
Please repost the question.
Task The task for this assignment is to have the following user-defined data type: struct rgb...
The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist Song_Title Year Sales Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...
C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...
Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...
Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...
Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...
This needs to be done in c++11 and be compatible with g++ compiling Project description: Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52 cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player...
How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...
== Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...
Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The Playlist class will contain a dynamic array of Song objects, and the Song class contains several pieces of information about a song. You will need to: 1) Finish the writing of two classes: Song and Playlist. The full header file for the Song class has been provided in a file called Song.h. 2) Write a main program (filename menu.cpp) that creates a single Playlist...
This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...