Question

Write a program that scores the total rainfall for each of 12 months into an array...

Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7};

The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain
Output:


What to deliver? Your .cpp file including: pseudocode in the beginning of the code, code with other appropriate comments and output at the bottom of your program. Code: 60 points pseudocode: 20 points, Comments: 10 points, output: 10 points


Total Points (100 pts)
The total rainfall for this year is 35 The average rainfall for this year is 2.91667 The month with the highest amount of rain is 10 with 4.3 inches. The month with the lowest amount of rain is 1 with 1.6 inches.

Extra credit: 10 points Instead of initializing the array as above, read the values for the array ‘thisYear[]” from the attached input file rainfall.txt. First read in the values from the array and then do the same as above. Submit that as second problem.

Output: Here’s how the output should look:


1.6 2.1 1.7 3.5 2.6 3.7 3.9 2.6 2.9 4.3 2.4 3.7 The total rainfall for this year is 35 The average rainfall for this year is 2.91667 The month with the highest amount of rain is 10 with 4.3 inches. The month with the lowest amount of rain is 1 with 1.6 inches.

.txt file

1.6 2.1 1.7 3.5 2.6 3.7 3.9 2.6 2.9 4.3 2.4 3.7

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/*
PSEUDOCODE:-

Pseudo code of program

totalRain(){
calculate sum of all values in array
returns the sum
}
  
averageRain(){
calculate average of elements in array
returns the average
}
  
leastRain(){
consider the first number to be minimum
for each of the rest of the number
if it is minimum then the current minimum
assign to minimum
assign index of minimum
returns index of minimum element
}
  
mostRain(){
consider the first number to be maximum
for each of the rest of the number
if it is larger then the current maximum
assign to maximum
assign index of maximum
returns index of maximum
}

{
In the main function

print elementsof array
print"The total rainfall for this year is " and call totalRain()
print"The average rainfall for this year is "and call averageRain()
Call mostRain() and store the value return in indexmost
print"The month with the highest amount of rain is "
and print indexmost and value stored in indexmost
Call leastRain() and store the value return in indexLeast
print"The month with the least amount of rain is "
and print indexLeastand value stored in indexLeast
}

*/

CODE OF THE GIVEN PROGRAM:-

#include <iostream>
#include <fstream>
using namespace std;

//Functioin to calculate total rain
double totalRain(double arr[],int n){
double sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
return sum;
}

//function to calculate average rain
double averageRain(double arr[],int n){
double avg=0;
for(int i=0;i<n;i++){
avg+=arr[i];
}
return (avg/12);
}

//function to calculate least rain
int leastRain(double arr[],int n){
double small=arr[0];
int index=0,i;
   for(i=0; i<n; i++)
   {
       if(small>arr[i])
       {
           small=arr[i];
           index =i;
       }
   }
  
return index;
}

//function to calculatemost rain
int mostRain(double arr[],int n){
double max=arr[0];
int i,index=0;
for(i=0;i<n;i++){
if(max<arr[i]) {
max=arr[i];
index=i;
}
}
return index;
}

int main()
{
double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7};
int size =12;
  
/* To enter values in array through text file use this code

ifstream inputFile; //Input file stream object
inputFile.open("rain.txt"); //Opening the file
//Read numbers from file into the array
for(int i= 0;i < size; i++){
inputFile >> thisYear[i];
} */
// Display the elements in array
for(int j= 0;j < size; j++){
cout<<thisYear[j]<<" ";
}
cout<<"\nThe total rainfall for this year is "<<totalRain(thisYear,size)<<endl;
cout<<"The average rainfall for this year is "<<averageRain(thisYear,size)<<endl;
int indexMost=mostRain(thisYear,size);// to store index of month with most rain
cout<<"The month with the highest amount of rain is "<<indexMost+1<<" with "<<thisYear[indexMost]<<" inches."<<endl;
int indexLeast=leastRain(thisYear,size);//to store index of month with leastrain
cout<<"The month with the least amount of rain is "<<indexLeast+1<<" with "<<thisYear[indexLeast]<<" inches."<<endl;
//Close the file
//inputFile.close();
return 0;
}

SCREENSHOTS OF CODE AND SAMPLE OUTPUT:-

SAMPLE OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
Write a program that scores the total rainfall for each of 12 months into an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a program that lets the user enter the total rainfall for each of 12 months...

    Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amount. PLEASE MODULARIZED THE CODE   PLEASE USE C PROGRAMMING AND ADD PSEUDOCODE

  • Recent research indicates that the effectiveness of antidepressant medication is directly related to the severity of...

    Recent research indicates that the effectiveness of antidepressant medication is directly related to the severity of the depression (Khan, Brodhead, Kolts & Brown, 2005). Based on pretreatment depression scores, patients were divided into four groups based on their level of depression. After receiving the antidepressant medication, depression scores were measured again and the amount of improvement was recorded for each patient. The following data are similar to the results of the study. Low Moderate High Moderate Moderately Severe Severe 1.2...

  • In Java language: Write a rainfall class that stores the total rainfall for each of 12...

    In Java language: Write a rainfall class that stores the total rainfall for each of 12 months of years 2015 -2017 into an 2D array of doubles. Use the text file at Actividades/Tareas link as input of rainfall data. The program should read the input data from a text file. The program should have methods that at least return the following: • Total rainfall for the years 2015 – 2017 - return a 1D array • The average monthly rainfall...

  • [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...

    [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall...

  • Write a program using the random number generator to create random rainfall measurements and save these...

    Write a program using the random number generator to create random rainfall measurements and save these numbers in a text file. You will have more than 12 readings to cover more than 1 year of measurements. You may assume that we do not have more than 5 inches of rain in a month. Your program will work with a text file of any number of months (you do not know how many readings are contained in the input file). Using...

  • A compound A, C7H12O3, contains two functional groups. One of the functional groups is a derivative...

    A compound A, C7H12O3, contains two functional groups. One of the functional groups is a derivative of carboxylic acid. The 1H NMR spectrum of the compound is given below. Deduce the structure of the compound A. Write your structure on the spectrum and using arrows assign all the peaks in the spectrum to various protons in the molecule. (5 Pts, NO PARTIAL CREDITS) 4.3 4.2 4.1 4.0 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0 2.9 2.8 2.7...

  • Write a Python program that allows the user to enter the total rainfall for each of...

    Write a Python program that allows the user to enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January: 8.9 inches February: 11.1 inches March: 13.4 inches April: 6.9 inches May: 8.7 inches June: 9.1 inches July: 15.9 inches August: 4.4 inches September: 3.1 inches October: 5.6 inches November: 7.8...

  • The Problem Design a program that lets the user enter the total rainfall for each of...

    The Problem Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the number of months above and below the average rainfall, and the highest rainfall for each quarter of the year. The Assignment Write a Java program that contains five method/functions as follows: main(). Calls the functions enterRain, rainStats, aboveBelow, and quarterlyRain. enterRain(). Uses...

  • Rainfall Statistics Write a program that lets the user enter the total rainfall for each of...

    Rainfall Statistics Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December....

  • Write a program in C++ that lets the user enter the total rainfall for each of...

    Write a program in C++ that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Input validation: Do not accept negative numbers for monthly rainfall figures.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT