By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

How to separate two kinds of outputs and average them?

P: 20
Hi all - I'm new to programming and still learning. I've made a program that lets the user convert temperature from F to C and C to F, as many times as the user wants. Most of the program is pretty much done but I am stuck on this part:

Question: I can't seem to figure out how to separate the fahrenheit and centigrade outputs, and averaging each of them separately. A fahrenheit average and a centigrade average.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. cout<<"\nEnter the amount of times you want to run this program:  ";
  2. cin>>num1;
  3.  
  4. for(cnt=1; cnt<=num1; cnt++)
  5.  
  6. {
  7.     cout<<"\n\nFahrenheit to Celcius - Press C.\n";
  8.     cout<<"Celsius to Fahrenheit - Press F.\n\n";
  9.     cin>>temp;
  10.  
  11.     if (temp=='C' || temp=='c')
  12.  
  13.         {cout<<"\nEnter temperature in Fahrenheit: ";
  14.         cin>>fahr;
  15.  
  16.         celsius = (fahr-32)*5/9;
  17.         cout<<"\n\nTemperature in Celsius is:";
  18.         cout<<celsius <<"\n\n\n";
  19.         }
  20.  
  21.     if (temp=='F' || temp=='f')
  22.         {cout<<"\nEnter temperature in Celsius: ";
  23.         cin>>celsius;
  24.  
  25.         fahr = 32 + celsius*9/5;
  26.         cout<<"\nTemperature in Fahrenheit is:  ";
  27.         cout<<fahr <<"\n\n\n";
  28.         }
  29.  
  30.     if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
  31.         cout<<"\nThe rules of the Matrix give you no third choice. "
  32.                 <<"\nChoose F or C.\n\n\n";
  33. }
Feb 20 '07 #1
Share this Question
Share on Google+
10 Replies


AdrianH
Expert 100+
P: 1,251
Question: I can't seem to figure out how to separate the fahrenheit and centigrade outputs, and averaging each of them separately. A fahrenheit average and a centigrade average.
Ok, I'm not sure if I understand what you are saying by averaging each seperatly. However, your indention style is lacking. I had to reindent your code and still I couldn't read it until I realised you had put the open brace '{' and some code right after it on the same line. It didn't make much sense and makes it harder to understand quickly. I would suggest setting up your blocks like this:

Expand|Select|Wrap|Line Numbers
  1. if (a == b) {
  2.   //... do stuff
  3. }
  4. else if (a == c) {
  5.   //... do other stuff
  6. }
  7. else {
  8.   //... do some other stuff
  9. }
  10.  
That would show your code like this:
Expand|Select|Wrap|Line Numbers
  1. cout<<"\nEnter the amount of times you want to run this program:  ";
  2. cin>>num1;
  3.  
  4. for(cnt=1; cnt<=num1; cnt++)
  5.  
  6. {
  7.     cout<<"\n\nFahrenheit to Celcius - Press C.\n";
  8.     cout<<"Celsius to Fahrenheit - Press F.\n\n";
  9.     cin>>temp;
  10.  
  11.     if (temp=='C' || temp=='c') {
  12.         cout<<"\nEnter temperature in Fahrenheit: ";
  13.         cin>>fahr;
  14.  
  15.         celsius = (fahr-32)*5/9;
  16.         cout<<"\n\nTemperature in Celsius is:";
  17.         cout<<celsius <<"\n\n\n";
  18.     }
  19.  
  20.     if (temp=='F' || temp=='f')    {
  21.         cout<<"\nEnter temperature in Celsius: ";
  22.         cin>>celsius;
  23.  
  24.         fahr = 32 + celsius*9/5;
  25.         cout<<"\nTemperature in Fahrenheit is:  ";
  26.         cout<<fahr <<"\n\n\n";
  27.     }
  28.  
  29.     if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
  30.         cout<<"\nThe rules of the Matrix give you no third choice. "
  31.                 <<"\nChoose F or C.\n\n\n";
  32. }
  33.  
which as you can see is more readable.

You should use better indenting to keep track of your code blocks. A couple of programmes you can download or may have on your system that do reindenting for you are: indent and emacs (can reindent blocks of code by marking the block and pressing C-M-\).

But back to your original question. Can you please explain what you mean a bit more clearly. Thanks.


