Help asap
so i think we might have nested loops in my upcoming final.
so im practicing some challanges.
heres a program sample
For how many years have you collected rainfall data? 2
Enter the rainfall (in inches) for month 1 of year 1: 4
Enter the rainfall (in inches) for month 2 of year 1: 5.2
Enter the rainfall (in inches) for month 3 of year 1: 4.7
Enter the rainfall (in inches) for month 4 of year 1: 3.6
Enter the rainfall (in inches) for month 5 of year 1: 2.3
Enter the rainfall (in inches) for month 6 of year 1: .6
Enter the rainfall (in inches) for month 7 of year 1: .1
Enter the rainfall (in inches) for month 8 of year 1: .3
Enter the rainfall (in inches) for month 9 of year 1: 2.4
Enter the rainfall (in inches) for month 10 of year 1: 3.7
Enter the rainfall (in inches) for month 11 of year 1: 5.2
Enter the rainfall (in inches) for month 12 of year 1: 7.1
Enter the rainfall (in inches) for month 1 of year 2: 5.2
Enter the rainfall (in inches) for month 2 of year 2: 6.5
Enter the rainfall (in inches) for month 3 of year 2: 4.4
Enter the rainfall (in inches) for month 4 of year 2: 6.8
Enter the rainfall (in inches) for month 5 of year 2: 5.1
Enter the rainfall (in inches) for month 6 of year 2: 2.1
Enter the rainfall (in inches) for month 7 of year 2: 0
Enter the rainfall (in inches) for month 8 of year 2: 0
Enter the rainfall (in inches) for month 9 of year 2: .9
Enter the rainfall (in inches) for month 10 of year 2: 2.6
Enter the rainfall (in inches) for month 11 of year 2: 5.1
Enter the rainfall (in inches) for month 12 of year 2: 4.2
The total number of months of rainfall data: 24
The total inches of rainfall for that period: 82.1
The average rainfall per month for that period: 3.42083
i just need to know how to do that blod bold part. I could just do it by rainfall for month 1, 2, 3, 4, 18, 20, 21, 22, 23, 24 (2 years = 24 months) but i want it if i put 2 years to show 12 months then the other 12 months for the next year.
heres my code so far
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double total = 0.0;// total rainfall inches
int numData;//number of years collected rainfall data
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
for (int month = 1; month <= numData; month++)
{ float inches;
cout <<"Enter the rainfall (in inches) for month" << month << ": ";
cin >> inches;
}
system("pause");
return 0;
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
use nested loops
for(int year = 1; year < 2; year++)
{
for(int month = 1; month <= 12; month++)
{
// blabla
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thanks dude that helped a lot but idk it didnt want to cout the months for the 2nd year. i put 2 years and it only gaved me the first 12 months of year 1.
heres my updated code
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double total = 0.0;// total rainfall inches
int numData;
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
for(int year = 1; year < 2; year++)
{
for(int month = 1; month <= 12; month++)
{
float inches;
cout <<"Enter the rainfall (in inches) for month" << month << ": ";
cin >> inches;
}
}
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
ok thanks for the help seems like im getting it. sorry for putting "asap" :D
one question to mr boolf when i put my input when it says "of year1" i cant seem to give it a space. like i out "of year 13.4 i want it to look of year 1: 3.4 seems easy to do but im having trouble
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double total = 0.0;// total rainfall inches
int numData;
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
for(int month = 0; month < numData*12; month++)
{
float inches;
cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
<< (month/12) + 1;
cin >> inches;
}
system("pause");
return 0;
}
alright lets say for the first input you type 3.2 it looks like 13.2 because the "1" of year isnt spaced
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
LOL dumb me
thanks a lot.
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
hmm i seem to be stuck (no surprise) lol
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double totalRain = 0.0;// total rainfall inches
double totalMonths = 0.0;
int numData;
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
for(int month = 0; month < numData*12; month++)
{
float inches;
cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
<< (month/12) + 1 << ": ";
cin >> inches;
totalRain += inches;
totalMonths =
}
cout <<"The total number of months of rainfall data:" << totalMonths << endl;
cout <<"The total inches of rainfall for that period:" << totalRain << endl;
system("pause");
return 0;
}
i was able to get the total inches of rain but idk how to do this partThe total number of months of rainfall data: 24 (saying i put 2 years)
The total inches of rainfall for that period: 82.1
The average rainfall per month for that period: 3.42083
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
ohh ok yeah numYears * 12 works perfect.
i was also able to calcluate the avg damn that was a bit hard. once again thanks for the help. :D
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
anyone know why this isnt working?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double totalRain = 0.0;// total rainfall inches
double count = 0.0;
int numData;
char answer;
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
if (numData < 1)
{
cout <<"Invalid: number of years must be at least one." << endl;
cout <<"Do you want to try again (y/n)?";
cin >> answer;
{
if (answer == 'Y' || answer == 'y')
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
if (answer != 'Y' || answer != 'y')
cout <<"Goodbye";
}
}
for(int month = 0; month < numData*12; month++)
{
float inches;
cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
<< (month/12) + 1 << ": ";
cin >> inches;
count++;
totalRain += inches;
}
cout <<"The total number of months of rainfall data:" << numData * 12 << endl;
cout <<"The total inches of rainfall for that period:" << totalRain << endl;
cout <<"The average rainfall per month for that period:" << totalRain / (float)count << endl;
system("pause");
return 0;
}
the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.
ex
For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
cool thanks!
and actually im using count variable for the avg and it works fine.
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
(sorry missed second page of comments)
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116