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);
}
summationto zero.10repeatedly withscanfadd a space before the%d(e.g.scanf(" %d", &array[c]);) so you are not reading thenewlinein the input buffer asarray[c]. (the space causesscanfto skip all whitespace, includingnewlinesbefore reading the number)