| | |
Help asap
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
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
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)
#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;
Last edited by anbuninja; Dec 15th, 2008 at 6:17 pm.
use nested loops
C++ Syntax (Toggle Plain Text)
for(int year = 1; year < 2; year++) { for(int month = 1; month <= 12; month++) { // blabla } }
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.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
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
heres my updated code
C++ Syntax (Toggle Plain Text)
#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; } }
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Either that or
This assuming I understood your question.
Please read here why such thing as asap is to be avoided.
c++ Syntax (Toggle Plain Text)
for(int month = 0; month < numYears*12; month++) { std::cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << std::endl; // do what you want }
This assuming I understood your question.
Please read here why such thing as asap is to be avoided.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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
Last edited by anbuninja; Dec 15th, 2008 at 6:51 pm.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Well you cut off the endline - put it back or simply insert a space
Was it hard?
c++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
hmm i seem to be stuck (no surprise) lol
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
C++ Syntax (Toggle Plain Text)
#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 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
![]() |
Similar Threads
- I feel sick... Hard drive not found... need help asap =( (Troubleshooting Dead Machines)
- Extracting folder information with c++ (C++)
- Need Help ASAP.. iMac(blue and white) wont eject the cd (Apple Hardware)
- Need Help ASAP.. iMac(blue and white) wont eject the cd (Troubleshooting Dead Machines)
- Need x86 assembly programmer ASAP ! (Assembly)
- Very New to Java and need help converting cubic inches to cm (Java)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets







