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);
#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);
}
#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;
}
#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;
}
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.
#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;
}
#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;
}