(you can ignore the questions for thought IF YOU HAVE TO) 2.1 Write a function i
ID: 3624140 • Letter: #
Question
(you can ignore the questions for thought IF YOU HAVE TO)
2.1 Write a function in c++ that can/will calculate factorial of numbers. This factorial function should accept an integer number as variable n as input and return the factorial of a number. Proto-type of this factorial function has to be: unsigned long factorialTest (int n). Using a for loop, print factorial for n = 0 to 10.
Your output should look like the following:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Part two:
Now write a function that can/will calculate the value of an e^x function as the sum of n element(s). e^x function is calculated with:
The following should look like the output:
exp(0) actual value = 1:00 computed value = 1:00
1exp(1) actual value = 2:72 computed value = 2:72
exp(2) actual value = 7:39 computed value = 7:39
exp(3) actual value = 20:09 computed value = 20:09
exp(4) actual value = 54:60 computed value = 54:58
exp(5) actual value = 148:41 computed value = 148:11
exp(6) actual value = 403:43 computed value = 399:87
exp(7) actual value = 1096:63 computed value = 1067:02
exp(8) actual value = 2980:96 computed value = 2790:78
exp(9) actual value = 8103:08 computed value = 7096:47
exp(10) actual value = 22026:47 computed value = 17435:19
Hints:
Use for loop to compute sum of the first 12 elements of the equation. Call the factorial function to compute the factorial value required for the e^x function computation. Use exp(x) function call from cmath library to compute the actual e^x value. Computed value is calculated using exponential(x; 12) function call.
Lastly answer three simple questions:
Questions for Thought:
(a) When do we use functions ?
(b) What are advantageous and disadvantages of using functions?
(c)Suppose the variable x is declared inside main, factorial and exponentialfunction. Is this an error?
(d) What is the dierence between unsigned int and int?
(e) Write a function that calculates value of a sin(x) function as sum of n elements. sin(x) function is calculated as
where x is in radians.
What I have so far is the full first part (feel free to correct mistakes):
#include
#include
using namespace std;
unsigned long factorial(int n)
{
unsigned long value=1;
for(int x=1; x<=n; ++x)
{
value *=x;
}
return value;
}
int main()
{
unsigned long k = factorial(0);
cout << "0! = " << k << endl;
unsigned long j = factorial(1);
cout << "1! = " << j << endl;
unsigned long i = factorial(2);
cout << "2! = " << i << endl;
unsigned long h = factorial(3);
cout << "3! = " << h << endl;
unsigned long g = factorial(4);
cout << "4! = " << g << endl;
unsigned long f = factorial(5);
cout << "5! = " << f << endl;
unsigned long e = factorial(6);
cout << "6! = " << e << endl;
unsigned long d = factorial(7);
cout << "7! = " << d << endl;
unsigned long c = factorial(8);
cout << "8! = " << c << endl;
unsigned long b = factorial(9);
cout << "9! = " << b << endl;
unsigned long a = factorial(10);
cout << "10! = " << a << endl;
return 0;
}
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
unsigned long factorial(int n)
{
unsigned long value=1;
for(int x=1; x<=n; ++x)
value *=x;
return value;
}
double exponential(int k,int p)
{
double sum=0;
for(int p=0; p<=12; p++)
sum = sum + (pow(k,p)/factorial(p));
return sum;
}
int main()
{
unsigned long k = factorial(0);
cout << "0! = " << k << endl;
unsigned long j = factorial(1);
cout << "1! = " << j << endl;
unsigned long i = factorial(2);
cout << "2! = " << i << endl;
unsigned long h = factorial(3);
cout << "3! = " << h << endl;
unsigned long g = factorial(4);
cout << "4! = " << g << endl;
unsigned long f = factorial(5);
cout << "5! = " << f << endl;
unsigned long e = factorial(6);
cout << "6! = " << e << endl;
unsigned long d = factorial(7);
cout << "7! = " << d << endl;
unsigned long c = factorial(8);
cout << "8! = " << c << endl;
unsigned long b = factorial(9);
cout << "9! = " << b << endl;
unsigned long a = factorial(10);
cout << "10! = " << a << endl;
for(int k=0; k<=10; k++)
cout <<"exp(" <<k <<") actual value = "<<exp(k)<<" computed value = "<<exponential(k,12) << endl;
return 0;
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.