This is an example that our prof gave us to solve. We basically had to fill in the blanks. I've bold the blanks that we filled in.
My answer differs with the prof's.
Here's the question: Assume a car moves with a constant speed, s. Given the travel time, t and speed, the total distance traveled is given by d = s X t.
My answer:
Code:
#include<stdio.h>
void dist(double d, double s, double t)
{
d= s*t;
}
int main(void)
{
double d,s=20, t=20;
dist(d, s, t);
printf("Distance traveled is %lf",d);
return(0);
}
Professor's Answer:
Code:
#include<stdio.h>
void dist(double *d, double s, double t)
{
*d= s*t;
}
int main(void)
{
double d,s=20, t=20;
dist(&d, s, t);
printf("Distance traveled is %lf",d);
return(0);
}
I don't understand why he used "&d" and "*d" instead of "d" in the function and while calling function. I'll be really thankful if somebody explains that to me.