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

Hi!, I really need help with this assignment. Objectives Write a basic C program

ID: 3903802 • Letter: H

Question

Hi!, I really need help with this assignment.

Objectives

Write a basic C program with command line inputs

Check for input

Write recursive functions

Write a source (.c) and header (.h) file

Call functions with parameters and return values

Hand-in Requirements

Save your source files in a folder named lastname-firstname-assignment1-4 replacing lastname with your last name and firstname with your first name (all lower-cased). You will have the following code files in your folder:

calculate.c

calculate.h

To submit, zip the file and send the zip file with the same naming convention (all lower-cased): lastname-firstname-assignment1-4.tar.gz

Background Information

Your program will calculate the factorial, permutations, binomial coefficient/combination, and a fourth mathematical function/equation of your choice.

Factorial

A factorial of a number is the product of all the descending numbers below it until 1. Example: The factorial of 4 is 24: 4 * 3 * 2 * 1 = 24.

The notation for a factorial is the exclamation mark: n! = n * (n-1) * (n-2) * ... * 1. 0! is 1.

Permutations

A permutation represents the following: given n items, how many groups of k items can you make where ordering matters. Example, there are three items A, B, C; how many groups of 2 items are there? There are six (6): AB, BA, AC, CA, BC, CB. In permutations, although AB and BA are the same two items, the ordering matters so they are counted as different groupings.

The formula for calculating the permutation values is:

[permutation formula]

Source: https://en.wikipedia.org/wiki/Permutation

This formula assumes that n > k > 0.

Binomial Coefficient/Combinations

The binomial coefficient represents the following: given n items, how many groups of k items can you make without any repeats. Example: there are three items A, B, C; how many groups of 2 items are there? There are three (3): AB, AC, BC. In combinations, ordering of the items do not matter as opposed to permutations.

The formula for calculating the binomial coefficient is:

[combination formula]

Image source: https://en.wikipedia.org/wiki/Binomial_coefficient

This formula assumes that n >= k >= 0. For the rest of this assignment, we will refer to the binomial coefficient as combination.

Fourth Mathematical Function

That fourth mathematical function should require one or more arguments (we will limit them to integer arguments, for simplicity) and some calculations that utilizes arithmetic expressions in the function body. Some examples: quadratic equation, Pythagorean theorem. Whatever you choose, describe the function and the arguments and their order in the file comments in C so that the TA know how to use them.

Process

At the command line, the user will execute the program and pass in the combinatoric function they want followed by the numerical arguments separated by space.

For factorial, it will look like this: ./calculate factorial <integer n>

For combination, it will look like this: ./calculate combination <integer n> <integer k>

For permutation, it will look like this: ./calculate permutation <integer n> <integer k>

The fourth mathematical function you choose will follow the same format.

After the user hits enter, the program will calculate the appropriate result and print out the following:

Result: 720

Assignment 1-4 by Firstname Lastname

Tasks

The program you will write for this assignment has two files: a C source file (.c) and a header file (.h). In the C source file,

Write a main function that processes command line arguments. In our base program, use the arguments argc and argv.

Write a function to compute the factorial of a number recursively.

Write a function to compute the combination given n and k using the recursive factorial function you wrote.

Write a function to compute the permutation give n and k using the recursive factorial function you wrote.

Write a function to compute the fourth mathematical function you have chosen. Be sure to add documentation about this function in the file comments.

All values are integers and should be declared as unsigned int's since we are only working with positive numbers. For this program, you will not have any scanf's; rather, all input comes from the command line.

Input Validation

For this assignment, you will need to validate the arguments:

If the first argument is not one of the listed (factorial, combination, permutation), then print out an appropriate error message and stop the program. Spelling counts as well as case-sensitivity.

If any of the numerical arguments are greater than 15 or less than 0, stop the program. You can use an assert statement for this check. Otherwise, print out an appropriate error message and stop the program.

If there are not enough numerical arguments for the given math functions, then print out an appropriate error message and stop the program.

If a numerical argument is not numerical, then print out an appropriate error message and stop the program. Note that atoi() will convert a string to an integer. However, if the string has no numerical values,atoi() will return a 0. You will need to find out if the original string was a "0" or not. If it was not, then print out the error message and stop the program. Otherwise, continue with 0 as the argument.

Explanation / Answer

Compile this code "gcc filename.c -lm." Please read code carefully. It may not 100% perfect but it is definately 99%.

Please do not downvote without asking queries in comments

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include<math.h>

unsigned int factorial(unsigned int n)
{
if (n == 0 || n == 1)
{
return 1;
}

return n * factorial(n - 1);
}

unsigned int permutation(unsigned int n, unsigned int k)
{
unsigned int numerator = factorial(n);
unsigned int denom = factorial(n-k);

return numerator/denom;
}

unsigned int combination(unsigned int n, unsigned int k)
{
unsigned int numerator = factorial(n);
unsigned int denom = factorial(k) * factorial(n-k);

return numerator/denom;
}
// This function implements pythagoras theorem
// pass sides of trianges, you get third side of type float.

float pythagoras(unsigned int a, unsigned int b)
{
unsigned int a_sq = a*a;
unsigned int b_sq = b*b;
return sqrt(a_sq + b_sq);
}

int main(int argc, char* argv[])
{

if (!strcmp(argv[1], "permutation") || !strcmp(argv[1], "combination")) {
if (atoi(argv[2]) < 0 && atoi(argv[2]) > 15) {
printf("incorrect number ");
exit(0);
}
if (atoi(argv[3]) < 0 && atoi(argv[3]) > 15) {
printf("incorrect number ");
exit(0);
}
if (argc != 4) {
printf("incorrect number of args ");
exit(1);
}

if (!strcmp(argv[1], "permutation")) {
printf("%d", permutation(atoi(argv[2]), atoi(argv[3])));
} else {
printf("%d", combination(atoi(argv[2]), atoi(argv[3])));
}

} else if (!strcmp(argv[1], "factorial")) {
if (atoi(argv[2]) < 0 || atoi(argv[2]) > 15) {
printf("incorrect number ");
exit(0);
}
if (argc != 3) {
printf("incorrect number of args ");
}
printf("%d", factorial(atoi(argv[2])));
}

return 0;
}

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