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: 3903023 • 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:

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:

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

//calculate.c file

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include"calculate.h"


void main(int argc,char *argv[])
{
int number,k,h,p,b;
if(argc==1)
{
number=atoi(argv[0]);
k=atoi(argv[1]);
if(number>0)
printf("Factorial is",factorial(number));
else
printf("No Number Negative number, Factorial is not possible");
}
if(argc==2)
{
number=atoi(argv[0]);
k=atoi(argv[1]);
if(k>0 && number>=k)
{
printf("Permutation is",permutation(number,k));
printf("Combination is",combination(number,k));
}
else
printf("No Number Negative number, Permutation/combination is not possible");
}

if(argc==3)
{
a=atoi(argv[0]);
b=atoi(argv[1]);
c=atoi(argv[2]);
printf("calculating Pythagorian theorem..... ");
if(a==0 || b==0 || c==0)
printf("Wrong entry");
else {
if(pythagorian_theorem(a,b,c)
printf("These three value can form a right angled triangl as theorem verifies correct");
else
printf("These three value can not form a right angled triangl as theorem verifies incorrect");
}
}

getch();
}

//calculate.h


long int factorial(unsigned int);
int permutation(unsigned int,unsigned int);
int combination(unsigned int,unsigned int);
int pythagorian_theorem(unsigned int,unsigned int,unsigned int);
int


long int factorial(unsigned int n)
{
if(n==0 || n==1)
return 1;
else
return(n*fact(n-1));
}
int permutation(unsigned int n,unsigned int k)
{
return(factorial(n)/factorial(n-k));
}

int combination(unsigned int n,unsigned int k)
{
return(factorial(n)/(factorial(k)*factorial(n-k)));
}
/*pythagorian theorem implementation
it is a theorem that defines right angled triangle
It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the "Pythagorean equation":[1]

{ a^{2}+b^{2}=c^{2},} a^{2}+b^{2}=c^{2},
where c represents the length of the hypotenuse and a and b the lengths of the triangle's other two sides.
*/


int pythagorian_theorem(unsigned int a,unsigned int b,unsigned int c)
{
if(c*c==(a*a+b*b))
return 1;
else
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