Adrian
Feb 20 '07 #2

P: 20
Ok, I'm not sure if I understand what you are saying by averaging each seperatly. However, your indention style is lacking. I had to reindent your code and still I couldn't read it until I realised you had put the open brace '{' and some code right after it on the same line. It didn't make much sense and makes it harder to understand quickly. I would suggest setting up your blocks like this:

Expand|Select|Wrap|Line Numbers
  1. if (a == b) {
  2.   //... do stuff
  3. }
  4. else if (a == c) {
  5.   //... do other stuff
  6. }
  7. else {
  8.   //... do some other stuff
  9. }
  10.  
That would show your code like this:
Expand|Select|Wrap|Line Numbers
  1. cout<<"\nEnter the amount of times you want to run this program:  ";
  2. cin>>num1;
  3.  
  4. for(cnt=1; cnt<=num1; cnt++)
  5.  
  6. {
  7.     cout<<"\n\nFahrenheit to Celcius - Press C.\n";
  8.     cout<<"Celsius to Fahrenheit - Press F.\n\n";
  9.     cin>>temp;
  10.  
  11.     if (temp=='C' || temp=='c') {
  12.         cout<<"\nEnter temperature in Fahrenheit: ";
  13.         cin>>fahr;
  14.  
  15.         celsius = (fahr-32)*5/9;
  16.         cout<<"\n\nTemperature in Celsius is:";
  17.         cout<<celsius <<"\n\n\n";
  18.     }
  19.  
  20.     if (temp=='F' || temp=='f')    {
  21.         cout<<"\nEnter temperature in Celsius: ";
  22.         cin>>celsius;
  23.  
  24.         fahr = 32 + celsius*9/5;
  25.         cout<<"\nTemperature in Fahrenheit is:  ";
  26.         cout<<fahr <<"\n\n\n";
  27.     }
  28.  
  29.     if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
  30.         cout<<"\nThe rules of the Matrix give you no third choice. "
  31.                 <<"\nChoose F or C.\n\n\n";
  32. }
  33.  
which as you can see is more readable.

You should use better indenting to keep track of your code blocks. A couple of programmes you can download or may have on your system that do reindenting for you are: indent and emacs (can reindent blocks of code by marking the block and pressing C-M-\).

But back to your original question. Can you please explain what you mean a bit more clearly. Thanks.


Adrian
Thanks Adrian. I'll try to do that next time.

The program asks you how many times you want to run it. Lets say you want to run it 5 times. The program asks you if you want to convert from f to c or c to f. It does this 5 times, user inputs temperature, program outputs and then closes. The program basically gives you two kinds of outputs: Fahrenheit and Celcius. I need to take all the fahrenheit outputs and get an average. I need to take all the Celcius outputs and get an average.

Thanks.
Feb 20 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Well, in order to find an average, you need to have a sum and a total number. Since you need 2 averages, you will need 2 sums and 2 totals. You can have 6 variables, defined as follows:

Expand|Select|Wrap|Line Numbers
  1. int fSum = 0, fTotal = 0; // No Fahrenheit numbers read, so no total and no sum
  2. int cSum = 0, cTotal = 0; // No Celcius numbers read, so no total and no sum
  3. double fAverage; // Nothing to calculate average with
  4. double cAverage; // Nothing to calculate average with
In the loop, if the user wants fahrenheit, then you can add the result to fSum and increment fTotal by 1. The same goes for Celcius.

Finally, after the user has entered their numbers, you can calculate the average. Remember that average = sum / total.
Feb 20 '07 #4

P: 20
Well, in order to find an average, you need to have a sum and a total number. Since you need 2 averages, you will need 2 sums and 2 totals. You can have 6 variables, defined as follows:

Expand|Select|Wrap|Line Numbers
  1. int fSum = 0, fTotal = 0; // No Fahrenheit numbers read, so no total and no sum
  2. int cSum = 0, cTotal = 0; // No Celcius numbers read, so no total and no sum
  3. double fAverage; // Nothing to calculate average with
  4. double cAverage; // Nothing to calculate average with
In the loop, if the user wants fahrenheit, then you can add the result to fSum and increment fTotal by 1. The same goes for Celcius.

