Part 1 : Thanks a lot!! Part 2 (20 points) Use the two functions as subroutines
ID: 3686035 • Letter: P
Question
Part 1 :
Thanks a lot!!
Part 2 (20 points) Use the two functions as subroutines [ie. create two subroutines Part 2 (20 points) Use the two functions as subroutines (i.e. create two subroutines using your solution in Part 1) to compute ex from a value of x within the given range. The user should type in an input value of x, and your program will generate the value of e Print the values of e, e', ande' A helpful MIPS instruction is cvt.s.w fd fs that converts an integer in the source register fs into a single precision floating format in the destination register fd. Here is an example of its usage: mtc1 $ve, $f1 # move to register $f1 (in coprocessor C1) from register $v0 cvt. s . w $f1, $f1 # convert the integer in $fl to single precision floating point format div. s $f0, $f0, $f1 # divide $fo by $f1 and store the result in $f0Explanation / Answer
Part 1 :
1) Power(x,n) will return X^n
#include<stdio.h>
int power(int x, unsigned int n)
{
if( n == 0)
return 1;
else if (n%2 == 0)
return power(x, n/2)*power(x, n/2);
else
return x*power(x, n/2)*power(x, n/2);
}
int main()
{
int x = 2;
unsigned int n = 3;
printf("%d", power(x, n));
getchar();
return 0;
}
2) Factorial n will return n!
#include <stdio.h>
int main()
{
int n, count;
unsigned long long int factorial=1;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n< 0)
printf("Error!!! Factorial of negative number doesn't exist.");
else
{
for(count=1;count<=n;++count)
{
factorial*=count;
}
printf("Factorial = %lu",factorial);
}
return 0;
}
Part2: to compute e^x and print e,e^2,e^3
#include <stdio.h>
float exponential(int n, float x)
{
float sum = 1.0f; // initialize sum of series
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
int main()
{
int n = 1000;
float x,x1=1,x2=2,x3=3;
printf("Enter X Value /n /n");
scanf("%f",&x);
printf("e^x = %f", exponential(2*n, x));
printf("e^1 = %f", exponential(2*n, x1));
printf(" e^2 = %f", exponential(2*n, x2));
printf(" e^3 = %f", exponential(2*n, x3));
getchar();
return 0;
}