# Noob in need of help.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-21-2009
gec5741
Noob in need of help.
Noob needs help.

I have a situation where I'm asked to ask the user to enter in the number of cities to calculate an average temperature for and then use each of the 3 basic loop structures.

Now where I am is this. I ask the suer how many cities? Then I have a basic for loop wich is working. I can't seem to figure out how to get the program to add up all the temps to do the average. I am also not sure bout the variables.

I'm very new to this. Here is my code

Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {     //declare variables     int Temp = 0;     int averageTemp = 0;     int numCities = 0;     //double sum = 0; not sure?         cout << "Enter the number of cities you would to average: ";     cin >> numCities;             for ( int i=0; i<=numCities; i++ )         {             cout <<"Enter the Temp: ";             cin >> Temp;         }        //for ( int Temp=0; //this is where I'm stuck             //averageTemp = (temp + sum) /numCities;               cout << "Average temp: " << averageTemp <<endl;                       system("pause");     return 0;```
• 01-21-2009
Akkernight
Since you want more than one temperature
Code:

` cin >> Temp;`
won't work, since you'll only replace the current inputted temperature with the newly inputted one, you should use a vector for this ;)
• 01-21-2009
gec5741
That makes sense. But I have to kinda stay on what we have learned so far. And this is only my 3rd lab in this class. We havn't covered vectors. Is there a more basic way to accomplish this? I am at work and do not have my book. Wanted to get a jump on this so I'm not up to late tonight.
• 01-21-2009
rags_to_riches
If all you need to do is print the average, then a vector or array is not necessary. Instead you would do the following:

Create a variable to store the total temperature and initialize it to 0.
Read number of cities into another variable.
Loop that many times asking for the temperature, adding the entered result to the total.
Divide total by the number of cities to get average.
• 01-21-2009
Edo
Heya, I'm new too! The problem here is that every time the user enters the temperature, it gets written to Temp -- you are accidentally overwriting Temp every time.

Code:

```        for ( int i=0; i<=numCities; i++ )         {             cout <<"Enter the Temp: ";             cin >> Temp; // <---- right here         }```
You're also going to have an extra city because of this:
Code:

` for ( int i=0; i<=numCities; i++ )`
If the user entered 1 city, the code would run once at i = 0, then again at i = 1 ...

Something like this would work better:
Code:

```        int totalTemp = 0;         for ( int i=0; i<numCities; i++ ) // i < numCities instead of <=, because it's starting at 0 instead of 1         {             cout <<"Enter the Temp: ";             cin >> Temp;             totalTemp = totalTemp + Temp;          }         averageTemp = totalTemp / numCities;         cout << "Average temp: " << averageTemp <<endl;```
• 01-21-2009
gec5741
Perfect! Thank you.

I just changed a couple variables to double to accomadate for decimal and were in buisness. I thought I tried somethig similar but I'm sure I had something wrong. I am only 3 classes in my first programming class ever so I'm still real green :) But I like it a lot and love learning!
• 01-21-2009
gec5741
Now I have to figure out how to do the same loop in a while and do while loop.
Here is the code I added for my while loop but it's not doing the average for the while loop. adding the temps together it seems and spitting that out as the averate. Here's the code.

Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {     //declare variables     int Temp = 0;     double averageTemp = 0;     int numCities = 0;     double totalTemp = 0;     int i = 0;         cout << "Enter the number of cities you want to average: ";     cin >> numCities;         for ( int i=0; i<numCities; i++ )         {         cout <<"Enter the Temp: ";         cin >> Temp;         totalTemp = totalTemp + Temp;         }                  averageTemp = totalTemp / numCities;         cout << "Average temp: " << averageTemp <<endl;               while ( i<numCities)         {         cout <<"Enter the Temp: ";         cin >> Temp;         totalTemp = totalTemp + Temp;         i++;         }                          averageTemp = totalTemp / numCities;           cout << "Average temp: " << averageTemp <<endl;                         system("pause");     return 0;         }```
• 01-21-2009
tabstop
And why do you believe that totalTemp is 0 when you start the while loop?
• 01-22-2009
gec5741
Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.
• 01-22-2009
matsp
Quote:

Originally Posted by gec5741
Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.

All variables need to be assigned a value before you "use them" - that is, before you compare it with something else (including zero) or use it for calculations. Otherwise it will contain some random garbage [1], and most likely not do at all what you expect.

[1] It is not random in the sense that a random number is, but rather in the sense that "you can't tell me what it will be without knowing A LOT about what goes on before you get to the usage of the variable", and also, it may well change if you add or remove some other variables around the variable we talk about. Some compilers intentionally fill undefined variable memory with some recognizable pattern - in which case the value is just some "strange number" every time.

--
Mats
• 01-22-2009
Phyxashun
What you want is something like this. Notice the reinitialization of sumTemp to 0.

Code:

```#include <iostream> int main() {     int    cityTemp    = 0;     double avgTemp    = 0;     int    numCities  = 0;     double sumTemp    = 0;     std::cout << "Enter the number of cities you want to average: ";     std::cin >> numCities;     for (int i = 0; i < numCities; i++)     {         std::cout << "Enter the Temp: ";         std::cin >> cityTemp;         sumTemp += cityTemp;     }     avgTemp = sumTemp / numCities;     std::cout << "Average temp: " << avgTemp << std::endl;     int counter = 0;     sumTemp  = 0;     while (counter <= numCities)     {         std::cout << "Enter the Temp: ";         std::cin >> cityTemp;         sumTemp += cityTemp;         counter++;     }     avgTemp = sumTemp / numCities;     std::cout << "Average temp: " << avgTemp << std::endl;     return 0; }```
• 01-22-2009
tabstop
Quote:

Originally Posted by gec5741
Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.

I did not mean when you declared your variables, as Phyxashun pointed out -- before the while loop is not where you declared your variables.

To answer your question, you need to initialize variables to a value if you intend to use them. For instance, you don't have to initialize numCities, because you're not using it before your read into the variable. Same with temp. But you do have to initialize totalTemp, because the first thing you do with it is use it (totalTemp + temp).
• 01-22-2009
gec5741
Thank you,

I think i'm starting to see. Weird thing is the while loop dosent' seem to want to put the avreage in a decimal if needed. it's averageing a temp of 2 and 3 as average = 3. Where the for loop will display it as 2.5.
• 01-22-2009
gec5741
I'm getting there. I have the while loop working now. But my do while loop won't bump the appriate amount of times. I'll get it.
• 01-22-2009
Phyxashun
The easy solution is to use a cast:

Code:

`avgTemp = (double)sumTemp / (double)numCities;`
The better solution (to some) would be to make all the variable doubles.

Both would give you the fraction you are looking for.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last