I've tried changing variable types but this is still not working.
double power(double a, long long b){
while(b>1){
a *= a;
b--;
}
return a;
}
Your code won't run properly when b>2 because when you're doing a = a*a; the second time it won't be a^3 but a^4 and the next time it will be a^8 and so on.
The right code would be something like this below:
double power(double a, long long b){
double k = 1;
while(b>0){
k *= a;
b--;
}
return k;
}
You're changing a on every iteration. Say you call it like power(2, 3).
First you do 2 * 2 and assign this to a, which becomes 4.
Next iteration, you'll do again a * a which is 4 * 4. Just keep the result in a variable and don't change the arguments:
double power(double a, long long b){
double r = a;
while(b>1){
r *= a;
b--;
}
return r;
}
you are changing the variable a and it courses the defects. what you can do instead is
double power(double base, double exp)
{
double result = 1;
for(int i = 0; i < exp; i++)
{
result *= base;
}
return result;
}
Related
I tried following function (as suggested on these forums) to calculate power. However, it is causing program to hang up.
static long ipow(int b, int e) {
long r = 1;
while (e--) r *= b;
return r;
}
double cfilefn(int a, int b, int c) {
return (ipow(a, b) / (double)c);
}
cfilefn(2,3,4);
The function looks all right. Where is the error and how can it be solved?
The ipow function will misbehave if the second argument is a negative number: it will run for a while and have implementation defined behavior when e reaches INT_MIN. You should modify the test while (e--) r *= b; as:
static long ipow(int b, int e) {
long r = 1;
while (e-- > 0)
r *= b;
return r;
}
Note however that ipow will cause arithmetic overflow for moderately large values of e and since you want a double result from cfilefn, you should use double arithmetics for the power function:
#include <math.h>
double cfilefn(int a, int b, int c) {
return pow(a, b) / c;
}
I am trying to use the function that I was given by my professor to calculate the integral of a polynomial function (polynomial such as: ax^2+bx+c). the function is:
double numbericalIntegration(double a ,double b ,double(*func)(double)){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x);
}
return sum*delta;
}
I changed a lot in order to integrate a polynomial function. but I was get the answer 0. why is that? and I'd appreciate if anybody tried to correct my work. my code is:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,3))/3 +(b*pow(x,2))/2 + (c*x);
}
double numbericalIntegration(double a ,double b ,double(*func)(double,double,double,double), double firstNum, double secondNum, double thirdNum){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x, firstNum, secondNum, thirdNum);
}
return sum*delta;
}
int main()
{
double (*func)(double,double,double,double);
func = integralPoly;
double sum = numbericalIntegration(2,4,func,1,1,4);
printf("sum = %d",sum);
return 0;
}
You need to change two things. First your polynomial function doesn't make any sense. You said it needs to be in the form of ax^2+bx+c but in your code polynomial is (ax^3)/3+(bx^2)/2+c*x. Your function should be:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,2)) +(b*x) + c;
}
Also you need to change your printf. %d is integer type specifier and you need double, so you need to use %f for example:
printf("sum = %f",sum);
Now the output of your program is:
sum = 32.666016
which is correct for your parameters.
I want to write a program that calculates expontiation using the Gauss' algorithm but if give input etc base=2,exp=50 i get as a result 0.0000.
#include<stdio.h>
float fastpower(int a,int b);
main()
{
int base,exp;
printf("Base:\n");
scanf("%d",&base);
printf("Exp:\n");
scanf("%d",&exp);
fastpower(base,exp);
system("pause");
}
float fastpower(int a,int b)
{
double result=1;
while (b>0)
{
if (b%2!=0)
result=result*a;
b=(b/2);
a=a*a;
}
printf("result is %lf\n",result);
}
Declare a as long (int64):
/*
compute a**b
*/
/* double fastpower(double a, int b) is even more better */
double fastpower(long a, int b) { /* double is more natural here: double result */
double result = 1.0;
while (b > 0) {
if (b % 2 != 0)
result *= a;
b /= 2;
a *= a; /* <- a is long to prevent overflow here */
}
/* You'd rather not output in functions */
printf("result is %lf\n", result);
return result; /* do not forget to return the result*/
}
But long could do overflow as well (e.g. 10**50); in this case use double for a
We're taking up numerical methods in my programming class and the first algorithm introduced to us was the bisection method for root finding. Here's my attempt at implementing it using recursion:
#include <math.h>
#include <stdio.h>
#define tolerance 0.00001
double my_function(double z){
double answer = 5*pow(z,2) + 5*z - 2;
return answer;
}
double bisection(double (*fxn)(double),double a, double b){
double m = ((a+b)/2);
if (fabs(b-a) < tolerance){
double root = a;
printf("value of a is %lf\n",root);
return a;
}
else if (fxn(m) > 0){
b = m;
}
else if (fxn(m) < 0){
a = m;
}
bisection(my_function, a, b);
}
int main(){
double g = 0.01;
double z = 1;
double x = bisection(my_function,g,z);
printf("root is %lf\n",x);
return 0;
}
and here is the output:
value of a is 0.306225
root is nan
The root is correct (slightly off, but within the tolerance level) but somewhere in between returning the value and printing it, it somehow turns into NaN. I'm stumped. What am I doing wrong?
You are not returning from the recursive call. Change the last statement in bisection to
return bisection(my_function, a, b);
My first guess:
in the section
if (fabs(b-a) < tolerance){
double root = a;
printf("value of a is %lf\n",root);
return a;
}
You return a instead of root. Try returning root and see if that helps.
How do I write a function that have a input function (is objective to any function), array of input numbers and length of input array?
Function:
double accumulator(double (*function)(double, double), double array[], int length)
Main:
int main(){
double array[10];
for (int i=0; i<10; i++)
array[i] = i+1;
printf("Sum is: %g\n", accumulator(sum,array,10));
printf("Product is: %g\n", accumulator(product,array,10));
return 0;
}
For example sum should be 55 (1 + 2 + .... + 10) and product 362880 (1 * 2 * ... * 10).
I guess the function should by recursive but I still cant get the right results :/
I have got this non-recursive solution but it of course works only for sum...
double accumulator(double (*function)(double, double), double array[], int length)
{
int temp = 0;
for (int i = 0;i<length;i++)
{
temp = (*function)(temp, array[i]);
}
return temp;
}
on the top of course:
double sum(double x, double y){
return x+y;
}
double product(double x, double y){
return x*y;
}
What is wrong with:
double multiplicator(double (*function)(double, double), double array[], int length)
{
int temp = 1;
for (int i = 0;i<length;i++)
{
temp = (*function)(temp, array[i]);
}
return temp;
}
Either a different function or you need to supply the neutral element for the operation (0 for sum, 1 for product).
It doesn't work for multiplication because multiplying anything by 0 gives, well 0
you need to use first element as an initial value
double accumulator(double (*function)(double, double), double array[], int length)
{
int temp = array[0];
for (int i = 1; i < length;i++) // start from #1
{
temp = (*function)(temp, array[i]);
}
return temp;
}
Your solution is almost there if you set temp = array[0] and start your loop at i = 1 instead of i = 0.
Two thoughts:
You should use double temp rather than int temp.
You need to have a different starting value for addition versus multiplication. A sum should start at temp = 0, but a product should start at temp = 1. Otherwise the product will always be 0.
You could add add another initial value parameter:
double accumulator(double (*function)(double, double), double array[], int length, double initial)
Or you use the first array element as the starting value (but then you'll need to check for the special case where the array is empty):
double temp = array[0];
For what it's worth, your "accumulator" function is alternatively known as "reduce" in other functional programming contexts. That may help if you want to Google the term.