Rejecting unsafe velocity inputs

A C program reads a signed displacement and an elapsed time to calculate average velocity. Its input code is


C
double displacement_m = 0.0;
double time_s = 0.0;
int fields_read = scanf("%lf %lf", &displacement_m, &time_s);


Assume any converted values are finite and representable. The program should reject the input whenever fewer than two values were assigned or the elapsed time is not strictly positive. Signed displacement is allowed. Which condition correctly fills the blank before the division?


C
if ( /* condition */ )
{
    printf("Invalid input\n");
    return 1;
}
double velocity_m_s = displacement_m / time_s;
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More C Basics Questions: