Complementary task for topic: 6
M Nemeth 2023-08-29 15:21:04.627218'
Pointers: Pass to function by address
Pointers: Pass to function by address
Write a program, that doubles a complex number, modifying the variable. The doubling should be realized in a separate function.
Hint: The same problem, but now we want to modify an outer variable inside a called function. This cannot be done without the help of pointers.
Solution
#include
void double_complex(double* re, double* im){
*re=2*(*re);
*im=2*(*im);
return;
}
int main()
{
double re=1.0, im=2.0; //to be doubled
printf("The double of %lf + %lf i ",re,im); //Must printed here, as the variable itself will change!
double_complex(&re,&im);
printf("is %lf + %lf i",re,im); //Must printed here, as the variable itself will change!
return 0;
}