1

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 an example output looks 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

Day #3: The average distance is 0.000.

How many flights were completed in day #2?

3

How long was flight #1?

7.50

How long was flight #3?

13.00

Day #5: The average distance is 0.000.

As you can see there are multiple problems with the output right now, the average distance is not calculating, I am not able to input numbers for each flight, and the flight and day #'s are incorrect. 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.

1
  • You should check the return value from scanf() to ensure you get the expected number of values (1) on each call. If you don't, you may have a problem, especially if the user typed a letter instead of a digit. Commented Sep 24, 2015 at 3:35

4 Answers 4

2

There were several problems, I left a comment for each fix I made. Let me know if you have questions

#include <stdio.h>

int main() {
    int days, flights, i, j;
    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);

        // Reset total
        total = 0.0;

        // Use a different index variable than i, I used j
        for (j = 1; j <= flights; j++) {
            // Do not increment i here (I removed i++)
            printf("How long was flight #%d?\n", j);
            scanf("%lf", &length);

            // This was probably backwards, you never assigned total
            //length += total;
            total += length;
        }

        // Compute the average per day outside the flights loop
        average = total / flights;
        printf("Day #%d: The average distance is %.3lf\n", i, average);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This sets the total to 0.0 correctly; the other answers don't yet do that. Good one.
Thank you very much, this was extremely helpful and I see all of the mistakes I made
0

I fixed your code. You can't use the same variable in nested "for" loops since the second use of the variable confuses the first use of the same variable. For this reason, I added a counter variable called J.

#include <stdio.h>

int main() {
int days, flights, i,j;
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(j=1; j<=flights; j++){
        printf("How long was flight #%d?\n", i++);
        scanf("%lf", &length);
        total += length;
    }
    average = total / flights;
    printf("Day #%d: The average distance is %.3lf\n", i, average);
}
return 0;
}

Comments

0

Use a different loop counter for the inner for loop. E.g. use j instead of i.

for(int j=1; j<=flights; j++){

You also need to move the following lines from the inner loop.

average = total / flights;
printf("Day #%d: The average distance is %.3lf\n", i, average);

They should be right after the inner loop.

1 Comment

Whoops, I forgot to change that in the post but I have actually done that in the code already and am still getting all of the same problems.
0

Please note that you have used same iteration variable for both the loops:

 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++){
        ...

Use another variable j:

 for(i=1; i<=days; i++) {
    printf("How many flights were completed in day #%d?\n", i);
    scanf("%d", &flights);
    for(int j=1; j<=flights; j++){
        ...

Also, you need to correct the business logic as follows:

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

}

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.