Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C My professor said there is no way this code works...but it does and we don't know why.

bouttreefiddy

New Coder
This was a a homework assignment to take the input of the mile time of three runners and return their time in m/s and ft/s. We had to create three functions. I wasn't too sure about functions so I just did some trial and error until it worked. When I submitted it, he gave me a 0 and said that there was no way that the code actually worked and to fix it... but it really does work. I talked to him about it and he said that it shouldn't work because I am using the return of "calc_seconds" (which just converts the inputted minutes/seconds into straight seconds) from the first function to calculate the m/s and ft/s in their respective functions. Since the functions can't talk to each other directly, it shouldn't work, right? I know it says not to just paste a wall of code but I feel like I have to include all of it since I don't know what could be making it work. Like I said, I have tried it with dozens of numbers and it's always correct. It's probably not the most well written code but I am just getting started learning it.

C:
#include <stdio.h>

//constants
#define FEET_PER_MILE 5280
#define METERS_PER_MILE 1609.34

//function protypes
int time_in_seconds(int minutes, int seconds);
double feet_per_second(int minutes, int seconds);
double meters_per_second(int minutes, int seconds);


int main(void)
{
  int input_minutes, input_seconds, out_seconds;
  double fps, mps;

  printf("Enter Runner 1's time in minutes and seconds.\n");
  scanf("%d" "%d", &input_minutes, &input_seconds); //get input
  out_seconds = time_in_seconds(input_minutes, input_seconds);  //call functions
  fps = feet_per_second(out_seconds, FEET_PER_MILE);
  mps = meters_per_second(out_seconds, METERS_PER_MILE);
  printf("Runner 1 ran the mile in %d minutes and %d seconds at a speed of %.3lf feet per second, or %.3lf meters per second.\n\n", input_minutes, input_seconds, fps, mps); //display outputs

  printf("Enter Runner 2's time in minutes and seconds.\n");
  scanf("%d" "%d", &input_minutes, &input_seconds);
  out_seconds = time_in_seconds(input_minutes, input_seconds);
  fps = feet_per_second(out_seconds, FEET_PER_MILE);
  mps = meters_per_second(out_seconds, METERS_PER_MILE);
  printf("Runner 2 ran the mile in %d minutes and %d seconds at a speed of %.3lf feet per second, or %.3lf meters per second.\n\n", input_minutes, input_seconds, fps, mps);

  printf("Enter Runner 3's time in minutes and seconds.\n");
  scanf("%d" "%d", &input_minutes, &input_seconds);
  out_seconds = time_in_seconds(input_minutes, input_seconds);
  fps = feet_per_second(out_seconds, FEET_PER_MILE);
  mps = meters_per_second(out_seconds, METERS_PER_MILE);
  printf("Runner 3 ran the mile in %d minutes and %d seconds at a speed of %.3lf feet per second, or %.3lf meters per second.\n", input_minutes, input_seconds, fps, mps);

return 0;
}

//function definitions

//calculate total seconds
int time_in_seconds(int minutes, int seconds)
{
  int calc_seconds;
  calc_seconds = (minutes*60)+seconds;
  return calc_seconds;

}
//calculate fps
double feet_per_second(int minutes, int seconds)
{
  double calc_fps;
  int calc_seconds;
  calc_fps = (double)FEET_PER_MILE/calc_seconds;
  return calc_fps;

}
//calculate mps
double meters_per_second(int minutes, int seconds)
{
  double calc_mps;
  int calc_seconds;
  calc_mps = (double)METERS_PER_MILE/calc_seconds;
  return calc_mps;
}
 
Hello bouttreefiddy,

Waaahh, gave you 0, kind of rude :laugh:.

I must say your code is pretty clean.

There is a small problem in the functions 'feet_per_second, and 'meters_per_second'. You are using the variable 'calc_seconds;':
C:
calc_mps = (double)METERS_PER_MILE/calc_seconds;
But you did not set its value...:
C:
int calc_seconds;

You also do not use the argument 'minutes'.

The arguments you give the function 'meters_per_second' do not seem to match what it expects:
mps = meters_per_second(out_seconds, METERS_PER_MILE);
double meters_per_second(int minutes, int seconds)
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom