Function help

Hey guys,

Just started learning about functions and attempted a problem from a textbook; "Write a function cube_root that receives a double argument and calculates and returns an approximate cube root for it. You should also write a main program that allows you to test your function"

I tried, and subsequently failed. Any help as to where I went wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_VALUE 1e+6
#define MIN_VALUE 1e-6
#define MAX_LOOP 25


double cbrt(double input);

int main(int argc, char *argv[]){

    int input;

    printf("Enter a number to be cube rooted:");
    scanf("%d", input);

    printf("The resulting cube root is: %d", cbrt(input));

return 0;

}

double cbrt(double input){

    int i;
    double solution;
    double x = 1.0;

    if (MIN_VALUE>input || MAX_VALUE<input){
        return 0;
    }

    for (i=0;i<MAX_LOOP;i++){
        solution = (2*x+input/(x*x))/3;

    }

    return solution;

    }





Program crashes when I enter anything. Thanks.
Use

scanf("%d", &input);

instead of

scanf("%d", input);
1) you should pass variable to scanf() by pointer (why do you use scanf()?)
Like that: scanf("%d", &input);
2) %d is for integers. I don't remember, what is used for doubles, but you can use %f for floats.
printf("The resulting cube root is: %f", cbrt(input));
3) Your cbrt function ist' working properly. Look at your loop: for now it is equivalent to
(2+input)/3
Topic archived. No new replies allowed.