I need to write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. It should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. This is what I have so far and I'm not even sure if it's correct:
#include <iostream>
using namespace std;
int main()
{
int years;
// Get the amount of years.
cout << "Enter how many years of rainfall: ";
cin >> years;
double avgRain = 0;
double rainSum = 0;
int count = 0;
double monthlyTotals[12];
string outRainSum;
string lowMonth;
string highMonth="January";
string monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
cout << "Please enter the amount of rainfall for each month: " << endl;
I have been looking through my textbook and I just can't seem to understand how to put this all together correct.
Now part 2 you should make for-loop for each year:
1 2 3
for (int i=0; i<years; i++){
// some code here
}
on // some code here -part you need to use some variables such as input, monthTotal, sum and average. Variable sum is used only for average for that period (a year).
Use knowledge (which you have somewhat seems) and try to solve problem with new thing i gave to you; nested loops are actually only 2 or more looping sequences in each other (like 2 for-loops):
1 2 3 4 5
for (int i=0;i<something; i++) {
for (int j=0; j<something_else; j++) {
//fancy code here
}
}
This is my first semester taking a C++ class. the chapter that we are on hasn't introduced anything that you typed. That's why I'm having a hard time understanding it. I've tried looking up examples of something similar to help me with this but everything I look up has stuff that we haven't got to yet. I do better seeing an example of something so that I can look at it and say ok so this code does this. I'm more of a visual learner. Do you think you could come up with a similar program for me to study off of if it's not too much trouble?
Can you tell me which of following keywords you have heard within a class?
-do
-while
-for
None?
step 1: Look through material your teacher gave to you by paper, school website or anything like that.
Can you NOW tell me which of following keywords you just have read from the material?
-do
-while
-for
Like i said earlier: nested loops are actually 2 or more looping sequences within each other. And those looping sequences you might ask? Them are composed via: do, while and for loops.
Show me loop, that you make, which takes 12 numbers (well floats seems from OP) and make average of values given. i'll continue afterwards.
EDIT: Keep original code in another file. Make a new cpp file and show the task above :) Remember: programming is like taking steps while walking: each little step will get you closer to end.
After working loop you actually can make "copy-paste"ing
The section that this assignment comes from in our book is "Count-Controlled Loops: The for Loop." And this is the hint our teacher gave us for this specific assignment: "This is also a count controlled loop and it is nested. The outer loop will respond to user input, the inner loop will iterate twelve times." I actually have to go to work in a few minutes so I will reply again later tonight with the rest of the information you asked for. Thank you for your help!
Ok. I have read the chapter and went over that section several times trying to understand it and I don't so that's why I tried to use an alternative solution to find help (this site). If you're just going to say negative things to me, please don't bother commenting. Like I said, this is my first class on C++ and I'm sorry that I'm not comprehending it as well as you. Sorry for wasting your time.
#include <iostream>
usingnamespace std;
int main()
{
int years;
// Get the amount of years.
cout << "Enter how many years of rainfall: ";
cin >> years;
double avgRain = 0;
double rainSum = 0;
int count = 0;
double monthlyTotals[12]; // not actually needed
string outRainSum; // sum i was talking earlier :)
string lowMonth; //huh?
string highMonth="January"; //huh?
string monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // Good: easy to use in INNER loop
After code above:
1 2 3 4 5 6 7
for (int i=0; i<years; i++) {
for (int j=0; j<12; j++) { // NOTE: there is 12 months in a year (as you know)
cout << "This is " << monthNames[j] << ". How much water did sky gave you in this month?";
//....... More actual code here (you know how: use variable=another_variable+yet_another_variable and so on)
// cin >> might help you here... Think man; think
}
}
Lastly: never ever think programming is always taking code other people made for you. There might be bugs (errors in the code you nay not notice).
Simplyfying task helps alot.
Example:
"I need to calculate area of n amount of rectangles" (n will be integer)
Let's think shall we?
-area of rectangle is width*height
->make code to take 2 numbers and multiple those together
-->we have now code snippet that calculates area of ONE rectangle.
Now we have some lines of code which actually do something. That wasn't our task thought.We need way to produce same behavior again and again. That's why we have loops. (in this case for loop):
1 2 3
for (i=0; i<areas_needed_to_be_calculated; i++) {
//copy-paste earlie code snippet here
}
You all are giving me very in depth programs. I don't think they should be that long. I'm only in the 3rd chapter. Shouldn't they be a little more simple? My past assignments I was able to do in no time. This is just throwing me off because of the whole outer and inner loop thing. That's what I'm not understanding.
@H3avenlySoul: ignore greenleaf800073's program. It's not worth reading because of the way he declared month names array and the GOTO statement.
and I think the program's requirement is
display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
which means highMonth, lowMonth is not needed.
I guess the output should be
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Total xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
Average xx.x xx.x xx.x xx.x xx.x xx.x xx.x xx.x xx.x xx.x xx.x xx.x
@tntxtnt: It wouldn't look like that. The program doesn't need the total and average for each month. The program wants the total months (so how ever many years I put in when it ask me at the beginning multipled by 12), total inches of rainfall (ALL the numbers added together), and the average per month (total amount of rainfall for ALL months divided by (how ever many years I put in when it ask me at the beginning multipled by 12)).
When the program is executed this is how I had it pictured:
How many years: (enter a number)
Enter the amount of rainfall for each month:
Jan: (enter a number for every month)
Feb:
Mar:
Apr:
May:
June:
July:
Aug:
Sep:
Oct:
Nov:
Dec:
Total number of months: (program calculates)
Total inches of rainfall: (program calculates)
Average rainfall per month: (program calculates)
If I had put in 3 when it asked how many years, then Jan would show up 3 times, Feb would show up 3 times, and so on. So I would input a total of 36 numbers for inches of rainfall.
Also, how are you all posting the programs like it looks in the software where each line is numbered?
wrap around your code with [ code] tags
1 2 3
int main()
{
}
//change [code ] to [code]
[code ]int main()
{
}[/code]
.
how to calculate total inches
change double rainSum = 0; to double totalInches = 0;
1 2 3 4 5 6 7 8 9 10 11 12
for (int i=0; i<years; i++)
{
for (int j=0; j<12; j++)
{//you need brackets here
cout << "Amount of rainfall for " << monthNames[j] << ": ";
cin >> inches; //get a month rainfall amount
totalInches += inches; //because you don't have an array to store the rainfall amount of each month
//so after getting a month rainfall amount, you must add it to totalInches immidiately
}
}