Luis Fernandes - 8 months ago 38

C Question

i'm very new to C, please understand if my doubts seem stupid but i'm stuck. I have searched a lot but i could not find an answer that solved my issue.

My program is suposed to ask the user for the number of laps of a race, then asks wich time each lap took.

Then, it should state the fastest, the slowest, the average time for lap and the total time of the race. Right now, the total time and average is working. The minimum and maximum value and their location are not.

here is my code:

`#include<stdio.h>`

#include<cs50.h>

int main()

{

int array[100], maximum, minimum, c, laps, location = 1;

float average;

int summation;

printf("how many laps did the race had?\n");

laps = GetInt();

printf("how many time each of the %i laps took in seconds?\n", laps);

for (c = 0; c < laps; c++)

{

scanf("%d", &array[c]);

maximum = array[0];

minimum = array[0];

}

for ( c = 1; c < laps; c++)

{

if (array[c] < minimum)

{

minimum = array[c];

location = c + 1;

}

else if (array[c] > maximum)

{

maximum = array[c];

location = c + 1;

}

for ( c = 0; c < laps; c++)

{

summation = summation + array[c];

average = (summation / laps);

}

}

printf("The fastest lap was %d and had the time of %d seconds.\n", location, minimum);

printf("The slowest lap was %d and had the time of %d seconds.\n", location, maximum);

printf("The race took %d seconds\n", summation);

printf("The avegare time for lap was %.2f seconds.\n", average);

}

Answer

```
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
```

Should be

```
int summation = 0;
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
}
average = (summation / laps);
```

Since it's useless to compute the average before you know the whole sum

You use the same `location`

for both min and max position. Use `minLocation`

and `maxLocation`

instead

You've got a bracket issue :

```
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location = c + 1;
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
}
```

should be

```
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location = c + 1;
}
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
```