2

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

}
2
  • You forgot to initialize summation to zero. Commented Apr 2, 2015 at 8:34
  • 1
    To prevent reading 10 repeatedly with scanf add a space before the %d (e.g. scanf(" %d", &array[c]);) so you are not reading the newline in the input buffer as array[c]. (the space causes scanf to skip all whitespace, including newlines before reading the number) Commented Apr 2, 2015 at 8:34

3 Answers 3

4
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);
 }
Sign up to request clarification or add additional context in comments.

5 Comments

It should also be outside the other loop
well! thanks! now the program is successfull in pointing the max and the min! but still have the location problem. still not getting the right one
just realized now what you said! having 2 variables! on for maxlocation and another for inlocation! many thanks guys! program working! :) @Thomas and Jack too (it seems i can only mark one person)
@LuisFernandes, yes, you can only mark one person, but since this is my post, I'll be notified no matter what. (Oh, and don't forget to vote up all the answers that helped you)
i need reputation to be able to upvote... going to wotk on it and i will return after i am able to do it
3

Place the for() loop for minimum and maximum separately

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];
      min_location = c + 1;//Change this also
   }
   else if (array[c] > maximum)
   {
       maximum = array[c];
       max_location = c + 1;//use separate variable
   }
 }//Don't nest the summation inside this
 for ( c = 0; c < laps; c++)
 {
  summation = summation + array[c];
  average = (summation / laps);
 }

Initialize summation as 0 int summation=0 in start to avoid the garbage value also been included . Also use separate variable for minimum and maximum location

1 Comment

yeap! thats sums it all! :)
1

For starters, change this

 for (c = 0; c < laps; c++)
 {
      scanf("%d", &array[c]);
      maximum = array[0];
      minimum = array[0];
 }

to this:

 for (c = 0; c < laps; c++)
 {
      scanf("%d", &array[c]);
 }
 maximum = array[0];
 minimum = array[0];

It's not producing a wrong output, but I'll explain your mistake later.

Take a look at this now:

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

You have (and I'm sure you haven't noticed that) one for loop inside an other, using the same counter. I don't know why you would do something like it. This will produce a logical error, since c is changing internally, something not supposed to be happening in a simple for loop like this. You can see what happens in the iteration by printing c each time and you'll see weird things happen.

Let's try this approach, where we change this:

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

to this:

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

You only need to divide by the number of laps only when you have the whole summation and the whole number of laps available. That's why I place it outside a loop. Like the very first comment, it will not produce an error, it's just something not needed to be done.

So, the final code piece would be:

#include<stdio.h>
#include<cs50.h>

int main()
{
int array[100], maximum, minimum, c, laps, location_min, location_max = 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];
 summation = array[0];
 for ( c = 1; c < laps; c++)
 {
      summation = summation + array[c];
      if (array[c] < minimum)
      {
          minimum = array[c];
          location_min = c + 1;
       }
       else if (array[c] > maximum)
       {
           maximum = array[c];
           location_max = c + 1;
       }
 }
 average = (summation / laps);

printf("The fastest lap was %d and had the time of %d seconds.\n", location_min, minimum);
printf("The slowest lap was %d and had the time of %d seconds.\n", location_max, maximum);
printf("The race took %d seconds\n", summation);
printf("The avegare time for lap was %.2f seconds.\n", average);

}

5 Comments

well... my summation and average where the only things working and now they are not working... so, not sure about the code you suggested. but thanks for trying to help!
I'm sorry about that, I have fixed it, I didn't notice your loop started from c=1
i have solved the for inside a for loop thing! i have solved the part where the program was failing to give me the fastest and the slowest lap. the only thing missing now is getting it to tell me wich lap was the fastest and the slowest. :) one step at a time.
Yeah, I haven't noticed you were using the same variable for the index of the maximum and the minimum values, try the fix (I hope it's the last one). I need some coffee
already working! i had the sae name for maxlocation and minlocation. i added a second variable and now its perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.