professor asked for : Decimal values should be displayed using default precision
ID: 3806049 • Letter: P
Question
professor asked for :
Decimal values should be displayed using default precision, i.e. do not specify precision.
what does he mean ? please tell me what im supposed to change in my program HELPPP
//Headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
double rainfallTotal(double[], int);
double averageMonthlyRainfall(double[], int);
double highestAmountRainfall(double[], int, int &);
double lowestAmountRainfall(double[], int, int &);
int main()
{
//constant gregorian calendar
const int ALL_MONTHS = 12;
//2 variables
int highIndex = 0;
int lowIndex = 0;
//Declare variables
double totalRf;
double averageRf;
double mostRf;
double leastRf;
double monthlyRf[ALL_MONTHS];
//array containing containing the months
string monthName[ALL_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for (int i = 0; i < ALL_MONTHS; i++)
{
cout << "Enter rainfall for " << monthName[i] << ": ";
cin >> monthlyRf[i];
//not allowing the user to insert negative numbers
//giving them a second chance to retry the month that had a negative value
while (monthlyRf[i] < 0)
{
cout << "Invalid data (negative rainfall)--retry for the month of " <<monthName[i] << ":" << endl;
cin >> monthlyRf[i];
}
}
//Calling the methods
totalRf = rainfallTotal(monthlyRf, ALL_MONTHS);
averageRf = averageMonthlyRainfall(monthlyRf, ALL_MONTHS);
mostRf = highestAmountRainfall(monthlyRf, ALL_MONTHS, highIndex);
leastRf = lowestAmountRainfall(monthlyRf, ALL_MONTHS, lowIndex);
//displaying all the outputs
//with setprecision
cout << fixed << showpoint << setprecision(3) << endl;
cout << "Total rainfall: " << totalRf << endl;
cout << "Average rainfall: " << averageRf << endl;
cout << "Least rainfall in " << monthName[lowIndex] <<endl;
cout << "Most rainfall in " << monthName[highIndex] <<endl;
system("pause");
return 0;
}
//Method definition of rainfallTotal
double rainfallTotal(double totalRainfall[], int n)
{
double total = 0;
for (int i = 0; i < n; i++)
total += totalRainfall[i];
return total;
}
//Method definition of averageMonthlyRainfall
double averageMonthlyRainfall(double totalRainfall[], int n)
{
double average = 0.0;
double total = 0;
for (int i = 0; i < n; i++)
total += totalRainfall[i];
//calculate average
average = total / n;
return average;
}
//Method definition of lowestAmountRainfall
double lowestAmountRainfall(double totalRainfall[], int n, int &monthIndex)
{
double least;
int i = 0;
least = totalRainfall[i];
while (i < n)
{
if (totalRainfall[i] < least)
{
least = totalRainfall[i];
monthIndex = i;
}
i++;
}
return least;
}
//Method definition of highestAmountRainfall
double highestAmountRainfall(double totalRainfall[], int n, int &monthIndex)
{
double high;
int i = 0;
high = totalRainfall[i];
while (i < n)
{
if (totalRainfall[i] > high)
{
high = totalRainfall[i];
monthIndex = i;
}
i++;
}
return high;
}
Explanation / Answer
Hi
I have removed the below two lines from the code which are not required as per your requriement.
#include <iomanip>
//with setprecision
cout << fixed << showpoint << setprecision(3) << endl;
Please find the below updated code.
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
double rainfallTotal(double[], int);
double averageMonthlyRainfall(double[], int);
double highestAmountRainfall(double[], int, int &);
double lowestAmountRainfall(double[], int, int &);
int main()
{
//constant gregorian calendar
const int ALL_MONTHS = 12;
//2 variables
int highIndex = 0;
int lowIndex = 0;
//Declare variables
double totalRf;
double averageRf;
double mostRf;
double leastRf;
double monthlyRf[ALL_MONTHS];
//array containing containing the months
string monthName[ALL_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for (int i = 0; i < ALL_MONTHS; i++)
{
cout << "Enter rainfall for " << monthName[i] << ": ";
cin >> monthlyRf[i];
//not allowing the user to insert negative numbers
//giving them a second chance to retry the month that had a negative value
while (monthlyRf[i] < 0)
{
cout << "Invalid data (negative rainfall)--retry for the month of " <<monthName[i] << ":" << endl;
cin >> monthlyRf[i];
}
}
//Calling the methods
totalRf = rainfallTotal(monthlyRf, ALL_MONTHS);
averageRf = averageMonthlyRainfall(monthlyRf, ALL_MONTHS);
mostRf = highestAmountRainfall(monthlyRf, ALL_MONTHS, highIndex);
leastRf = lowestAmountRainfall(monthlyRf, ALL_MONTHS, lowIndex);
//displaying all the outputs
cout << "Total rainfall: " << totalRf << endl;
cout << "Average rainfall: " << averageRf << endl;
cout << "Least rainfall in " << monthName[lowIndex] <<endl;
cout << "Most rainfall in " << monthName[highIndex] <<endl;
//system("pause");
return 0;
}
//Method definition of rainfallTotal
double rainfallTotal(double totalRainfall[], int n)
{
double total = 0;
for (int i = 0; i < n; i++)
total += totalRainfall[i];
return total;
}
//Method definition of averageMonthlyRainfall
double averageMonthlyRainfall(double totalRainfall[], int n)
{
double average = 0.0;
double total = 0;
for (int i = 0; i < n; i++)
total += totalRainfall[i];
//calculate average
average = total / n;
return average;
}
//Method definition of lowestAmountRainfall
double lowestAmountRainfall(double totalRainfall[], int n, int &monthIndex)
{
double least;
int i = 0;
least = totalRainfall[i];
while (i < n)
{
if (totalRainfall[i] < least)
{
least = totalRainfall[i];
monthIndex = i;
}
i++;
}
return least;
}
//Method definition of highestAmountRainfall
double highestAmountRainfall(double totalRainfall[], int n, int &monthIndex)
{
double high;
int i = 0;
high = totalRainfall[i];
while (i < n)
{
if (totalRainfall[i] > high)
{
high = totalRainfall[i];
monthIndex = i;
}
i++;
}
return high;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter rainfall for January: 11
Enter rainfall for February: 22
Enter rainfall for March: 33
Enter rainfall for April: 44
Enter rainfall for May: 55
Enter rainfall for June: 66
Enter rainfall for July: 77
Enter rainfall for August: 88
Enter rainfall for September: 99
Enter rainfall for October: 12
Enter rainfall for November: 13
Enter rainfall for December: 14
Total rainfall: 534
Average rainfall: 44.5
Least rainfall in January
Most rainfall in September
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.