943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1082
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 15th, 2008
0

Help asap

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. double total = 0.0;// total rainfall inches
  10. int numData;//number of years collected rainfall data
  11.  
  12.  
  13. cout <<"For how many years have you collected rainfall data? ";
  14. cin >> numData;
  15.  
  16.  
  17.  
  18. for (int month = 1; month <= numData; month++)
  19. { float inches;
  20.  
  21. cout <<"Enter the rainfall (in inches) for month" << month << ": ";
  22. cin >> inches;
  23.  
  24. }
  25.  
  26.  
  27. system("pause");
  28. return 0;
Last edited by anbuninja; Dec 15th, 2008 at 6:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

use nested loops
C++ Syntax (Toggle Plain Text)
  1. for(int year = 1; year < 2; year++)
  2. {
  3. for(int month = 1; month <= 12; month++)
  4. {
  5. // blabla
  6. }
  7. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Dec 15th, 2008
0

Re: Help asap

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. double total = 0.0;// total rainfall inches
  11. int numData;
  12.  
  13. cout <<"For how many years have you collected rainfall data? ";
  14. cin >> numData;
  15.  
  16. for(int year = 1; year < 2; year++)
  17. {
  18. for(int month = 1; month <= 12; month++)
  19. {
  20. float inches;
  21.  
  22. cout <<"Enter the rainfall (in inches) for month" << month << ": ";
  23. cin >> inches;
  24.  
  25. }
  26. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

Either that or
c++ Syntax (Toggle Plain Text)
  1. for(int month = 0; month < numYears*12; month++) {
  2. std::cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << std::endl;
  3. // do what you want
  4. }

This assuming I understood your question.

Please read here why such thing as asap is to be avoided.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 15th, 2008
0

Re: Help asap

ok thanks for the help seems like im getting it. sorry for putting "asap"

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

I don't understand what do you mean - I put an endline before cin.

Post your code and explain where do you want to put the space (i.e. how is the output and how you'd like it to be)
Last edited by mrboolf; Dec 15th, 2008 at 6:47 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 15th, 2008
0

Re: Help asap

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. double total = 0.0;// total rainfall inches
  11. int numData;
  12.  
  13. cout <<"For how many years have you collected rainfall data? ";
  14. cin >> numData;
  15.  
  16. for(int month = 0; month < numData*12; month++)
  17.  
  18. {
  19. float inches;
  20. cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
  21. << (month/12) + 1;
  22. cin >> inches;
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. system("pause");
  33. return 0;
  34. }

alright lets say for the first input you type 3.2 it looks like 13.2 because the "1" of year isnt spaced
Last edited by anbuninja; Dec 15th, 2008 at 6:51 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
1

Re: Help asap

Well you cut off the endline - put it back or simply insert a space

c++ Syntax (Toggle Plain Text)
  1. cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << ": ";

Was it hard?
Last edited by mrboolf; Dec 15th, 2008 at 6:53 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 15th, 2008
0

Re: Help asap

LOL dumb me
thanks a lot.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

hmm i seem to be stuck (no surprise) lol

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. double totalRain = 0.0;// total rainfall inches
  11. double totalMonths = 0.0;
  12. int numData;
  13.  
  14. cout <<"For how many years have you collected rainfall data? ";
  15. cin >> numData;
  16.  
  17. for(int month = 0; month < numData*12; month++)
  18.  
  19. {
  20. float inches;
  21. cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
  22. << (month/12) + 1 << ": ";
  23. cin >> inches;
  24.  
  25. totalRain += inches;
  26. totalMonths =
  27. }
  28. cout <<"The total number of months of rainfall data:" << totalMonths << endl;
  29. cout <<"The total inches of rainfall for that period:" << totalRain << endl;
  30.  
  31.  
  32. system("pause");
  33. return 0;
  34. }

i was able to get the total inches of rain but idk how to do this part

The 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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC