0

I'm trying to make a program that asks how many days have you practiced, and then for each day ask how many flights you made and then determine the average of those flights for each day, this is my code right now:

#include <stdio.h>

int main() {
    int days, flights, i;
    double length, total, average;

printf("How many days have you been practicing?\n");
scanf("%d", &days);

for(i=1; i<=days; i++) {
    printf("How many flights were completed in day #%d?\n", i);
    scanf("%d", &flights);
        for(i=1; i<=flights; i++){
            printf("How long was flight #%d?\n", i++);
            scanf("%lf", &length);
            length += total;
            average = total / flights;
            printf("Day #%d: The average distance is %.3lf\n", i, average);
            }
    }
    return 0;
}

Right now I get to the part where I enter how long flight #1 was, but when I input a value the program stops working and I can't continue so I'm not sure if I have any other problems after that. Any help would be greatly appreciated as I am new to this! As an example the final output should look something like this:

How many days have you been practicing?

2

How many flights were completed in day #1?

2

How long was flight #1?

5.00

How long was flight #2?

10.00

Day #1: The average distance is 7.500.

How many flights were completed in day #2?

3

How long was flight #1?

7.50

How long was flight #2?

13.00

How long was flight #3?

15.75

Day #2: The average distance is 12.083.

2 Answers 2

2

You use the same variable (i) in both loops (days & flights), thus overriding it in the inner loop, use a different variable and you should be set.

Edit:

Also, you should probably change the line:

printf("How long was flight #%d?\n", i++);

To:

printf("How long was flight #%d?\n", i+1);
Sign up to request clarification or add additional context in comments.

3 Comments

I changed the variable in the second loop to "j" but the program still fails at the same part when I input how long flight #1 was for day #1
please see my edit. I kept it i in the edit, but it should be the flight counter, (probably j)
Okay I did that but still received the same error (program has stopped working) and now for the first flight of day #1 it says flight #2 instead of flight #1
0
  • Yor were made some mistakes in your code.I have pasted modified and correct code below and also mention comments where you had made mistake so that you can easily understand.

    #include <stdio.h>
    
    int main() 
    {
      int days, flights, i,j;
      double length, total = 0, average;
    
      printf("How many days have you been practicing?\n");
      scanf("%d", &days);
    
      for(i=1; i<=days; i++)
      {
          printf("How many flights were completed in day #%d?\n", i);
          scanf("%d", &flights);
          for(j=1; j<=flights; j++)
          {
              //in this loop you are using same int i which is already used in first for loop and alo u r incrementig it.
             // so need to use another int variable.
             printf("How long was flight #%d?\n", j);
             scanf("%lf", &length);
            // u r copying total to length ,but total is not initialized anywhere.
             // it should be like this.
             total += length;
          }
         // and u have to print average value after completion of loop.
    total = total/flights;
    printf("Day #%d: The average distance is %.3lf\n", i, total);
    total = 0;
      }    
     return 0;
    }
    
  • Hope this will help you.

Comments

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.