Thread: sentinel controlled loop with arrays ... help please

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    9

    sentinel controlled loop with arrays ... help please

    Hello again. I am having some trouble with my assignment here. I feel like I am close, but I am missing a part of it. Here are the instructions:

    In hurricane season, we would like to keep track of the severity of the weather. We will invent a measure of weather severity that combines the wind speed, and rain fall.

    Ask the user to enter data on the wind and rain for several days ending the program with -1

    The program will use a sentinel controlled loop, so the program will count the number of days entered, and that count is included in the output.

    Store the wind and rain values in 2 arrays. There will not be more than 20 numbers entered.

    The weather severity will be calculated by adding:

    1. average rain multiplied by 10,
    2. The average wind.


    For example, if the average rain is 0.5 inches, and the average wind is 30 mph, the weather severity will be ( 0.5 x 10 ) + 30.0 = 35.0

    The program will output the average rain, and average wind. It will also output the number of days for which data were entered, and the weather severity.

    The bolded part is where I am struggling. I cannot figure out how to incorporate that counter into my program while using an array. Maybe I have just been staring at my screen too long and need to step away, but this is driving me crazy. Any help would be appreciated.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define SIZE 10
    
    
    double getRainfall() {
        double result;
    
    
        printf("\nEnter a rainfall amount (-1 to quit):  ");
        scanf_s("%lf", &result);
    
    
        while (result ==  -1) {
            break; /*this is where i tried to do something i thought
    might work... it didnt.*/ 
        
        }
        
        return result;
    }
    
    
    double calculateTotalRain(double rainfall[], int size) {
        double result = 0.0;
        int i;
        for (i = 0; i < size; i++) {
            result += rainfall[i];
        
                }
        return result;
    }
    
    
    double getWind() {
        double result;
    
    
        printf("\nEnter wind speed:  ");
        scanf_s("%lf", &result);
    
    
        while (result < 0.0) {
            printf("%.2lf is not a wind speed.", result);
            printf("\nEnter another wind speed:  ");
            scanf_s("%lf", &result);
    
    
        }
    
    
        return result;
    }
    double calculateTotalWind(double wind[], int size) {
        double result = 0.0;
        int i;
        for (i = 0; i < size; i++) {
            result += wind[i];
            
    
    
        }
        return result;
    }
    
    
    double findSmallest(double rainfall[], int size) {
        double result = 0.0;
        int i;
        for (i = 0; i < SIZE; i++) {
            if (rainfall[0] > result)
                result = rainfall[i];
    
    
        }
        return result;
    }
    
    
    
    
    main() {
        double rainfall[SIZE] = { 0 }, totalRainfall, averageRainfall;
        double wind[SIZE] = { 0 }, totalWind, averageWind, weatherSeverity;
        int i;
    
    
    
    
        for (i = 0; i < SIZE; i++)
            rainfall[i] = getRainfall();
    
    
        for (i = 0; i < SIZE; i++)
            wind[i] = getWind();
    
    
        totalRainfall = calculateTotalRain(rainfall, SIZE);
        averageRainfall = totalRainfall / SIZE;
        totalWind = calculateTotalWind(wind, SIZE);
        averageWind = totalWind / SIZE;
        weatherSeverity = (averageRainfall * 10) + averageWind;
    
    
        printf("For these %i days, the average rainfall is %.2lf\n", SIZE, averageRainfall);
        printf("The average wind speed is %.2lf\n", averageWind);
        printf("The weather severity is %.2lf\n", weatherSeverity);
    
    
    
    
        system("pause");
    }
    Any kind of guidance will be greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    #include <stdio.h>
    
    #define SIZE 20
    
    int getRainfall(double *rain) {
        int size = 0;
        while (1) {
            double n;
            if (scanf("%lf", &n) != 1 || n == -1)
                break;
            rain[size++] = n;
        }
        return size;
    }
    
    int main() {
        double rain[SIZE];
        int size = getRainfall(rain);
        for (int i = 0; i < size; i++)
            printf("%6.2f\n", rain[i]);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    You can't use == with a double because you are comparing to something like -0.99999999999999999999999 instead of -1. But DBL_EPSILON is defined in float.h, maybe someone here can explain how to make use of that?
    --edit--
    Oh, I need to wake up. Rainfall amount can't be less than zero, so you could just test for < 0.0 instead of looking for a maximum relative difference.
    --edit 2--
    Can the computer have a rounding error on 0.0? If so, then I suppose to allow someone to enter an extremely miniscule amount of rainfall, you'd better test for something like < -0.001 or such.
    Last edited by christophergray; 04-28-2018 at 07:03 PM. Reason: sleepyness

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You can't use == with a double because you are comparing to something like -0.99999999999999999999999 instead of -1

    As long as the value assigned to the double is an integer that can be represented in the number of bits (+1) allocated for the mantissa of the floating-point representation, then that integer will be stored perfectly. You're thinking of non-integer values like 0.1.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    IF you receive the sentinel, THEN you want to break out of the FOR loop that is filling your rainfall array. The BREAK needs to be inside your FOR loop. So move the FOR loop, or move the BREAK.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by christophergray View Post
    IF you receive the sentinel, THEN you want to break out of the FOR loop that is filling your rainfall array. The BREAK needs to be inside your FOR loop. So move the FOR loop, or move the BREAK.
    What code are you looking at???
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by john.c View Post
    What code are you looking at???
    The original poster's code, lines 14, 89-90.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with sentinel controlled loop
    By n.jimenez in forum C Programming
    Replies: 5
    Last Post: 10-10-2012, 02:55 PM
  2. sentinel controlled do-while loop
    By theCanuck in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2011, 11:07 PM
  3. Sentinel controlled loop
    By arjay in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 04:30 AM
  4. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  5. Sentinel-Controlled repetition
    By Hybrid in forum C Programming
    Replies: 7
    Last Post: 02-07-2008, 09:43 PM

Tags for this Thread