# Compute the results of a race against Achilles [closed]

I was given the task to write a code that takes inputs from the user that include the following:

• length of a certain race
• the speed of Achilles (the main participant)
• the number of participants excluding Achilles
• the head start that each participant will get (a head start from Achilles)
• speed of each participant

I then was asked to calculate the time that takes Achilles and each participant to finish the race and then the output would be:

1. the time that took Achilles to finish the race
2. the ones that took a shorter time than Achilles to finish the race
3. the ones that finished the race after Achilles (3 & 2 in the same line and the order is the same order of the input. one space between each number excluding the last)
4. When Achilles finished the race

## Example:

input:

100
9.5
5
20 7.5 90 1.2 10.6 4.25 80 1.7 50 7.3


output:

10.53
8.33 6.85 10.67 21.04 11.76
3


I wrote the code and it works fine, although I can't eliminate the last space from the scond line of the output. (in the example above, the line ends 11.76 )

## The code:

#include <stdio.h>

int main()
{
int length,size,cnt1=0,i;
float speed1,speed2[7],distance[7],time1,time2[7],small[7],big[7];
scanf("%d",&length);
scanf("%f",&speed1);
scanf("%d",&size);
time1=(float)length/speed1;
for(i=0;i<size;i++){
scanf("%f%f",&distance[i],&speed2[i]);
}
for(i=0;i<size;i++){
time2[i]=((length-distance[i])/speed2[i]);
}
if(distance[i]>=length)
time2[i]=0;
printf("%.2f",time1);
printf("\n");

for(i=0;i<size;i++){
if(time2[i]<time1){
small[i]=time2[i];
printf("%.2f ",small[i]);
cnt1++;
}
}
for(i=0;i<size;i++){
if(time2[i]>=time1){
big[i]=time2[i];

printf("%.2f",big[i]);
if(i+1<size)
printf(" ");
}
}
printf("\n");
printf("%d",cnt1+1);

return 0;
}

• "now I wrote the code and it works fine but I can't succeed to eliminate the last space from the third line of the output." So it doesn't work completely yet? – Mast May 28 '19 at 15:54
• it does work. however, the design of the output is not perfect(or as specified) – baraah baryhe May 29 '19 at 11:36

The indentation seems to have lost a level for much of the code, which makes it hard to read. I'm guessing something went wrong when you copied the code into the question, and that your source doesn't really look that bad.

Let's start with the definition of main(). It's generally better to declare main as a function taking no arguments: int main(void).

Consider that all I/O may fail. For this program, it's probably acceptable to ignore output errors, but we really must handle input errors correctly, even if it's as simple as

if (scanf("%d", &length) != 1) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}


We can save some repetition by combining reads:

if (scanf("%d%f%d", &length, &speed1, &size) != 3) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}


Now we come to reading the other participants' speeds and head starts. The code currently assumes that there are no more than 7 participants, and relies on fixed size arrays to store them. But look at the requirements: we get to print them in the same order that we read them, and we don't need to store any of these values after we've printed them, so we can make a much simpler loop:

#include <stdio.h>

int main(void)
{
double length;
double achilles_speed;
unsigned int competitor_count;

if (scanf("%lf%lf%u", &length, &achilles_speed, &competitor_count) != 3) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}

const double achilles_time = length / achilles_speed;
printf("%.2f\n", achilles_time);

unsigned int achilles_position = 1; /* start at 1, and increment for each faster competitor */

for (unsigned int i = 0;  i < competitor_count;  ++i) {
if (scanf("%lf%lf", &head_start, &speed) != 2) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}

double time = (length - head_start) / speed;
printf("%.2f ", time);

if (time < achilles_time) {
++achilles_position;
}
}

printf("\n%u", achilles_position);

return 0;
}


That's starting to look a bit simpler, but I've lost the logic that omits the space after the last finisher's time. One way to change this is to print a character before the time, and change that after the first output:

printf("%.2f", achilles_time);

char sep = '\n';

for (unsigned int i = 0;  i < competitor_count;  ++i) {
...
printf("%c%.2f", sep, time);
sep = ' ';
...
}


Here, we start by printing the newline that ends Achilles' time line on the first iteration, but change that to a space for the subsequent iterations.

# Complete modified code

#include <stdio.h>

int main(void)
{
double length;
double achilles_speed;
unsigned int competitor_count;

if (scanf("%lf%lf%u", &length, &achilles_speed, &competitor_count) != 3) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}

const double achilles_time = length / achilles_speed;
printf("%.2f", achilles_time);

unsigned int achilles_position = 1;
char sep = '\n';

for (unsigned int i = 0;  i < competitor_count;  ++i) {
if (scanf("%lf%lf", &head_start, &speed) != 2) {
fprintf(stderr, "%s:%u: input failed\n", __FILE__, __LINE__);
return 1;
}

const double time = (length - head_start) / speed;
printf("%c%.2f", sep, time);
sep = ' ';

achilles_position += time < achilles_time;
}

printf("\n%u", achilles_position);
}