Comparing C and Python: Thunderstorm Distance Calculator 04 Jul 2023

In this post, we’ll compare the C code of a Thunderstorm Distance Calculator with its Python counterpart. By examining the differences between the two implementations, we can gain insights into how Python’s syntax and features diverge from C.

Let’s start by reviewing the original C code:

#include <stdio.h>

// Constants
#define SPEED_OF_SOUND 343.2
#define INVALID_DISTANCE -1.0
#define INVALID_INPUT -1.0

// Calculate distance based on time elapsed
double calculate_distance(double time_elapsed) {
    if (time_elapsed < 0) {
        printf("Invalid input: time elapsed cannot be negative.\n");
        return INVALID_DISTANCE;
    }
    return SPEED_OF_SOUND * time_elapsed;
}

// Get time elapsed from user input
double get_time_elapsed() {
    double time_elapsed;
    printf("Enter the time elapsed (in seconds) between seeing the lightning and hearing the thunder: ");
    if (scanf("%lf", &time_elapsed) != 1) {
        printf("Error reading input. Please enter a valid floating-point number.\n");
        return INVALID_INPUT;
    }
    return time_elapsed;
}

// Print the calculated distance
void print_distance(double distance) {
    if (distance >= 0) {
        printf("The thunderstorm is approximately %.2f meters away.\n", distance);
    }
}

// Main program entry point
int main() {
    double time_elapsed = get_time_elapsed();
    double distance = calculate_distance(time_elapsed);
    print_distance(distance);
    return 0;
}

Now, let’s examine the Python version, emphasizing the differences from the original C code:

# Constants
SPEED_OF_SOUND = 343.2
INVALID_DISTANCE = -1.0
INVALID_INPUT = -1.0

# Calculate distance based on time elapsed
def calculate_distance(time_elapsed):
    if time_elapsed < 0:
        print("Invalid input: time elapsed cannot be negative.")
        return INVALID_DISTANCE
    return SPEED_OF_SOUND * time_elapsed

# Get time elapsed from user input
def get_time_elapsed():
    while True:
        try:
            time_elapsed = float(input("Enter the time elapsed (in seconds) between seeing the lightning and hearing the thunder: "))
            return time_elapsed
        except ValueError:
            print("Error reading input. Please enter a valid floating-point number.")

# Print the calculated distance
def print_distance(distance):
    if distance >= 0:
        print(f"The thunderstorm is approximately {distance:.2f} meters away.")

# Main program entry point
def main():
    time_elapsed = get_time_elapsed()
    distance = calculate_distance(time_elapsed)
    print_distance(distance)

if __name__ == "__main__":
    main()

1. Syntax and Variable Declarations:

2. Preprocessor Directives and Constants:

3. Input and Output:

print(“Enter the time elapsed…”) and time_elapsed = float(input())`.

4. Exception Handling:

5. String Formatting:

6. Loop Constructs:

7. Function Definitions:

8. String Concatenation:

Similarities

Despite the differences outlined above, there are some similarities between the C and Python implementations:

article [ programming  computerscience  ]