Problem with homework

This is a discussion on Problem with homework within the C Programming forums, part of the General Programming Boards category; quzah, i thought that originally, but just before the while loop fscan changes the value of dummyMonth to the first ...

  1. #16
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    quzah, i thought that originally, but just before the while loop fscan changes the value of dummyMonth to the first integer in the user inputted text file.

    Code:
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
        {
           for (dummyDay=0; dummyDay < 31; dummyDay++)
                weatherArray[dummyMonth][dummyDay] = 0;
        }
    
    // Read temperature file and store temperature averages to array
    
    fscanf(ifp, "%d", &dummyMonth);
    
    while (dummyMonth != -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            printf("%d - ", dummyMonth);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (weatherArray [dummyMonth][dummyDay] + tempTemp) / 2.0;
                printf("%d - %.2f\n", dummyDay, tempTemp);
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", &dummyMonth);
        }
    Common, if you're going to do it, you might as well do it right

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Just got the program to not break... and you led me in the right direction.

    Code:
    fscanf(ifp, "%d", &dummyMonth);
    
    while (dummyMonth != -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            printf("%d - ", dummyMonth);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (weatherArray [dummyMonth][dummyDay] + tempTemp) / 2.0;
                printf("%d - %.2f\n", dummyDay, tempTemp);
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", &dummyMonth);
        }
    The weather array is where it was breaking. I changed

    Code:
    weatherArray [dummyMonth][dummyDay] = tempAverage;
    to
    Code:
    weatherArray [dummyMonth-1][dummyDay-1] = tempAverage;
    So now I just need to try test values to make sure it giving me the correct calculations.

  3. #18
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    The problem now is in the calculation of the average temp on the particular day chosen and the average temp in the month chosen.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Break it down - are you getting the summation correct? Then division is the problem.

  5. #20
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    The average temp on a particular day and the average temperature for that month are calculating to be the same... oh man.

  6. #21
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    This is my code at its current state. The average for the day and the average for the month keep calculating to be the same, which makes no sense. Formula wise they should be different.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    // Declare variables
    
    char cityFileName[20];
    int month, day, dummyMonth, dummyDay, dummyVar;
    float weatherArray[12][31], tempTemp, tempAverage;
    
    // Prompt user with questions
    
    printf ("What is the month and day of your wedding?\n");
    scanf("%d %d", &month, &day);
    
    // Adjusting month and day values to align with array
    
    month = month - 1;
    day = day - 1;
    
    printf("What file stores your city's temperature data?\n");
    scanf("%s", cityFileName);
    
    // Program will open the filename the user inputted
    
    FILE *ifp = fopen(cityFileName,"r");
    
    if (! ifp)
        {
            printf("The file did not open\n");
            exit (255);
        }
    
    // Initialize the weather array to recieve temperature averages
    
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
        {
           for (dummyDay=0; dummyDay < 31; dummyDay++)
                weatherArray[dummyMonth][dummyDay] = 0;
        }
    
    // Read temperature file and store temperature averages to array
    
    fscanf(ifp, "%d", &dummyMonth);
    
    while (dummyMonth != -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
    
            if (weatherArray[dummyMonth-1][dummyDay-1] == 0)
                weatherArray[dummyMonth-1][dummyDay-1] = tempTemp;
    
            if (tempTemp != -99)
            {
                tempAverage = (weatherArray [dummyMonth-1][dummyDay-1] + tempTemp) / 2;
                weatherArray [dummyMonth-1][dummyDay-1] = tempAverage;
                //printf("%d - %d - %d - %.2f\n", dummyMonth, dummyDay, dummyVar, weatherArray [dummyMonth-1][dummyDay-1]);
            }
            fscanf(ifp, "%d", &dummyMonth);
        }
    
    
    // Calculate the average temperature for the month of the chosen wedding date
    
    int monthAverageTemp = weatherArray[month][0];
    
        for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                if (weatherArray[month][dummyDay] != 0)
                {
                    monthAverageTemp = (monthAverageTemp + weatherArray[month][dummyDay]) / 2;
                    printf("\n%.2f\n", weatherArray[month][dummyDay]);
                }
            }
    
    // Print calculations to the user
    
    printf("\nThe average temperature on your wedding day is %.2f degrees F.\n",
           weatherArray[month][day]);
    
    printf("The average temperature on your wedding month is %.2f degrees F.\n",
           monthAverageTemp);
    
    // Condition that states whether wedding should be held outside or not
    
    if ((weatherArray[month][day] >= 60) && (weatherArray [month][day] <= 75))
        printf("The weather looks good for an outdoor wedding.\n");
    else
        printf("It is probably best to move the wedding indoors, sorry.\n");
    
    return 0;
    
    }

  7. #22
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    monthAverageTemp needs to be a float for one.

    This is the sample info I was given:

    What is the month and day of your wedding?
    3 3
    What file stores your city’s temperature data?
    FLORLAND.txt
    Sample Output
    The average temperature on your wedding day is 64.15 degrees F.
    The average temperature on your wedding month is 66.46 degrees F.
    The weather looks good for an outdoor wedding!

    My program is giving an average temp on the wedding days as 61.01 and the average for the month as 68.31.

    However when I use smaller test data, the program is calculating what I'm calculating by hand.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework problem Please Help!
    By alexwink in forum C Programming
    Replies: 24
    Last Post: 10-27-2006, 08:20 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 04:39 PM
  4. homework problem
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2002, 07:12 PM
  5. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 07:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21