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:
- average rain multiplied by 10,
- 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.