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

Complete the color2.c program. Note that: –color has a member representing the n

ID: 3606521 • Letter: C

Question

Complete the color2.c program. Note that:

–color has a member representing the name. It’s assumed that name is no more than 25 characters.

–It can hold multiple colors in an array of color structures. It’s assumed that there are no more than 100 colors to be held.

Complete the functions

–insert: It allows the user to enter the name, the red, green, and blue member values and store them in the array of color structures. It will check whether the color being added has already existed.

–search: It should also allow user to look up a color’s RGB values by name.

color2.c program provided below:

#include "readline.h"
#include <stdio.h>
#include <string.h>
#define NAME_LEN 25
#define MAX_COLORS 100

struct color{
   char name[NAME_LEN+1];
   int red;
   int green;
   int blue;
};

struct color brighter(struct color c);
struct color make_color(int red, int green, int blue);
void print(struct color colors[], int nc);
void insert(struct color colors[], int *nc);
void search(struct color colors[], int nc);

int main()
{

char code;
struct color cols[MAX_COLORS];
int num_colors = 0;

for (;;) {
    printf("Enter operation code: ");
    scanf(" %c", &code);
    while (getchar() != ' ')   /* skips to end of line */
      ;
    switch (code) {
      case 'i': insert(cols, &num_colors);
                break;
      case 's': search(cols, num_colors);
                break;
      case 'p': print(cols, num_colors);
                break;
      case 'q': return 0;
      default: printf("Illegal code ");
    }
    printf(" ");
}

    return 0;
}

/*insert a color to the array */
void insert(struct color colors[], int *nc)
{
if (*nc == MAX_COLORS) {
    printf("Database is full; can't add more parts. ");
    return;
}

//add your code here

}
/*display the colors stored in the array*/
void print(struct color colors[], int nc)
{

int i;

printf("Color Red "
         "Green Blue ");
for (i = 0; i < nc; i++)
printf("%s %d %d %d ", colors[i].name, colors[i].red,
           colors[i].green, colors[i].blue);
}
/*search a color by name and display the RGC if found */
void search(struct color colors[], int nc)
{
//add your code here

}

Explanation / Answer

#include "readline.h"
#include <stdio.h>
#include <string.h>
#define NAME_LEN 25
#define MAX_COLORS 100
struct color{
char name[NAME_LEN+1];
int red;
int green;
int blue;
};
struct color brighter(struct color c);
struct color make_color(int red, int green, int blue);
void print(struct color colors[], int nc);
void insert(struct color colors[], int *nc);
void search(struct color colors[], int nc);
int main()
{
char code;
struct color cols[MAX_COLORS];
int num_colors = 0;
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'i': insert(cols, &num_colors);
break;
case 's': search(cols, num_colors);
break;
case 'p': print(cols, num_colors);
break;
case 'q': return 0;
default: printf("Illegal code ");
}
printf(" ");
}
return 0;
}

/*insert a color to the array */
void insert(struct color colors[], int *nc)
{
if (*nc == MAX_COLORS) {
printf("Database is full; can't add more parts. ");
return;
}
// nc acts as a indicator for size.If *nc is equal to MAX_COLORS ,it displays "
//Database is full"
//So to insert any color we have to check whether any size of struct colors is full or not
//To add any color, we have to increment the value of *nc


char tempColor[25]; // Declared this variable for temporary storage of color to be inserted

printf("Enter Color Name you want to ADD: ");
scanf("%25s",tempColor); //%25s ensures to read only 25 characters
for(int i=0;i<*nc;i++) //This for loop is used to check whether the color is existed or not
{
if(strcmp(tempColor,colors[i].name)==0) //For strings comparison,we used strcmp.If it is zero,both are equal.
{
printf("Color already Exist in Database...");
return; // If the color is already present ,it will return saying "Color already Exist in Database..."
}

}

*nc=*nc+1; //*nc is incremented because ,this is indicator of size of character array.So to insert any color,we have to increment the value of *nc
strcpy(colors[*nc-1].name,tempColor);


printf("Enter red value: "); //inserting the red value
scanf("%d",&colors[*nc-1].red);
  
printf("Enter green value: ");//Inserting the blue value
scanf("%d",&colors[*nc-1].green);
printf("Enter blue value: "); //Inserting the green value
scanf("%d",&colors[*nc-1].blue);


return;
}


/*display the colors stored in the array*/
void print(struct color colors[], int nc)
{
int i;
printf("Color Red "
"Green Blue ");
for (i = 0; i < nc; i++)
{
  
  
printf("%s %d %d %d ", colors[i].name, colors[i].red,
colors[i].green, colors[i].blue);
}
}


/*search a color by name and display the RGC if found */
void search(struct color colors[], int nc)
{
char tempColor[25]; //used to store the color name to be searched
  
  
printf("ENTER the color you want to search: ");
  
scanf("%25s",tempColor);
  
for(int i =0;i<nc;i++) //Used for loop to search the color
{
if(strcmp(tempColor,colors[i].name)==0)
{
  
printf("%s %d %d %d ", colors[i].name, colors[i].red,
colors[i].green, colors[i].blue);

return; //Used return statement to return whenever the color name is present in database
}
}
  
//if for loop got completed,it indicates the searched element is not present in database.
//If it is present,loop would have returned the value at the very next statement
printf("Color doesn't Exist");
return;

}