Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 declare a struct that can be used for holding the colors of a rectangl

ID: 3577517 • Letter: P

Question

Problem 1 declare a struct that can be used for holding the colors of a rectangle. Colors are composed of a Red, Green, Blue component (integer values between 0 and 255) (**)

..........

..........

..........

Problem 2 declare an array that can be used to hold the colors of a rectangle using the struct defined above (**)

..........

..........

..........

Problem 3 write a complete function (not main) that calculates the average of the red components only in an array of struct defined above. (**)

..........

..........

..........

Explanation / Answer

1. declare a struct that can be used for holding the colors of a rectangle. Colors are composed of a Red, Green, Blue component (integer values between 0 and 255)

struct colors_rectangle{
unsigned char red, green, blue;
};

This is the way of declaring a struct having three elements inside red, green and blue. This elements are unsigned char, instead of int because it is mentioned specifically that the values will range from 0 to 255. The range of unsigned char is from 0 to 255. We could use int also, but that would be inefficient as int is normally 4 bytes, but unsigned char is 1 byte only. It is a huge difference when the datas become enormous.

struct is the keyword signifying it is a structure declaration and colors_rectangle is the name given to this type of structure.

2. declare an array that can be used to hold the colors of a rectangle using the struct defined

For declaration this can be used:

struct colors_rectangle rect_arr[4];

This defines a structure array variable rect_arr of the type of structure defined by struct colors_rectangle. I have given the array length as 4 here. This array is similar to other integer or char array, except that here each element in the array is not just a single entity but a whole structure. So its like a series of structures.

For initialization alongwith declaration this can be used:

struct colors_rectangle rect_arr[4] = {{2,2,2},{3,3,3},{4,4,4},{5,5,5}};

Here each structure is initialized using a pair of curly braces, and the whole initialization enclosed by a pair of curly braces.

3. write a complete function (not main) that calculates the average of the red components only in an array of struct defined above.

double red_average(struct colors_rectangle r_arr[],int count){
int i;
double sum = 0,avg;
for(i=0;i<count;i++)
sum = sum + r_arr[i].red;
avg = sum/count;
return avg;
}

Here red_average is the function name and it returns a double. It takes two parameters - a structure array and an integer denoting the array length. The structure element of array are accessed using the dot operator inside the function, and each structure of the array is looped through by the for loop. We use the sum and avg variable to store the total sum and average, and at last returns the avg.

A simple program to exemplify these :

#include<stdio.h>

struct colors_rectangle{
unsigned char red, green, blue;
};

double red_average(struct colors_rectangle r_arr[],int count);

int main()
{
struct colors_rectangle rect_arr[4] = {{2,2,2},{3,3,3},{4,4,4},{5,5,5}};
double r_avg = red_average(rect_arr,4);
printf(" Red Average : %f",r_avg);
return 0;
}

double red_average(struct colors_rectangle r_arr[],int count){
int i;
double sum = 0,avg;
for(i=0;i<count;i++)
sum = sum + r_arr[i].red;
avg = sum/count;
return avg;
}

Output:

Red Average : 3.500000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote