Calculate e^x given epsilon and x values in c - c

long double power1(double x, int n) {
long double power = 1;
int i;
for(i=1; i<=n; i++) {
power *= i;
}
return power;
}
long long int factorial(long long n) {
long long int pr = 1;
long long int i;
for(i=1; i<=n; i++){
pr *= i;
}
return pr;
}
double myExp(double x, double epsi) {
double sum=(double)1;
int i;
while(power1(x, i)/((1.0)*factorial(i)) - epsi <= 0) {
sum += power1(x, i)/(1.0*factorial(i));
i++;
}
return sum;
}
int main(int argc, char *argv[]) {
system("cls");
double x, epsi;
int n;
x=1.5;
epsi=0.00001;
n=1000;
printf("exp(%.lf,%f)=%f\n", x, epsi, myExp(x, epsi));
printf("\n");
}
I need help to fix my exponential function e^x given the epsi value and x value such that abs(x^n/n!) <= epsi, where n is the first integer satisfying the condition. It seems the while loop doesn't work. When I enter 1.5, the desired output is 4.481689, but the result is 1.0000.

Related

C.find an error in a very small program.Calculating the sum of the first k numbers of the sequence

Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}

Return two pointers of arrays

To summarize my problem,
i want an array to return two array pointers. One of these pointer arrays must be float or double, this will give me the quotient operation. Another should be int, and that should give the remainder of the division.
For examle if i have two arrays as: int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4}; when i want to reach my pointers result should be like: Quotient is: {1.5,2,3,3,4,4.5} Remainder is: {1,0,0,0,0,2}
Here is my codes:
#include<stdio.h>
void div(int a[], int b[], float *quotient, int *remainder) {
float quo[6];
int remain[6];
for(int i = 0; i< 6 ; i++)
{
quo[i] = a[i] / (double)b[i];
remain[i] = a[i] % b[i];
*remainder = remain[i];
*quotient = quo[i];
*remainder++;
*quotient++;
}
// quotient = quo;
// remainder = remain;
}
int main() {
int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4};
float q;
int r;
div(a, b, &q, &r);
for(int i = 0; i< 6 ; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", q, r);
}
// printf("Quotient is: %.1f\nRemainder is: %d\n", *q, *r);
return 0;
}
You need to pass an array for the quotient and remainder into the divide function.
Than you can read the values after the function returns.
#include<stdio.h>
void div(int* a, int* b, float* quotient, int* remainder, int count) {
for (int i = 0; i < count; i++)
{
quotient[i] = a[i] / (double)b[i];
remainder[i] = a[i] % b[i];
}
}
int main() {
int a[] = { 3,6,9,12,16,18 }, b[] = { 2,3,3,4,4,4 };
#define LENGTH (sizeof(a) / sizeof(int))
float quotient[LENGTH];
int remainder[LENGTH];
div(a, b, quotient, remainder, LENGTH);
for (int i = 0; i < LENGTH; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", quotient[i], remainder[i]);
}
return 0;
}

Function average and stdDev const int tab[ ]. Average problems

I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;

Factorials of large numbers

How to compute factorial of numbers like 300 as the output is not even in bound of unsigned long long int ?Please help.
#include<stdio.h>
#include<stdlib.h>
unsigned long long int factorial(int number) {
unsigned long long int temp;
if(number <= 1) return 1;
temp = (number * factorial(number - 1));
return temp;
}
int main(){
int t,k,i,a[100001];
unsigned long long int sum[100001];
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&a[i]);
}
for(k=0;k<t;k++){
sum[k]=0;
for(i=0;i<=a[k];i++){
sum[k] += ((factorial(a[k])/(factorial(i)*factorial(a[k]-i)))%3);
//printf("%d\n",sum[k]);
}}
for(i=0;i<t;i++){
printf("%llu\n",sum[i]);
}
return 0;
}
I tried this but it halt at only 60!.
You can try this for Factorials of large numbers :
#include<iostream>
#include<cstring>
int max = 5000;
void display(int arr[]){
int ctr = 0;
for (int i=0; i<max; i++){
if (!ctr && arr[i]) ctr = 1;
if(ctr)
std::cout<<arr[i];
}
}
void factorial(int arr[], int n){
if (!n) return;
int carry = 0;
for (int i=max-1; i>=0; --i){
arr[i] = (arr[i] * n) + carry;
carry = arr[i]/10;
arr[i] %= 10;
}
factorial(arr,n-1);
}
int main(){
int *arr = new int[max];
std::memset(arr,0,max*sizeof(int));
arr[max-1] = 1;
int num;
std::cout<<"Enter the number: ";
std::cin>>num;
std::cout<<"factorial of "<<num<<"is :\n";
factorial(arr,num);
display(arr);
delete[] arr;
return 0;
}

prime numbers in a given range

I have to find all the prime numbers between two numbers m and n. (1 <= m <= n <= 1000000000 and n-m <= 100000). I am using sieve of eratosthenes but getting wrong answer. Can anyone help me what is wrong with my code.
#include<stdio.h>
#include<math.h>
int S[100002];
void sieve(long long int m, long long int n)
{
long long int x=sqrt(n);
long long int i,j;
long long int a;
for(i=0;i<=n-m+2;i++)
S[i]=0;
if(m%2==0)
i=m;
else {
i=m+1;
}
for (;i<=n;i+=2){
S[i-m]=1;
}
for (i=3;i<=x;i+=2){
if(i>=m && S[i-m]) continue;
if(i*i>=m)j=i*i;
else {
a = (m-i*i)%(2*i);
if(a==0)j=m;
else
j=m+ (2*i -a);
}
for (;j<=n;j+=2*i){
S[j-m]=1;
}
}
if (m==1)i=1; else i=0;
for (;i<=n-m;i++)
if (!S[i]){
printf("%lld\n",i+m);
}
}
int main(){
int t;
long long int m,n;
scanf("%d\n",&t);
while(t--){
scanf("%lld %lld",&m,&n);
sieve(m,n);
printf("\n");
}
return(0);
}
if(m%2==0)
i=m;
else {
i=m+1;
}
for (;i<=n;i+=2){
S[i-m]=1;
}
Now, what happens if m <= 2? Will 2 be considered prime or not?
You should use loop in main and call prime function.
For performance, I recommend you to avoid using sqrt function because it requires a lot of CPU clocks.
bool isPrime(int number){
if(number < 2) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(int i=3; (i*i)<=number; i+=2){
if(number % i == 0 ) return false;
}
return true;
}
***Change datatype for the range of number (long, long long, etc).
It is the most efficient(Sieve method) way of finding prime number between a range.
Here 1 is not consider as a prime number as a conventionally way.
#include<stdio.h>
#include<string.h>
#define max 10000000
using namespace std;
int main()
{
unsigned long long int i, j, k, m, n;
unsigned long long int* a = new unsigned long long int[max];
scanf("%ul %ul",&m,&n);
for(i = 1;i<=n;i++)
a[i]=i;
a[1] = 0;
for(i=2;(i*i)<=n;i++)
if(a[i]!=0)
for(k=2*i;k<=n;k=k+i)
if(a[k]!=0)
a[k]=0;
for(i =m;i<=n;i++)
if(a[i]!=0)
printf("%ul ",a[i]);
memset(a, 0, sizeof(a));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
void prime(int );
int main(){
int x, end;
printf("Enter end of the range:\n");
scanf("%d", &end);
for(x = 2;x <= end;x++){
prime(x);
}
return 0;
}
void prime(int x){
int j, count = 1;
for(j=2;j <= x;j++){
if(x % j == 0){
count += 1;
//printf("count = %d,x = %d", count, x);
}
}
if(count == 2){
printf("\n%d\n", x);
}
}

Resources