# Write a C function with the following prototype: void fillColor(struct Color *p

Posted: January 26th, 2023

Place your order now for a similar assignment and have exceptional work written by our team of experts, At affordable rates

Write a C function with the following prototype:
void fillColor(struct Color *p, struct Data d, char type)
This function assigns values to the Color structure ‘p’s members
based on the value in the char variable ‘type’ as follows:
char
type: assigned struct Color *p values:
‘R’ sets p’s name to “Red” and copies d’s struct members ‘r’, ‘g’, and ‘b’ to
p’s ‘red’, ‘green’, and ‘blue’ members respectively.
‘G’ sets p’s name to “Red” and copies d’s struct members ‘r’, ‘g’, and ‘b’ to
p’s ‘red’, ‘green’, and ‘blue’ members respectively.
‘B’ sets p’s name to “Red” and copies d’s struct members ‘r’, ‘g’, and ‘b’ to
p’s ‘red’, ‘green’, and ‘blue’ members respectively.
Q2. Write a C function with the following prototype:
void displayHexCodeAsString(const struct Color *s, int n)
This function accepts ‘n’ Color structures and displays the structure’s ‘name’ and the value
in each of the Color’s integer members (red, green, and blue) as a string in hexadecimal
notation as follows (see below) followed by a newline:
Color:color R:hexString G:hexString B:hexString
You may assume that no integer value will exceed 2147483647 or 7FFFFFFF in Hexadecimal.
Recall, the hexadecimal number system is base16 and contains 16 digits (0 to 9)
and (A to F; where A=10, B=11, C=12, D=13, E=14, and F=15).
HEXADECIMAL 0 1 2 3 4 5 6 7 8 9 A B C D E F
DECIMAL 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Steps:
1. Divide the decimal number by 16 and treat the division as an integer division
(i.e. the whole number portion only).
2. Compute the remainder by using the modulus operator.
3. Divide the result again by 16 and treat the division as an integer division.
4. Repeat step 2 and 3 until result is 0.
5. The hex value is the digit sequence of the remainders from last to first.
Note: The remainder here refers to the left over value after performing
the integer division. (see example below).
Example: Convert integer 177 to hexadecimal
Solution: Divide 177 by 16 until the quotient is zero.
177/16 = 11, with a remainder of 1
11/16 = 0, with a remainder of 11 (11 is B in hexadecimal)
The remainders from bottom to top would be B1
(so 177 in decimal is B1 in hexadecimal) and the function would display
the output as a string: “B1″ using %s with printf( ).
HINT: Consider using the following char array in your solution:
char hex[17]=”0123456789ABCDEF”;
Given that the position of each char digit in the array parallels its actual
integer value!
/ Your solution may ONLY use functions from the following
// included C library header files.
#include
#include
#define NUM 3
struct Color {
char name[12];
int red, green, blue;
};
struct Data {
int r, g, b;
};
// function prototypes:
void fillColor(struct Color *p, struct Data d, char type);
void displayHexCodeAsString(const struct Color *s, int n);
int main( ) {
char types[NUM] = { ‘G’, ‘B’, ‘R’ };
// hex strings 4000 10 B1 7FFFFFFF 24E 0 BC 100 ABCD
int values[NUM*3] = { 16384, 16, 177, 2147483647, 590, 0, 188, 256, 43981 }, i;
struct Color myColors[NUM];
struct Data myData[NUM];
for(i=0; i<NUM; i++) {
myData[NUM].r = values[i*NUM];
myData[NUM].g = values[i*NUM+1];
myData[NUM].b = values[i*NUM+2];
}
for(i=0; i<NUM; i++)
fillColors(&myColors[i], myData[i], types[i]);
displayHexCodeAsString(myColors, NUM);
return 0;
}
/* ========== CODE TO PLACE IN YOUR SOURCE FILE FOR SUBMISSION STARTS BELOW ========== */
// You may add your own helper functions or symbolic constants here.
// Functions that you add to this code MUST also be prototyped here.
// Correct Program OUTPUT should be:
/*
Color:Green R:4000 G:10 B:B1
Color:Blue R:7FFFFFFF G:24E B:0
Color:Red R:BC G:100 B:ABCD
*/

### Expert paper writers are just a few clicks away

Place an order in 3 easy steps. Takes less than 5 mins.

## Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
\$0.00