Finally, after the user has entered their numbers, you can calculate the average. Remember that average = sum / total.
Thanks Ganon. I set up the following variables:

Expand|Select|Wrap|Line Numbers
  1.     float fahr,celsius,fsum,csum;
  2.     char temp;
  3.     int cnt,num1,fcount,ccount;
What I'm having trouble with is how to use the function count. I can use it in the if statement but outside it, I'm a bit shaky. I imagine I would have something like the following:

Expand|Select|Wrap|Line Numbers
  1.                          if (temp=='C' || temp=='c')
  2.  
  3.                             {cout<<"\nEnter temperature in Fahrenheit: ";
  4.                             cin>>fahr;
  5.                             ccount++;
  6.  
  7.                             celsius = (fahr-32)*5/9;
  8.                             cout<<"\n\nTemperature in Celsius is:";
  9.                             cout<<celsius <<"\n\n\n";
  10.                             }
Can you tell me if that is the correct way count++ is used. In the above example, what I'm trying to do is to increment ccount variable one time in the loop. Is it correct the way I used it? If not, could you please correct me or direct me to a resource that shows how count is properly used?

Thanks.
Feb 20 '07 #5

P: 20
Thanks Ganon. I set up the following variables:

Expand|Select|Wrap|Line Numbers
  1.     float fahr,celsius,fsum,csum;
  2.     char temp;
  3.     int cnt,num1,fcount,ccount;
What I'm having trouble with is how to use the function count. I can use it in the if statement but outside it, I'm a bit shaky. I imagine I would have something like the following:

Expand|Select|Wrap|Line Numbers
  1.                          if (temp=='C' || temp=='c')
  2.  
  3.                             {cout<<"\nEnter temperature in Fahrenheit: ";
  4.                             cin>>fahr;
  5.                             ccount++;
  6.  
  7.                             celsius = (fahr-32)*5/9;
  8.                             cout<<"\n\nTemperature in Celsius is:";
  9.                             cout<<celsius <<"\n\n\n";
  10.                             }
Can you tell me if that is the correct way count++ is used. In the above example, what I'm trying to do is to increment ccount variable one time in the loop. Is it correct the way I used it? If not, could you please correct me or direct me to a resource that shows how count is properly used?

Thanks.
Here's the mistake!!! I needed to set fcount=0 and ccount=0 before adding fcount++ and ccount++ in the loop!

Next step for me to figure out is how to add each of the Celsius outputs to csum and the Fahrenheit outputs to fsum. Any hint would be appreciated!
Feb 20 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Here's the mistake!!! I needed to set fcount=0 and ccount=0 before adding fcount++ and ccount++ in the loop!

Next step for me to figure out is how to add each of the Celsius outputs to csum and the Fahrenheit outputs to fsum. Any hint would be appreciated!
Um, add them together?

I think you want to re-phrase that question....
Feb 20 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
Right, you won't be outputting to fsum and csum, you'll be adding the result to fsum and csum. Make sure you set these both to 0 like you did with fcount and ccount before you do your calculations.
Feb 20 '07 #8

P: 20
Right, you won't be outputting to fsum and csum, you'll be adding the result to fsum and csum. Make sure you set these both to 0 like you did with fcount and ccount before you do your calculations.
You guys are awesome!! I used += operator to add the results to the fsum and csum. Now I can proceed to do the arithmetic.

My professor allows us to ask him questions if we've made an effort but he seldom responds to us via email... the last two times I went to his office hours, the dude was awol.

Thanks for guiding me in the right direction! I'll definitely help others when I get better.
Feb 20 '07 #9

Ganon11
Expert 2.5K+
P: 3,652
Glad to see you got it, and even more glad to see you're willing to stick around! Just remember, a lot of people here aren't 'experts', and a lot of people ask questions that even new programmers can answer, so don't feel discouraged.
Feb 20 '07 #10

AdrianH
Expert 100+
P: 1,251
You guys are awesome!! I used += operator to add the results to the fsum and csum. Now I can proceed to do the arithmetic.

My professor allows us to ask him questions if we've made an effort but he seldom responds to us via email... the last two times I went to his office hours, the dude was awol.

Thanks for guiding me in the right direction! I'll definitely help others when I get better.
Great going shk, you are on your way to being a great programmer. :)


Adrian
Feb 21 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.