C Program : Explanation to Racer Data Storage Program

in c-programming •  last year 

Welcome back,
I am back with another explanation video from the program shared yesterday on my blog. If you want to skip explanation and try out the program then first visit the blog shared yesterday.

Let us now move on to the explanation. Let us look at the program,

Screenshot 2023-07-22 203950.png

Screenshot 2023-07-22 204010.png

Screenshot 2023-07-22 204114.png

You can copy the above code below :

#include <stdio.h>
#include <stdbool.h>

#define MAX_RACERS 100

// Function to sort the race times in ascending order using Bubble Sort
void bubbleSort(float times[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (times[j] > times[j + 1]) {
// Swap the elements
float temp = times[j];
times[j] = times[j + 1];
times[j + 1] = temp;
}
}
}
}

int main() {
float raceTimes[MAX_RACERS];
int numRacers = 0;
char option;

// Input race times for existing racers
printf("Enter the number of racers: ");
scanf("%d", &numRacers);

if (numRacers <= 0 || numRacers > MAX_RACERS) {
printf("Invalid number of racers. Please enter a value between 1 and %d.\n", MAX_RACERS);
return 1;
}

printf("Enter race times for %d racers (in seconds):\n", numRacers);
for (int i = 0; i < numRacers; i++) {
printf("Racer %d: ", i + 1);
scanf("%f", &raceTimes[i]);
}

// Sort race times in ascending order
bubbleSort(raceTimes, numRacers);

// Display the sorted race times in top-to-bottom position
printf("\nTop-to-bottom position:\n");
for (int i = 0; i < numRacers; i++) {
printf("Racer %d: %.2f seconds\n", i + 1, raceTimes[i]);
}

// Registration for a new racer
printf("\nDo you want to register a new racer? (Y/N): ");
scanf(" %c", &option);

if (option == 'Y' || option == 'y') {
if (numRacers == MAX_RACERS) {
printf("The maximum number of racers has been reached. Cannot register a new racer.\n");
} else {
printf("Enter the race time for the new racer (in seconds): ");
scanf("%f", &raceTimes[numRacers]);
numRacers++;

// Sort the updated race times with the new racer
bubbleSort(raceTimes, numRacers);

// Display the updated top-to-bottom position
printf("\nUpdated top-to-bottom position:\n");
for (int i = 0; i < numRacers; i++) {
printf("Racer %d: %.2f seconds\n", i + 1, raceTimes[i]);
}
}
} else {
printf("Thank you for using the race time organizer!\n");
}

return 0;
}

The program begins by including the necessary header files, stdio.h for input/output functions and stdbool.h to use the bool data type.

The program defines a constant MAX_RACERS to specify the maximum number of racers that can participate in the race. You can change this value as per your requirements.

The program defines a function bubbleSort() to sort the race times in ascending order using the Bubble Sort algorithm. Bubble Sort is a simple comparison-based sorting algorithm where adjacent elements are compared and swapped if they are in the wrong order.

Inside the main() function, the program declares an array raceTimes to store the race times of the racers and an integer variable numRacers to store the number of racers participating in the race.

The user is prompted to enter the number of racers (numRacers) for whom race times will be input.

The program checks if the entered number of racers is within the allowed range (1 to MAX_RACERS) and takes the input for the race times of each racer using a for loop.

After the race times are input, the program calls the bubbleSort() function to sort the race times in ascending order.

The sorted race times are then displayed in top-to-bottom position along with the corresponding racer numbers.

Next, the program asks the user if they want to register a new racer. If the user chooses to register a new racer (option == 'Y' or option == 'y'), it checks if the maximum number of racers (MAX_RACERS) has been reached. If not, it takes the race time for the new racer and adds it to the raceTimes array.

The updated raceTimes array with the new racer is sorted again using the bubbleSort() function, and the updated top-to-bottom position is displayed.

If the user decides not to register a new racer, the program displays a thank-you message.

The main() function ends, and the program returns 0, indicating successful execution.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!