Question

In: Computer Science

How to do this program in C ? You have a sock drawer. You have an...

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.

Solutions

Expert Solution

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


Related Solutions

Socks in a drawer: In your sock drawer you have 4 blue, 5 gray, and 3...
Socks in a drawer: In your sock drawer you have 4 blue, 5 gray, and 3 black socks. Half asleep one morning you grab 2 socks at random and put them on. Find the probability you end up wearing (Round each answer to four decimal places if necessary.) 2 blue socks. no gray socks. at least 1 black sock. a green sock. matching socks.
Sock Problem: Your sock drawer is very unorganized! No socks are paired, and they are all...
Sock Problem: Your sock drawer is very unorganized! No socks are paired, and they are all just thrown randomly into the drawer. You do know that the drawer has four red socks and four blue socks in it. You want to get some socks to wear in the morning, but you do not want to turn on a light for fear of waking up your family. If you draw two, what is the probability of a red pair match? If...
Revisit Bryden’s sock drawer from earlier in the test (4 Cool socks, 6 Hunk socks, 2...
Revisit Bryden’s sock drawer from earlier in the test (4 Cool socks, 6 Hunk socks, 2 Genius socks). If Mr. Smith draws out two socks, one at a time for Bryden to wear, what is the probability that they do not match?  
You will develop a program in C++ that will do a "find and replace" on an...
You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name. It will prompt your user for four inputs: The name of the input file. The characters to find in the input file. The replacement (substitute) characters. The name of the output file. It will perform the replacement. Note1: all instances must be replaced not just the first one on a line. Note2:...
You will develop a program in C++ that will do a "find and replace" on an...
You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name. It will prompt your user for four inputs: The name of the input file. The characters to find in the input file. The replacement (substitute) characters. The name of the output file. It will perform the replacement. Note1: all instances must be replaced not just the first one on a line. Note2:...
You are to write a program in C to do the following in a loop for...
You are to write a program in C to do the following in a loop for the KL46Z . Prompt the user for a positive integer greater than 1 and sanity-check the input. If the number is a prime number, it is to be printed on a new line in red text. If the number is evenly divisible by 7, it is to be printed on a new line in green text. If the current number is evenly divisible by...
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
How does a file drawer analysis (see book for description of “file drawer”) make the findings...
How does a file drawer analysis (see book for description of “file drawer”) make the findings of a meta-analysis more persuasive?
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
How do I run a lex program written with C on xcode?
How do I run a lex program written with C on xcode?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT