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

This assignment focuses on creating and manipulating selection control structure

ID: 3914918 • Letter: T

Question

This assignment focuses on creating and manipulating selection control structures.

The term combination in elementary algebra refers to one of the different ways a certain number of items can be selected from a list of items. For example, the combinations of four items a,b,c,d taken three at a time are abc, abd, acd, and bcd.

In other words, there are a total of four different combinations of four things "taken three at a time". In general, the number of combinations of n things taken k at a time is combinations = n! / ((n-k)!*k!), where the "!" is the symbol for factorial. The value of n! is n*(n-1)*(n-2)*...*1. For example, 4! = 4*3*2*1 = 24.

• Factorial explained

• Combinations explained

• Calculate factorial using C programming 1 and 2

• do while and while loop

• check that the user enters an integer between 0 and 10 only

The assignment is to create a program to display the number of combinations where n and k are entered by the user.

STEPS:

1) Input 1.

Ask for n. Check for bounds (1 <= n && n <= 10). i) If the user entered an integer within (1 and 10), move on to getting the value for k ii) Else keep asking the user till they give a correct value which is an integer within 1 and 10. iii) Reject any input greater than 10 or less than 1 by displaying an error message and then asking the user to re-enter the input. Do this as many times that they enter incorrect input.

2) Input 2.

Ask for k. Check for bounds (1 <= k && k <= n). i. If the user entered an integer within (1 and k), move on to next step. ii. Else keep asking the user till they give a correct value which is an integer within 1 and n. iii. Reject any input greater than n or less than 1 by displaying an error message and then asking the user to re-enter the input. Do this as many times that they enter incorrect input.

3) Calculate n – k.

4) Calculate factorial of n, k and n – k.

5) Calculate the Combination by plugging in factorial of n / (factorial of k * factorial of n - k)

6) Output

Print 5, the number of combinations of n things taken k at a time. This is the output of your program.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<stdio.h>

//method to find factorial of a positive number recursively

int factorial(int num){

                if(num<=0){

                                //base case 1, returning 0 for any number less than or equal to 0

                                return 0;

                }

                if(num==1){

                                //base case 2

                                return 1;

                }

                return num*factorial(num-1);

}

int main(){

                int n=0, k=0;

                //looping until user enters a valid value for n

                do{

                                printf("Enter the value for n (1 - 10): ");

                                scanf("%d",&n);

                                if(n<1 || n>10){

                                                printf("Invalid value,try again ");

                                }

                }while(n<1 || n>10);

               

                //looping until user enters a valid value for k

               

                do{

                                printf("Enter the value for k (1 - %d): ",n);

                                scanf("%d",&k);

                                if(k<1 || k>n){

                                                printf("Invalid value,try again ");

                                }

                }while(k<1 || k>n);

               

                int n_k=n-k; //value of n-k

                //finding factorials of n, k and n-k

                int n_fact=factorial(n);

                int k_fact=factorial(k);

                int n_k_fact=factorial(n_k);

                //finding number of combinations and displaying it

                int combinations=n_fact/(n_k_fact*k_fact);

                printf("Number of combinations of %d things taken %d at a time: %d",

                                n,k,combinations);

}

/*OUTPUT*/

Enter the value for n (1 - 10): 11

Invalid value,try again

Enter the value for n (1 - 10): -5

Invalid value,try again

Enter the value for n (1 - 10): 5

Enter the value for k (1 - 5): 6

Invalid value,try again

Enter the value for k (1 - 5): 4

Number of combinations of 5 things taken 4 at a time: 5

/*ANOTHER OUTPUT*/

Enter the value for n (1 - 10): 7

Enter the value for k (1 - 7): 2

Number of combinations of 7 things taken 2 at a time: 21