c++ arrays using for loops to output rainfall statistics
Write a program that reads in the rainfall for each of 12 months and outputs the month and the rainfall for the month. It should then output the month with the most rainfall, the month with the least rainfall, the total amount of rainfall for the year, and the average monthly rainfall. All rainfall amounts should be output with 2 significant digits after the decimal point.
The program should incorporate two parallel arrays: month of type string containing the month names, and rainfall of type double containing the rainfall in inches for the corresponding month.
Hint: Check slides 8 thru 10 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for examples.
The program should read in the rainfall amount for each month using a for loop.
The program should output the month right justified in a field width of 10 and the corresponding rainfall right justified in a field width of 6.
The program should calculate and display the total rainfall for the year and the average monthly rainfall.
The program should calculate the highest and lowest amounts and display the amounts and the corresponding month names.
Hint 1: Check slides 3 and 4 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for the code to find the lowest and the highest.
Hint 2: In addition, you will have to keep track of the index at which you found the highest and the lowest amount. Output the month name corresponding to the index. For example, if font rainfall[3] is the lowest rainfall, you will print font month[3] as the corresponding month.
Validation: Do not accept negative numbers for monthly rainfall figures.
When the input is as shown in Figure 1, your program should produce the output as shown in Figure 2.
Figure 1: (input)
3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3
Figure 2: (output)
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
this is my code.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfall [SIZE];
double highest;
double lowest;
double total, average;
cin >> rainfall[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[SIZE] << right <<setw(6) << rainfall[i] << endl;
}
for ( int i = 0; i < SIZE; i++)
{
if (rainfall[i] > highest)
{
highest = rainfall[i];
}
}
for (int i = 0; i < SIZE; i++)
{
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
}
}
for(int i = 0; i < SIZE; i++)
{
total = rainfall[0] + rainfall[1] + rainfall[2] + rainfall[3] + rainfall[4];
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
}
for(int i = 0; i << SIZE; i++)
{
average = total / SIZE;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
}
return 0;
}
c++
|
show 2 more comments
Write a program that reads in the rainfall for each of 12 months and outputs the month and the rainfall for the month. It should then output the month with the most rainfall, the month with the least rainfall, the total amount of rainfall for the year, and the average monthly rainfall. All rainfall amounts should be output with 2 significant digits after the decimal point.
The program should incorporate two parallel arrays: month of type string containing the month names, and rainfall of type double containing the rainfall in inches for the corresponding month.
Hint: Check slides 8 thru 10 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for examples.
The program should read in the rainfall amount for each month using a for loop.
The program should output the month right justified in a field width of 10 and the corresponding rainfall right justified in a field width of 6.
The program should calculate and display the total rainfall for the year and the average monthly rainfall.
The program should calculate the highest and lowest amounts and display the amounts and the corresponding month names.
Hint 1: Check slides 3 and 4 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for the code to find the lowest and the highest.
Hint 2: In addition, you will have to keep track of the index at which you found the highest and the lowest amount. Output the month name corresponding to the index. For example, if font rainfall[3] is the lowest rainfall, you will print font month[3] as the corresponding month.
Validation: Do not accept negative numbers for monthly rainfall figures.
When the input is as shown in Figure 1, your program should produce the output as shown in Figure 2.
Figure 1: (input)
3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3
Figure 2: (output)
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
this is my code.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfall [SIZE];
double highest;
double lowest;
double total, average;
cin >> rainfall[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[SIZE] << right <<setw(6) << rainfall[i] << endl;
}
for ( int i = 0; i < SIZE; i++)
{
if (rainfall[i] > highest)
{
highest = rainfall[i];
}
}
for (int i = 0; i < SIZE; i++)
{
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
}
}
for(int i = 0; i < SIZE; i++)
{
total = rainfall[0] + rainfall[1] + rainfall[2] + rainfall[3] + rainfall[4];
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
}
for(int i = 0; i << SIZE; i++)
{
average = total / SIZE;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
}
return 0;
}
c++
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
What is this line supposed to docin >> rainfall[SIZE];
?
– Galik
Nov 21 at 2:25
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
@LouisCastillo Guess you want to fill arrayrainfall
with number of rainfalls?
– SilvioCro
Nov 21 at 2:28
|
show 2 more comments
Write a program that reads in the rainfall for each of 12 months and outputs the month and the rainfall for the month. It should then output the month with the most rainfall, the month with the least rainfall, the total amount of rainfall for the year, and the average monthly rainfall. All rainfall amounts should be output with 2 significant digits after the decimal point.
The program should incorporate two parallel arrays: month of type string containing the month names, and rainfall of type double containing the rainfall in inches for the corresponding month.
Hint: Check slides 8 thru 10 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for examples.
The program should read in the rainfall amount for each month using a for loop.
The program should output the month right justified in a field width of 10 and the corresponding rainfall right justified in a field width of 6.
The program should calculate and display the total rainfall for the year and the average monthly rainfall.
The program should calculate the highest and lowest amounts and display the amounts and the corresponding month names.
Hint 1: Check slides 3 and 4 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for the code to find the lowest and the highest.
Hint 2: In addition, you will have to keep track of the index at which you found the highest and the lowest amount. Output the month name corresponding to the index. For example, if font rainfall[3] is the lowest rainfall, you will print font month[3] as the corresponding month.
Validation: Do not accept negative numbers for monthly rainfall figures.
When the input is as shown in Figure 1, your program should produce the output as shown in Figure 2.
Figure 1: (input)
3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3
Figure 2: (output)
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
this is my code.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfall [SIZE];
double highest;
double lowest;
double total, average;
cin >> rainfall[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[SIZE] << right <<setw(6) << rainfall[i] << endl;
}
for ( int i = 0; i < SIZE; i++)
{
if (rainfall[i] > highest)
{
highest = rainfall[i];
}
}
for (int i = 0; i < SIZE; i++)
{
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
}
}
for(int i = 0; i < SIZE; i++)
{
total = rainfall[0] + rainfall[1] + rainfall[2] + rainfall[3] + rainfall[4];
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
}
for(int i = 0; i << SIZE; i++)
{
average = total / SIZE;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
}
return 0;
}
c++
Write a program that reads in the rainfall for each of 12 months and outputs the month and the rainfall for the month. It should then output the month with the most rainfall, the month with the least rainfall, the total amount of rainfall for the year, and the average monthly rainfall. All rainfall amounts should be output with 2 significant digits after the decimal point.
The program should incorporate two parallel arrays: month of type string containing the month names, and rainfall of type double containing the rainfall in inches for the corresponding month.
Hint: Check slides 8 thru 10 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for examples.
The program should read in the rainfall amount for each month using a for loop.
The program should output the month right justified in a field width of 10 and the corresponding rainfall right justified in a field width of 6.
The program should calculate and display the total rainfall for the year and the average monthly rainfall.
The program should calculate the highest and lowest amounts and display the amounts and the corresponding month names.
Hint 1: Check slides 3 and 4 of CS1336_Lect7c_Arrays_Compare_Parallel.pptx for the code to find the lowest and the highest.
Hint 2: In addition, you will have to keep track of the index at which you found the highest and the lowest amount. Output the month name corresponding to the index. For example, if font rainfall[3] is the lowest rainfall, you will print font month[3] as the corresponding month.
Validation: Do not accept negative numbers for monthly rainfall figures.
When the input is as shown in Figure 1, your program should produce the output as shown in Figure 2.
Figure 1: (input)
3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3
Figure 2: (output)
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
this is my code.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfall [SIZE];
double highest;
double lowest;
double total, average;
cin >> rainfall[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[SIZE] << right <<setw(6) << rainfall[i] << endl;
}
for ( int i = 0; i < SIZE; i++)
{
if (rainfall[i] > highest)
{
highest = rainfall[i];
}
}
for (int i = 0; i < SIZE; i++)
{
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
}
}
for(int i = 0; i < SIZE; i++)
{
total = rainfall[0] + rainfall[1] + rainfall[2] + rainfall[3] + rainfall[4];
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
}
for(int i = 0; i << SIZE; i++)
{
average = total / SIZE;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
}
return 0;
}
c++
c++
edited Nov 21 at 2:44
David C. Rankin
40.1k32647
40.1k32647
asked Nov 21 at 2:22
Louis Castillo
1
1
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
What is this line supposed to docin >> rainfall[SIZE];
?
– Galik
Nov 21 at 2:25
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
@LouisCastillo Guess you want to fill arrayrainfall
with number of rainfalls?
– SilvioCro
Nov 21 at 2:28
|
show 2 more comments
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
What is this line supposed to docin >> rainfall[SIZE];
?
– Galik
Nov 21 at 2:25
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
@LouisCastillo Guess you want to fill arrayrainfall
with number of rainfalls?
– SilvioCro
Nov 21 at 2:28
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
What is this line supposed to do
cin >> rainfall[SIZE];
?– Galik
Nov 21 at 2:25
What is this line supposed to do
cin >> rainfall[SIZE];
?– Galik
Nov 21 at 2:25
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
@LouisCastillo Guess you want to fill array
rainfall
with number of rainfalls?– SilvioCro
Nov 21 at 2:28
@LouisCastillo Guess you want to fill array
rainfall
with number of rainfalls?– SilvioCro
Nov 21 at 2:28
|
show 2 more comments
3 Answers
3
active
oldest
votes
I have fixed your code and added comment on places I think you were off the line. Please try this out with your input and most importantly understand the changes made in the code and why is this fixing issues in your code. This way you won't just be coping/pasting the code but would also be learning in the process. Happy coding!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainfall[SIZE];
double highest = 0.0;
double lowest = 0.0;
double total = 0.0, average = 0.0;
double input;
for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
{
cin >> input;
cin.ignore(1000, 'n');
if (input > 0.0)
{
rainfall[i] = input;
//for the first element assign lowest and highest to same value.
if (i == 0)
{
highest = input;
lowest = input;
}
if (input > highest) highest = input;
if (input < lowest) lowest = input;
total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
i++; //if the number is non negative increment i.
}
}
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
}
average = total / SIZE;
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
return 0;
}
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
add a comment |
Your instructions are fairly specific and sufficient. Just take them one at a time. For starters, you will need to validate you read 12
months of rainfall data into an array of double
. Both to declare the array and validate the number of rainfall amounts read, you will need a constant. So:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
You have a choice where to declare your array of string
holding the name of each month
. You can declare the array local to main()
, or you can declare the names as a global array (probably preferable). You will not be changing the names of each month, so you should qualify the array as const
, e.g.
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
Whenever you are reading multiple inputs in a loop, you want to control the loop based on a valid input and ensure that no stream errors occurred (eofbit, failbit, badbit
) before you make use of your data. In your case, a single read-loop is all that is required to read the information, compute the sum
for use with the total
and average
as well as determining the max
and min
and the months where each occurred (maxmo
, and minmo
). (note: that min
is initialized sufficiently large so that it can handle determining the actual minimum. Initializing to 0
would not work)
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
Now with your read loop complete, you would likely want to validate you actually have 12
months of rainfall data, and then compute the average e.g.:
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
All that remains is formatting the output. The default justification is right
so there is no need to include that format modifier, but you will need to use std::fixed
and std::setprecision()
to format your floating-point numeric output with two decimal places. You need only set each once unless you make changes to them in subsequent output. You can simply set:
cout << fixed << setprecision(2); /* set fixed and precision(2) */
The floating-point output will now be output with two decimal places. All that remains is outputting the required information, e.g.
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
(note: there is no actual need for multiple cout
calls above, you can simply use a single cout
and string all the information together)
Putting it altogether, you could do something like the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
cout << fixed << setprecision(2); /* set fixed and precision(2) */
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
}
Example Use/Output
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" |
./bin/rainfall
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
Look things over and let me know if you have further questions.
add a comment |
- Variable
SIZE
doesn't make any sense because you Know that year will always have 12 months. It only waste memory. If you want to use some kind of variable, use#define SIZE 12
. Put it on the top under#include
section. - Use can use only one loop for all tasks.
- Last two loops also make no sense. If you want to calculate total amount of rainfall, put
total += rainfall[i];
inside loop.
cin >> rainfall[SIZE];
also makes no sense and it's not correct. Why? With that line you acctually will put value inrainfall[12];
but last index ofrainfall
is 11 not 12.
To fillrainfall
with values, putcin >> ranfall[i];
inside loop. You have to use two different loops for fillingrainfall
and calculating all other stuff.
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see thatconst int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with#define SIZE 12
. If you want to improve onconst int SIZE = 12;
, useconstexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404460%2fc-arrays-using-for-loops-to-output-rainfall-statistics%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have fixed your code and added comment on places I think you were off the line. Please try this out with your input and most importantly understand the changes made in the code and why is this fixing issues in your code. This way you won't just be coping/pasting the code but would also be learning in the process. Happy coding!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainfall[SIZE];
double highest = 0.0;
double lowest = 0.0;
double total = 0.0, average = 0.0;
double input;
for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
{
cin >> input;
cin.ignore(1000, 'n');
if (input > 0.0)
{
rainfall[i] = input;
//for the first element assign lowest and highest to same value.
if (i == 0)
{
highest = input;
lowest = input;
}
if (input > highest) highest = input;
if (input < lowest) lowest = input;
total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
i++; //if the number is non negative increment i.
}
}
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
}
average = total / SIZE;
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
return 0;
}
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
add a comment |
I have fixed your code and added comment on places I think you were off the line. Please try this out with your input and most importantly understand the changes made in the code and why is this fixing issues in your code. This way you won't just be coping/pasting the code but would also be learning in the process. Happy coding!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainfall[SIZE];
double highest = 0.0;
double lowest = 0.0;
double total = 0.0, average = 0.0;
double input;
for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
{
cin >> input;
cin.ignore(1000, 'n');
if (input > 0.0)
{
rainfall[i] = input;
//for the first element assign lowest and highest to same value.
if (i == 0)
{
highest = input;
lowest = input;
}
if (input > highest) highest = input;
if (input < lowest) lowest = input;
total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
i++; //if the number is non negative increment i.
}
}
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
}
average = total / SIZE;
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
return 0;
}
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
add a comment |
I have fixed your code and added comment on places I think you were off the line. Please try this out with your input and most importantly understand the changes made in the code and why is this fixing issues in your code. This way you won't just be coping/pasting the code but would also be learning in the process. Happy coding!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainfall[SIZE];
double highest = 0.0;
double lowest = 0.0;
double total = 0.0, average = 0.0;
double input;
for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
{
cin >> input;
cin.ignore(1000, 'n');
if (input > 0.0)
{
rainfall[i] = input;
//for the first element assign lowest and highest to same value.
if (i == 0)
{
highest = input;
lowest = input;
}
if (input > highest) highest = input;
if (input < lowest) lowest = input;
total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
i++; //if the number is non negative increment i.
}
}
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
}
average = total / SIZE;
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
return 0;
}
I have fixed your code and added comment on places I think you were off the line. Please try this out with your input and most importantly understand the changes made in the code and why is this fixing issues in your code. This way you won't just be coping/pasting the code but would also be learning in the process. Happy coding!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 12;
string months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainfall[SIZE];
double highest = 0.0;
double lowest = 0.0;
double total = 0.0, average = 0.0;
double input;
for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
{
cin >> input;
cin.ignore(1000, 'n');
if (input > 0.0)
{
rainfall[i] = input;
//for the first element assign lowest and highest to same value.
if (i == 0)
{
highest = input;
lowest = input;
}
if (input > highest) highest = input;
if (input < lowest) lowest = input;
total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
i++; //if the number is non negative increment i.
}
}
for (int i = 0; i < SIZE; i++)
{
cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
}
average = total / SIZE;
cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
return 0;
}
answered Nov 21 at 2:57
user3164323
626
626
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
add a comment |
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
Made an edit and removed couple of more loops.
– user3164323
Nov 21 at 3:01
add a comment |
Your instructions are fairly specific and sufficient. Just take them one at a time. For starters, you will need to validate you read 12
months of rainfall data into an array of double
. Both to declare the array and validate the number of rainfall amounts read, you will need a constant. So:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
You have a choice where to declare your array of string
holding the name of each month
. You can declare the array local to main()
, or you can declare the names as a global array (probably preferable). You will not be changing the names of each month, so you should qualify the array as const
, e.g.
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
Whenever you are reading multiple inputs in a loop, you want to control the loop based on a valid input and ensure that no stream errors occurred (eofbit, failbit, badbit
) before you make use of your data. In your case, a single read-loop is all that is required to read the information, compute the sum
for use with the total
and average
as well as determining the max
and min
and the months where each occurred (maxmo
, and minmo
). (note: that min
is initialized sufficiently large so that it can handle determining the actual minimum. Initializing to 0
would not work)
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
Now with your read loop complete, you would likely want to validate you actually have 12
months of rainfall data, and then compute the average e.g.:
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
All that remains is formatting the output. The default justification is right
so there is no need to include that format modifier, but you will need to use std::fixed
and std::setprecision()
to format your floating-point numeric output with two decimal places. You need only set each once unless you make changes to them in subsequent output. You can simply set:
cout << fixed << setprecision(2); /* set fixed and precision(2) */
The floating-point output will now be output with two decimal places. All that remains is outputting the required information, e.g.
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
(note: there is no actual need for multiple cout
calls above, you can simply use a single cout
and string all the information together)
Putting it altogether, you could do something like the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
cout << fixed << setprecision(2); /* set fixed and precision(2) */
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
}
Example Use/Output
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" |
./bin/rainfall
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
Look things over and let me know if you have further questions.
add a comment |
Your instructions are fairly specific and sufficient. Just take them one at a time. For starters, you will need to validate you read 12
months of rainfall data into an array of double
. Both to declare the array and validate the number of rainfall amounts read, you will need a constant. So:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
You have a choice where to declare your array of string
holding the name of each month
. You can declare the array local to main()
, or you can declare the names as a global array (probably preferable). You will not be changing the names of each month, so you should qualify the array as const
, e.g.
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
Whenever you are reading multiple inputs in a loop, you want to control the loop based on a valid input and ensure that no stream errors occurred (eofbit, failbit, badbit
) before you make use of your data. In your case, a single read-loop is all that is required to read the information, compute the sum
for use with the total
and average
as well as determining the max
and min
and the months where each occurred (maxmo
, and minmo
). (note: that min
is initialized sufficiently large so that it can handle determining the actual minimum. Initializing to 0
would not work)
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
Now with your read loop complete, you would likely want to validate you actually have 12
months of rainfall data, and then compute the average e.g.:
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
All that remains is formatting the output. The default justification is right
so there is no need to include that format modifier, but you will need to use std::fixed
and std::setprecision()
to format your floating-point numeric output with two decimal places. You need only set each once unless you make changes to them in subsequent output. You can simply set:
cout << fixed << setprecision(2); /* set fixed and precision(2) */
The floating-point output will now be output with two decimal places. All that remains is outputting the required information, e.g.
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
(note: there is no actual need for multiple cout
calls above, you can simply use a single cout
and string all the information together)
Putting it altogether, you could do something like the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
cout << fixed << setprecision(2); /* set fixed and precision(2) */
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
}
Example Use/Output
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" |
./bin/rainfall
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
Look things over and let me know if you have further questions.
add a comment |
Your instructions are fairly specific and sufficient. Just take them one at a time. For starters, you will need to validate you read 12
months of rainfall data into an array of double
. Both to declare the array and validate the number of rainfall amounts read, you will need a constant. So:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
You have a choice where to declare your array of string
holding the name of each month
. You can declare the array local to main()
, or you can declare the names as a global array (probably preferable). You will not be changing the names of each month, so you should qualify the array as const
, e.g.
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
Whenever you are reading multiple inputs in a loop, you want to control the loop based on a valid input and ensure that no stream errors occurred (eofbit, failbit, badbit
) before you make use of your data. In your case, a single read-loop is all that is required to read the information, compute the sum
for use with the total
and average
as well as determining the max
and min
and the months where each occurred (maxmo
, and minmo
). (note: that min
is initialized sufficiently large so that it can handle determining the actual minimum. Initializing to 0
would not work)
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
Now with your read loop complete, you would likely want to validate you actually have 12
months of rainfall data, and then compute the average e.g.:
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
All that remains is formatting the output. The default justification is right
so there is no need to include that format modifier, but you will need to use std::fixed
and std::setprecision()
to format your floating-point numeric output with two decimal places. You need only set each once unless you make changes to them in subsequent output. You can simply set:
cout << fixed << setprecision(2); /* set fixed and precision(2) */
The floating-point output will now be output with two decimal places. All that remains is outputting the required information, e.g.
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
(note: there is no actual need for multiple cout
calls above, you can simply use a single cout
and string all the information together)
Putting it altogether, you could do something like the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
cout << fixed << setprecision(2); /* set fixed and precision(2) */
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
}
Example Use/Output
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" |
./bin/rainfall
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
Look things over and let me know if you have further questions.
Your instructions are fairly specific and sufficient. Just take them one at a time. For starters, you will need to validate you read 12
months of rainfall data into an array of double
. Both to declare the array and validate the number of rainfall amounts read, you will need a constant. So:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
You have a choice where to declare your array of string
holding the name of each month
. You can declare the array local to main()
, or you can declare the names as a global array (probably preferable). You will not be changing the names of each month, so you should qualify the array as const
, e.g.
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
Whenever you are reading multiple inputs in a loop, you want to control the loop based on a valid input and ensure that no stream errors occurred (eofbit, failbit, badbit
) before you make use of your data. In your case, a single read-loop is all that is required to read the information, compute the sum
for use with the total
and average
as well as determining the max
and min
and the months where each occurred (maxmo
, and minmo
). (note: that min
is initialized sufficiently large so that it can handle determining the actual minimum. Initializing to 0
would not work)
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
Now with your read loop complete, you would likely want to validate you actually have 12
months of rainfall data, and then compute the average e.g.:
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
All that remains is formatting the output. The default justification is right
so there is no need to include that format modifier, but you will need to use std::fixed
and std::setprecision()
to format your floating-point numeric output with two decimal places. You need only set each once unless you make changes to them in subsequent output. You can simply set:
cout << fixed << setprecision(2); /* set fixed and precision(2) */
The floating-point output will now be output with two decimal places. All that remains is outputting the required information, e.g.
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
(note: there is no actual need for multiple cout
calls above, you can simply use a single cout
and string all the information together)
Putting it altogether, you could do something like the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MONTHS 12 /* if you need a constant, #define one (or more) */
int main (void) {
double rainfall[MONTHS] = {0}, /* declare/initialize variables */
sum = 0, min = 10000, max = 0, avg = 0;
const string month = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
int mo = 0, maxmo = 0, minmo = 0;
while ((cin >> rainfall[mo])) { /* while good input */
if (rainfall[mo] > 0) { /* validate positive rainfall */
sum += rainfall[mo]; /* increment sum by rainfall */
if (rainfall[mo] < min) /* set min and minmo */
min = rainfall[mo], minmo = mo;
if (rainfall[mo] > max) /* set max and maxmo */
max = rainfall[mo], maxmo = mo;
mo++;
}
}
if (mo != MONTHS) /* valdate 12 months of data read */
cerr << "warning: only '" << mo << "' months data available.n";
avg = sum / (double)mo;
cout << fixed << setprecision(2); /* set fixed and precision(2) */
for (int i = 0; i < mo; i++) /* output monthly data */
cout << setw(10) << month[i] << setw(6) << rainfall[i] << 'n';
/* output statistics */
cout << "nThe most rainfall was " << max << " inches in "
<< month[maxmo] << ".n";
cout << "The least rainfall was " << min << " inches in "
<< month[minmo] << ".n";
cout << "The total amount of rainfall for the year is "
<< sum << " inches.n";
cout << "The average monthly rainfall for the year is "
<< avg << " inches.n";
}
Example Use/Output
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" |
./bin/rainfall
January 3.20
February 0.55
March 2.20
April 0.56
May 0.24
June 0.95
July 2.00
August 0.35
September 5.90
October 1.10
November 2.80
December 0.30
The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.
Look things over and let me know if you have further questions.
answered Nov 21 at 4:00
David C. Rankin
40.1k32647
40.1k32647
add a comment |
add a comment |
- Variable
SIZE
doesn't make any sense because you Know that year will always have 12 months. It only waste memory. If you want to use some kind of variable, use#define SIZE 12
. Put it on the top under#include
section. - Use can use only one loop for all tasks.
- Last two loops also make no sense. If you want to calculate total amount of rainfall, put
total += rainfall[i];
inside loop.
cin >> rainfall[SIZE];
also makes no sense and it's not correct. Why? With that line you acctually will put value inrainfall[12];
but last index ofrainfall
is 11 not 12.
To fillrainfall
with values, putcin >> ranfall[i];
inside loop. You have to use two different loops for fillingrainfall
and calculating all other stuff.
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see thatconst int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with#define SIZE 12
. If you want to improve onconst int SIZE = 12;
, useconstexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
add a comment |
- Variable
SIZE
doesn't make any sense because you Know that year will always have 12 months. It only waste memory. If you want to use some kind of variable, use#define SIZE 12
. Put it on the top under#include
section. - Use can use only one loop for all tasks.
- Last two loops also make no sense. If you want to calculate total amount of rainfall, put
total += rainfall[i];
inside loop.
cin >> rainfall[SIZE];
also makes no sense and it's not correct. Why? With that line you acctually will put value inrainfall[12];
but last index ofrainfall
is 11 not 12.
To fillrainfall
with values, putcin >> ranfall[i];
inside loop. You have to use two different loops for fillingrainfall
and calculating all other stuff.
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see thatconst int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with#define SIZE 12
. If you want to improve onconst int SIZE = 12;
, useconstexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
add a comment |
- Variable
SIZE
doesn't make any sense because you Know that year will always have 12 months. It only waste memory. If you want to use some kind of variable, use#define SIZE 12
. Put it on the top under#include
section. - Use can use only one loop for all tasks.
- Last two loops also make no sense. If you want to calculate total amount of rainfall, put
total += rainfall[i];
inside loop.
cin >> rainfall[SIZE];
also makes no sense and it's not correct. Why? With that line you acctually will put value inrainfall[12];
but last index ofrainfall
is 11 not 12.
To fillrainfall
with values, putcin >> ranfall[i];
inside loop. You have to use two different loops for fillingrainfall
and calculating all other stuff.
- Variable
SIZE
doesn't make any sense because you Know that year will always have 12 months. It only waste memory. If you want to use some kind of variable, use#define SIZE 12
. Put it on the top under#include
section. - Use can use only one loop for all tasks.
- Last two loops also make no sense. If you want to calculate total amount of rainfall, put
total += rainfall[i];
inside loop.
cin >> rainfall[SIZE];
also makes no sense and it's not correct. Why? With that line you acctually will put value inrainfall[12];
but last index ofrainfall
is 11 not 12.
To fillrainfall
with values, putcin >> ranfall[i];
inside loop. You have to use two different loops for fillingrainfall
and calculating all other stuff.
answered Nov 21 at 2:40
SilvioCro
150112
150112
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see thatconst int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with#define SIZE 12
. If you want to improve onconst int SIZE = 12;
, useconstexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
add a comment |
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see thatconst int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with#define SIZE 12
. If you want to improve onconst int SIZE = 12;
, useconstexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
1
1
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see that
const int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with #define SIZE 12
. If you want to improve on const int SIZE = 12;
, use constexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
Strongly disagree with point 1. Vehemently disagree the solution to point one. A decent compiler will see that
const int SIZE = 12;
and sub in 12 where required. No memory cost and none of the macro substitution problems that come with #define SIZE 12
. If you want to improve on const int SIZE = 12;
, use constexpr int SIZE = 12;
– user4581301
Nov 21 at 3:36
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
I don't know what compiler he uses.
– SilvioCro
Nov 21 at 3:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404460%2fc-arrays-using-for-loops-to-output-rainfall-statistics%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not sure what im doing wrong when i run it i dont get a output
– Louis Castillo
Nov 21 at 2:23
What is this line supposed to do
cin >> rainfall[SIZE];
?– Galik
Nov 21 at 2:25
You can and have to use one loop, not five of them.
– SilvioCro
Nov 21 at 2:27
its supposed to read in the input that's shown
– Louis Castillo
Nov 21 at 2:27
@LouisCastillo Guess you want to fill array
rainfall
with number of rainfalls?– SilvioCro
Nov 21 at 2:28