473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to separate two kinds of outputs and average them?

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
10 1557
AdrianH
1,251 Expert 1GB
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
shk253
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
3,652 Expert 2GB
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
shk253
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
shk253
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
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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
shk253
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
3,652 Expert 2GB
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
1,251 Expert 1GB
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

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

Similar topics

31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
2
by: Burt Lewis | last post by:
Hi, I have 2 xsl style sheets calling separate rss feeds and I use ASP to display. Problem is that the 1st xsl takes on the style of the 2nd xsl even though they are different formats. I...
8
by: nickdu | last post by:
I'm trying to isolate "applications" into their own application domain within a single process. I've quoted applications because it's a logical representation of an application. Basically it...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
36
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks!...
3
by: LurfysMa | last post by:
I would like to hear opinions on the tradeoffs of putting the tables, forms, and queries for several related datasets in separate databases vs one combined database. I am working on an...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.