In this lab, you will familiarize yourself with structs through a small exercise
ID: 3750670 • Letter: I
Question
In this lab, you will familiarize yourself with structs through a small exercise. We will be mixing the RGB values of colors together to make a new one.
RGB stands for red, green and blue. Each element describes the intensity value ranging from 0 - 255. For example: black color will have RGB values (0, 0, 0) while white will have (255, 255, 255)
Create a dynamic array of structs color. The struct contains three integers named red, green and blue. This corresponds to the RGB values of a color. For each array element, ask the user to enter the intensity value of red, green and blue. The value should be between 0 and 255 (inclusive).
Additionally, compute the average of each of the red, green and blue components. For code modularity, implement a function that returns the average of each rgb component in your dynamic array. The function should take in a struct array, its length and the rgb type for which you want to compute the average. Print out the final result in the form (r, g, b), where r, g, b corresponds to each averaged value.
Can you guess what color you mixed? (Note: Your program does not need to print the final color mixed)
Explanation / Answer
#include <iostream>
using namespace std;
struct color {
int r ,g ,b ;
};
int average(struct color c){
return (c.r+c.g+c.b)/3;
}
int main(){
int n;
cout<<"Enter length of array : ";
cin>>n;
struct color arr[n];
cout<<"Enter color component of each mixing";
for(int i=0;i<n;i++){
cout<<"Enter Red (0 - 255) : ";
cin>>arr[i].r;
cout<<"Enter Green (0 - 255) : ";
cin>>arr[i].g;
cout<<"Enter Blue (0 - 255) : ";
cin>>arr[i].b;
}
for(int i=0;i<n;i++){
cout<<"( "<<arr[i].r<<" , "<<arr[i].g<<" , "<<arr[i].b<<" ) = "<<average(arr[i]);
cout<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.