Objectives Given an array, learn to compute the sum, find minimum/maximun, etc.
ID: 3776094 • Letter: O
Question
Objectives
Given an array, learn to compute the sum, find minimum/maximun, etc.
Learn to read data from a file, then fill an array (**Study sample code ArrayInitializationWithFileReading.cpp posted on Blackboard to learn how to do this)
Learn to use parallel arrays
Learn to write functions by passing an array as input parameter
Assignment description
In this assignment, you're given a text file which contains the twelve months' rainfall statistics data for Phoenix area in year 2015, you're required to write a program that analyzes 2015 Phoenix area rainfall data. In addition to the main() function, the program should contains the following functions as well:
Function
Function Description
void getData(double array[])
This function reads rainfall statistics data from file rainFall2015.txt for each of the twelve months in year 2015 and save the data inside the relevant double array.
double getTotal(double array[])
This function computes and returns to main() function the totalRainfall
double getAverage(double array[])
This function computes and returns to main() function the averageRainfall
int getLargest(double array[])
This function computes and returns to main() function the monthIndex (month number) with the highest rainfall amounts, not the amount of rain that fell in this month.
int getSmallest(double array[])
This function computes and returns to main() function the monthIndex (month number) with the lowest rainfall amounts, not the amount of rain that fell in this month.
After above computation, your program should display the following summary rainfall report on screen (by using main() or write another displayReport() function)
=== 2015 Rain Report for Phoenix AZ ===
Total rainfall: 9.55 inches
Average monthly rainfall: 0.80 inches
The least rain fell in June with 0.11 inches.
The most rain fell in August with 1.60 inches.
Sample Run
User input in bold and enclosed with [ ]
Please enter the file's name: [rainFall2015Ver1.txt]
=== 2015 Rain Report for Phoenix AZ ===
Total rainfall: 9.55 inches
Average monthly rainfall: 0.80 inches
The least rain fell in June with 0.11 inches.
The most rain fell in August with 1.60 inches.
Misce. Instructions
Study the following sample codes posted on Blackboard
Patially fill an array by reading data from a file
Using parallel arrays
Various operation on arrays demo
Submission
1. Make sure to name your file as Assignment.cpp (Be careful of the file's name, no empty space is allowed!).
rainFall2015Ver1.txt and rainFall2015V2.txt on the following submission website.
Function
Function Description
void getData(double array[])
This function reads rainfall statistics data from file rainFall2015.txt for each of the twelve months in year 2015 and save the data inside the relevant double array.
double getTotal(double array[])
This function computes and returns to main() function the totalRainfall
double getAverage(double array[])
This function computes and returns to main() function the averageRainfall
int getLargest(double array[])
This function computes and returns to main() function the monthIndex (month number) with the highest rainfall amounts, not the amount of rain that fell in this month.
int getSmallest(double array[])
This function computes and returns to main() function the monthIndex (month number) with the lowest rainfall amounts, not the amount of rain that fell in this month.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
void getData(double array[]);
double getTotal(double array[]);
double getAverage(double array[]);
int getLargest(double array[]);
int getSmallest(double array[]);
int main() {
string month[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
double array[12];
getData(array);
cout << "=== 2015 Rain Report for Phoenix AZ ==="<<endl;
cout << "Total rainfall : "<<getTotal(array)<<" inches"<<endl;
cout << "Average monthly rainfall : "<<getAverage(array)<< " inches"<<endl;
int largest = getLargest(array);
cout << "The most rain fell in "<<month[largest]<<" with " << array[largest] << " inches"<<endl;
int smallest = getSmallest(array);
cout << "The least rain fell in "<< month[smallest]<< " with " << array[smallest] << " inches" <<endl;
}
void getData(double array[]){
int i=0;
string line;
ifstream myfile ("rainFall2015.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
double num = atof(line.c_str());
array[i++] = num;
}
myfile.close();
}
}
double getTotal(double array[]){
int i;
double s=0;
for(i=0;i<12;i++)
s += array[i];
return s;
}
double getAverage(double array[]){
return getTotal(array)/12.0;
}
int getLargest(double array[]){
double l = array[0];
int i,ind=0;
for(i=1;i<12;i++){
if(array[i]>l){
l = array[i];
ind = i;
}
}
return ind;
}
int getSmallest(double array[]){
double s = array[0];
int i,ind=0;
for(i=1;i<12;i++){
if(array[i]<s){
s = array[i];
ind = i;
}
}
return ind;
}
/*
sample output
=== 2015 Rain Report for Phoenix AZ ===
Total rainfall : 78 inches
Average monthly rainfall : 6.5 inches
The most rain fell in December with 12 inches
The least rain fell in January with 1 inches
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.