1

I'm currently learning about top-down programming in C in my class and I somehow can't really get the hang of it.

I have been trying to learn it by this programming exercise where you have to calculate when someone should leave depending on their arrival time (and speed in km/hr and distance in km), but I have done something very, very wrong since the output is somewhere around four million - always.

I'm using the book Problem Solving and Program Design in C and the relevant chapter is 3.5. Can anyone tell me what I did wrong? Also can someone explain how formal parameters works in a ELI5 kind of way?

#include <stdio.h>
#define MINUTES_IN_HOUR 60

int find_dprt_time(int diffhrs, int diffmin, int arvl_time, int trvl_time);
double find_trvl_time(int trvl_time, int distance, int speed, double result);

int main()
{
     double distance;
     int time,
         speed,
         diffmin,
         diffhrs;

     printf("Enter the time you need to arrive in military time:\n");
     scanf("%d",&time);
     printf("Enter the distance to your destination in kilometers:\n");
     scanf("%lf",&distance);
     printf("Enter the speed you plan to average in km/hr:\n");
     scanf("%d",&speed);

     printf("Your departure time is %d%d.\n",diffhrs,diffmin);

     return 0;
}

int find_dprt_time(int diffhrs, int diffmin, int arvl_time, int trvl_time)
{

     diffhrs = arvl_time / 100 - trvl_time / 100;
     diffmin = arvl_time % 100 - trvl_time % 100;

     return (diffhrs, diffmin);
}

double find_trvl_time(int trvl_time, int distance, int speed, double result)
{

     result = distance / speed;
     trvl_time = MINUTES_IN_HOUR * result;

     return (trvl_time);
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
J.brink
  • 31
  • 3
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Oct 14 '16 at 14:45
  • 1
    You never call your calculation functions, so you end up with random garbage in the uninitialized variables `diffhrs` and `diffmin`. – Robert Oct 14 '16 at 15:15

2 Answers2

1

Your return (diffhrs, diffmin) statement doesn't do anything particularly useful - it just discards diffhrs and returns diffmin (read up on the comma operator in C).

Change:

return (diffhrs, diffmin);

to:

return diffhrs * 100 + diffmin;
Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Thank you and thank you for the debugging link! I've changed diffhrs and diffmin to dprt_time in main, but how to I make sure that the result from the return statement from find_dprt_time ends up in dprt_time in main? – J.brink Oct 14 '16 at 15:26
  • 1
    You need to *call* `find_dprt_time` - read up on calling functions in C and how to pass parameters. – Paul R Oct 14 '16 at 15:37
1

Welcome to Programming :)

You forgot to call your functions. So, you are displaying uninitialized variables. This might display some garbage values or it might even lead to a crash. See this question.

It is a good practice to initialize variables while you declare. Like:

int diffhrs = 0;
int diffmin = 0;

Try to call your functions. They might require some little tweaking. Try it out. That's the way to learn.

Also, it looks like you are trying to pass arguments by reference. You should make small changes for that.

Define function like this:

int find_dprt_time(int *diffhrs, int *diffmin, int arvl_time, int trvl_time)
{
    *diffhrs = arvl_time / 100 - trvl_time / 100;
    *diffmin = arvl_time % 100 - trvl_time % 100;
}

Call the function before displaying departure time like this:

find_dprt_time(&diffhrs, &diffmin, time, travel_time);

You can learn more about functions and passing arguments by reference here.

Community
  • 1
  • 1