445,898 Members | 2,012 Online
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 cout<<"\nEnter the amount of times you want to run this program:  "; cin>>num1;   for(cnt=1; cnt<=num1; cnt++)   {     cout<<"\n\nFahrenheit to Celcius - Press C.\n";     cout<<"Celsius to Fahrenheit - Press F.\n\n";     cin>>temp;       if (temp=='C' || temp=='c')           {cout<<"\nEnter temperature in Fahrenheit: ";         cin>>fahr;           celsius = (fahr-32)*5/9;         cout<<"\n\nTemperature in Celsius is:";         cout<>celsius;           fahr = 32 + celsius*9/5;         cout<<"\nTemperature in Fahrenheit is:  ";         cout<
10 Replies

 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 if (a == b) {   //... do stuff } else if (a == c) {   //... do other stuff } else {   //... do some other stuff }   That would show your code like this: Expand|Select|Wrap|Line Numbers cout<<"\nEnter the amount of times you want to run this program:  "; cin>>num1;   for(cnt=1; cnt<=num1; cnt++)   {     cout<<"\n\nFahrenheit to Celcius - Press C.\n";     cout<<"Celsius to Fahrenheit - Press F.\n\n";     cin>>temp;       if (temp=='C' || temp=='c') {         cout<<"\nEnter temperature in Fahrenheit: ";         cin>>fahr;           celsius = (fahr-32)*5/9;         cout<<"\n\nTemperature in Celsius is:";         cout<>celsius;           fahr = 32 + celsius*9/5;         cout<<"\nTemperature in Fahrenheit is:  ";         cout<

 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 if (a == b) {   //... do stuff } else if (a == c) {   //... do other stuff } else {   //... do some other stuff }   That would show your code like this: Expand|Select|Wrap|Line Numbers cout<<"\nEnter the amount of times you want to run this program:  "; cin>>num1;   for(cnt=1; cnt<=num1; cnt++)   {     cout<<"\n\nFahrenheit to Celcius - Press C.\n";     cout<<"Celsius to Fahrenheit - Press F.\n\n";     cin>>temp;       if (temp=='C' || temp=='c') {         cout<<"\nEnter temperature in Fahrenheit: ";         cin>>fahr;           celsius = (fahr-32)*5/9;         cout<<"\n\nTemperature in Celsius is:";         cout<>celsius;           fahr = 32 + celsius*9/5;         cout<<"\nTemperature in Fahrenheit is:  ";         cout<

 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 int fSum = 0, fTotal = 0; // No Fahrenheit numbers read, so no total and no sum int cSum = 0, cTotal = 0; // No Celcius numbers read, so no total and no sum double fAverage; // Nothing to calculate average with 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 int fSum = 0, fTotal = 0; // No Fahrenheit numbers read, so no total and no sum int cSum = 0, cTotal = 0; // No Celcius numbers read, so no total and no sum double fAverage; // Nothing to calculate average with 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     float fahr,celsius,fsum,csum;     char temp;     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                          if (temp=='C' || temp=='c')                               {cout<<"\nEnter temperature in Fahrenheit: ";                             cin>>fahr;                             ccount++;                               celsius = (fahr-32)*5/9;                             cout<<"\n\nTemperature in Celsius is:";                             cout<

 P: 20 Thanks Ganon. I set up the following variables: Expand|Select|Wrap|Line Numbers     float fahr,celsius,fsum,csum;     char temp;     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                          if (temp=='C' || temp=='c')                               {cout<<"\nEnter temperature in Fahrenheit: ";                             cin>>fahr;                             ccount++;                               celsius = (fahr-32)*5/9;                             cout<<"\n\nTemperature in Celsius is:";                             cout<

 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

 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

 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

 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