Help asap

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Help asap

 
0
  #1
Dec 15th, 2008
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help asap

 
0
  #2
Dec 15th, 2008
use nested loops
  1. for(int year = 1; year < 2; year++)
  2. {
  3. for(int month = 1; month <= 12; month++)
  4. {
  5. // blabla
  6. }
  7. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #3
Dec 15th, 2008
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
0
  #4
Dec 15th, 2008
Either that or
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #5
Dec 15th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
0
  #6
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #7
Dec 15th, 2008
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
1
  #8
Dec 15th, 2008
Well you cut off the endline - put it back or simply insert a space

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #9
Dec 15th, 2008
LOL dumb me
thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #10
Dec 15th, 2008
hmm i seem to be stuck (no surprise) lol

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC