C Program : Racer Data Storage Program

in c-programming •  last year 

Welcome back,
We are here with a new program for everyone. Today i will share a program related to Race competition data so let's get started.

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;
}

image.png

Now it is time for you to find the correct output for the given program. I am not going to give options today to make it even more difficult.

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!