1

I am in the midst of reading "C Primer Plus" by Stephen Prata. The 7th programming exercise of the 5th chapter wants you to "Write a program that requests a type double number and print the value of the number cubed. Use a function of your own design to cube the value and print it. The main() program should pass the entered value to this function."

What am I doing wrong? When I run this, I only get the same number I entered.

#include <stdio.h>
void cube(double n);
int main(void)
{
    double a;
    printf("Enter a Number: ");
    scanf("%lf", &a);
    cube(a);
    printf("%lf", a);

    return 0;
}

void cube(double n)
{
    n = n * n * n;
}
2
  • 1
    You're passing by value, so cube only modifies its local copy of the value you pass it. Commented May 20, 2014 at 15:34
  • 1
    All these answers are wrong. The exercise is to "use a function of your own design to cube the value and print it" (emphasis mine). It wants you to print the value from inside your function, not from main(), so all these considerations about returning values or passing a pointer are off-point. Commented May 20, 2014 at 16:15

7 Answers 7

2

You need to return the value!

#include <stdio.h>
double cube(double n);
int main(void)
{
    double a;
    printf("Enter a Number: ");
    scanf("%lf", &a);
    printf("%lf", cube(a));

    return 0;
}

double cube(double n)
{
    n = n * n * n;
    return n;

}

If you don't want to return the values then you can use pointers

#include<stdio.h>
void cube(double *n)
{
  *n = (*n) * (*n) * (*n);
}
int main()
{
  double n = 10;
  cube(&n);
  printf("%lf\n",n);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, you had my exact solution but with double instead of int.
@aglasser Sorry boss! I have the experience! :p ;)
1

Your function cube needs to return a result, e.g.

#include <stdio.h>

double cube(double n);

int main(void)
{
    double a, b;

    printf("Enter a Number: ");
    scanf("%lf", &a);
    b = cube(a);      // call `cube()` to get cube of a and save in b
    printf("%lf", b);

    return 0;
}

double cube(double n)
{
    return n * n * n; // return n cubed as function result
}

2 Comments

do you have to answer such questions?
@Cool_Coder: sure, why not ?
0

Your function cube() does cube the value assigned to a but does not return the result n.

This means that the variable a back in main has not changed.

You will need to alter your function prototype such that your cube function can be used as follows:

a = cube(a);

This will mean that a will be updated with the result returned by the function cube()

Comments

0

As the other answers have mentioned, your cube function must return a value. You can keep the return type as void however, if you pass in a pointer to n. Parenthesis for readability.

void cube(int *n)
{
    // dereference the pointer and cube it
    *n = (*n) * (*n) * (*n);
}

int main()
{
    int n = 3;
    cube(&n); // address of n variable
    printf("%d\n", n);
    return 0;
}

Outputs 27.

Comments

0

Instead of passing the value of a, you could pass the address of a in main().

#include <stdio.h>
void cube(double n);
int main(void)
{
    double a;
    printf("Enter a Number: ");
    scanf("%lf", &a);
    cube(&a);  
    printf("%lf", a);

    return 0;
}

Then in cube(), modify what is pointed to by the pointer n.

void cube(double *n)
{
    *n = (*n) * (*n) * (*n);
}

Comments

0

Thank you very much Paul Griffiths for carefully reading the question!

Following your advice the code should look like this:

#include <stdio.h>
void cube(double n);
int main(void)
{
    double a;
    printf("Enter a Number: ");
    scanf("%lf", &a);
    cube(a);

    return 0;
}

void cube(double n)
{
    n = n * n * n;
    printf("%lf", n);
}

Comments

-1

You should've passed the n in the function void cube (double n) as a reference.

void cube (double &n)

Read about the reference variables in C++ here http://www.tutorialspoint.com/cplusplus/cpp_references.htm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.