In: Computer Science
How to do this program in C ? You have a sock drawer. You have an infinite supply of red, green, yellow, orange, and blue socks. 1) Choose how many socks of each color you will put into the drawer. 0 is an ok number, but it would be nonsense to allow the user to put a negative number of socks into the drawer, so if the user tries to do that, print an error message and quit the program. 2) Ask the user to specify two colors of socks typing the first letters of the two colors in response to a prompt. Sample dialog: What two colors of socks are you interested in:YO It is an error to specify the same color twice. Your response of YO would mean that you are interested in yellow and orange socks. RR would be an improper response and should recieve an error message. 3) Pretend that the user shuts their eyes, reaches into the drawer, and retrieves one sock. Calculate the probability that the sock is one of the two specified colors. If c1 and c2 are the specified colors, and nk is the number of socks of color k, then the required probability would be (n1 + n2) / (n1 + n2 + n3 + n4 + n5) For instance, suppose there are 5 red socks, 4 green socks, 3 yellow socks, 2 ornage socks and 1 blue sock. If the user specified red and yellow (RY) the probability would be (5 + 3) / (5 + 4 + 3 + 2 + 1) Use ints to hold the number of socks of each color, but calculate the probabilities as doubles.
Answer: hey! Kindly find a solution to your problem. This program first asks for the number of socks of each color that you want to put in the socks drawer. It will prompt for each color. Then it will ask the colors name of socks in which you are interested in. It will take input first letters of colors of two socks. If you entered the same colors both, it will print an error message and exit from program. If both are different colors, then it will check combination(all combinations are defined in program) if anyone true, it will calculate probability and print.
If you have any query , feel free to ask me. Program is working well and giving desired result. Output you can see. Thanks.
#include <stdio.h> //include header files
#include <stdlib.h>
int main() //main function
{
int red,green, yellow,orange,blue,sum; //variable
declaration integer type
char c1[3]; //character type array
double probabi; //double for probability
printf("Red,green,Yellow,Orange,Blue \n");
printf("How many socks of each color you will put into the
drawer-\n");
//prompt for number of socks of each color put in drawer
printf("Red:");
scanf("%d",&red);
if(red<0) //check if number of socks in negative
number
{
printf("Invalid number"); //then
print error message
exit(0); // and exit
}
printf("\nGreen:");
scanf("%d",&green);
if(green<0) //check if number of socks in negative
number
{
printf("Invalid number");
//then print error message
exit(0); // and
exit
}
printf("\nYellow:");
scanf("%d",&yellow);
if(yellow<0) //check if number of
socks in negative number
{
printf("Invalid number");
//then print error message
exit(0); // and
exit
}
printf("\nOrange:");
scanf("%d",&orange);
if(orange<0) //check if
number of socks in negative number
{
printf("Invalid number");
//then print error message
exit(0); // and exit
}
printf("\nBlue:");
scanf("%d",&blue);
if(blue<0)
{
printf("Invalid number");
exit(0);
}
//prompt for colors of socks from user use first letter of
colors
printf("What two colors of socks are you interested in \n");
scanf("%s",&c1);
sum = red+green+yellow+orange+blue; //sum for all
number will be easy to calculation
if(c1[0]==c1[1]) //check colors letter if same
then
{
printf("you can not choose same
color twice \n"); //print error message
exit(0); //exit from program
}
else //if not same then check all
combinations of socks if anyone is true execute that code print
appropriate values
if(c1[0]=='R' && c1[1]=='G')
{
probabi = red+green; //to calculate
probability used formula as you given in question
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='R' &&
c1[1]=='Y')
{
probabi = red+yellow;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='R' &&
c1[1]=='O')
{
probabi = red+orange;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='R' && c1[1]=='B')
{
probabi = red+blue;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='G' &&
c1[1]=='R')
{
probabi = red+green;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='G' &&
c1[1]=='Y')
{
probabi = red+yellow;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='G' &&
c1[1]=='O')
{
probabi = red+orange;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='G' &&
c1[1]=='B')
{
probabi = red+blue;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='Y' &&
c1[1]=='R')
{
probabi = red+yellow;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='Y' && c1[1]=='G')
{
probabi = yellow+green;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='Y' && c1[1]=='O')
{
probabi = yellow+orange;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='Y' &&
c1[1]=='B')
{
probabi = yellow+blue;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='O' &&
c1[1]=='R')
{
probabi = orange+red;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='O' &&
c1[1]=='G')
{
probabi = orange+green;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='O' &&
c1[1]=='Y')
{
probabi = orange+yellow;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='O' &&
c1[1]=='B')
{
probabi = orange+blue;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='B' &&
c1[1]=='R')
{
probabi = blue+red;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='B' &&
c1[1]=='G')
{
probabi = blue+green;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='B' &&
c1[1]=='Y')
{
probabi = blue+yellow;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
else
if(c1[0]=='B' &&
c1[1]=='O')
{
probabi = blue+orange;
probabi = probabi/sum;
printf("Probability of socks are:
%lf",probabi);
}
return 0;
}
inpu Red, green, Yellow, Orange, Blue How many socks of each color you wil1 put into the drawer- Red:5 Green:4 Yellow:3 I Orange:2 Blue: 1 What two colors of socks are your interested in s RG Probability of socks are: 0.600000