Programming languages can be used to aid many scientific fields, including meteorology. This is the study of weather patterns, and it generates tremendous sums of data, which are difficult to appreciate in their raw form. Using a programming language like C++ can aid meteorologists in transforming data into more useful forms. For example, a program could calculate the average rainfall over the span of three months given daily rainfall measurements, which would allow a meteorologist to observe weather trends.
Instructions
1. Load the C++ IDE and start a new project. This will create a blank source-code file in the main window.
2. Create a main function. The program will reside within this function. You can write a main function by typing this:
int main()
{
}
3. Create a value that holds the number of days that occur in the three-month span you are interested in. Suppose the span was Oct-Nov-Dec, or 92 days. You would write this, inside the curly brackets of the main function:
int numberOfDays = 92;
4. Create an array that holds the daily rainfall values for three months. You will use the data type "float" to store the values, and the array will store as many elements as the variable "numberOfDays" holds. The "float" type is useful for numbers with decimal points. You can define the array by writing something like this below the previous line:
float rainfallThreeMonths[numberOfDays];
5. Assign the daily rainfall values to the array. The following line illustrates assign 10 values to an array. You will want to add all the values over the course of three months to the array defined in the previous step.
float rainfallTenDays[10] = {0.0, 0.0, 0.3, 0.5, 0.1, 0.0, 1.2, 0.4, 0.0, 0.0};
6. Define a variable that will hold the sum total rainfall over the course of the three month span, like this:
float sum = 0.0;
7. Iterate through the array and add all the values together. A straightforward approach is to use a for loop, like this:
for(int i = 0; i < numberOfDays; i++)
{
}
8. Write the arithmetic operation to sum all the values together by writing the following line within the for loop:
sum += rainfallThreeMonths[i];
9. Solve the average rainfall by dividing the "sum" variable by the "numberOfDays" variable. Write this in the line after the "}" bracket of the for loop:
float averageRainfall = sum / numberOfDays;
10. Print out the value calculated in the previous step by writing something like this:
printf("Average Rainfall for %d day: %f inches", numberOfDays, averageRainfall);
Tags: like this, daily rainfall, main function, values array, writing something like