Pass by Reference problem

I have an assignment that I know i'm close but i'm not sure what i'm missing. Let me know what you all think.

Assignment:

3. Write a function called cube that returns void, and takes one parameter; a double*. This function will calculate the cube of the variable passed in by reference. Call the parameter pVariable.

Inside the function, print out the value that is in pVariable, AND the value that it points to. Then calculate the cube of *pVariable.(This is what is really tripping me up)

In the main print out the value that is returned through the parameter. For example, if you pass a variable to the function that contains 3.0, that same variable will contain (the cube 3.0 * 3.0 * 3.0) which is 27.0 after the function is over.

Don’t forget to make a prototype for your function. The main function goes at the top, and the sub-functions go below.

Call your function again from the main to find the cube of 3.43. Print out the new value that is stored in variable. The answer should be: 40.353607

[code]
#include<stdio.h>
#include<stdlib.h>


void cube(double*);

main() {

double variable, newVariable;


cube(&variable);

newVariable = variable * variable * variable;
printf("The new Variable is : %.2lf\n", newVariable);

system("pause");
}

void cube(double *pVariable) {

void cube(double *pVariable);

*pVariable = 3.43;

printf("pVariable has %.1lf and has a value of %p\n", &pVariable);

}
Updated program but I still do not have it right

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
#include<stdio.h>
#include<stdlib.h>


void cube(double*);

main()	{

	double a = 3.0, b;
	

	cube(&a);

	b = a*a*a;
 
	printf("The new Variable is :   %.2lf\n", b);

	system("pause");
}

void cube(double *pVariable)	{

	printf("pVariable has %i and has a value of %p\n", &pVariable);

}
closed account (z05DSL3A)
void cube(double *pVariable) is where you are meant to be doing the maths...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<stdlib.h>


void cube(double*);

int main()
{
    double a = 3.43;
    
    cube(&a);
    
    printf("The new Variable is :   %.6lf\n", a);
}

void cube(double *pVariable)
{
    // do the maths here
    
    return;
}
Last edited on
I know how the math is suppose to go but now I keep getting an error saying you can not convert a double to double

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<stdlib.h>


void cube(double, double*);

main()	{
	double a = 3.0;
	double variable;
	
	printf("pVariable has %.2lf and has a value of %p\n", a, &a);

	cube(a, &variable);
 
	printf("The new Variable is :   %.6lf\n", a);

	system("pause");
}

void cube(double a, double *pVariable)	{

	pVariable = a * a * a;

}
Last edited on
It isn't saying you are converting from double to double, it is saying you are converting from double to double * (pointer to double). Change line 22 to *pVariable = a * a * a;. You have to dereference pVariable. The way you have it there, you are trying to modify the memory address rather than the value it is pointing to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<stdlib.h>


void cube(double, double*);

int main()	{
	double a = 3.0;
	double variable;
	
	printf("pVariable has %.2lf and has a value of %p\n", a, &a);

	cube(a, &variable);
 
	printf("The new Variable is :   %.6lf\n", a);

	system("pause");
}

void cube(double a, double *pVariable)	{

	*pVariable = a * a * a;

}
Last edited on
closed account (z05DSL3A)
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
#include<stdio.h>
#include<stdlib.h>


void cube(double*);

int main()
{
    double a = 3.43;
    
    cube(&a);
    
    printf("The new value for variable is :   %.6lf\n", a);
}

/******************************************************************************
 *
 * Write a function called cube that returns void, and takes one parameter; a 
 * double*. This function will calculate the cube of the variable passed in by 
 * reference. Call the parameter pVariable.
 *
 * Inside the function, print out the value that is in pVariable, AND the value 
 * that it points to. Then calculate the cube of *pVariable.
 *
 *****************************************************************************/
void cube(double *pVariable)
{
    double a = *pVariable;
    
    printf("The address that pVariable has is %p and has a value at that " \
           "address is %f\n", pVariable, *pVariable);
    
    *pVariable = a * a * a;
    
    return;
}
@CDavis
If this is a C++ file, I was wondering, do you have to use the C headers or will your teacher let you use cstdio and cstdlib instead?

If it is a C file, then that question is answered right there.
Last edited on
Topic archived. No new replies allowed.