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: - 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 <<"\n\n\n";
-
}
-
-
if (temp=='F' || temp=='f')
-
{cout<<"\nEnter temperature in Celsius: ";
-
cin>>celsius;
-
-
fahr = 32 + celsius*9/5;
-
cout<<"\nTemperature in Fahrenheit is: ";
-
cout<<fahr <<"\n\n\n";
-
}
-
-
if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
-
cout<<"\nThe rules of the Matrix give you no third choice. "
-
<<"\nChoose F or C.\n\n\n";
-
}
10 1557
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: -
if (a == b) {
-
//... do stuff
-
}
-
else if (a == c) {
-
//... do other stuff
-
}
-
else {
-
//... do some other stuff
-
}
-
That would show your code like this: -
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 <<"\n\n\n";
-
}
-
-
if (temp=='F' || temp=='f') {
-
cout<<"\nEnter temperature in Celsius: ";
-
cin>>celsius;
-
-
fahr = 32 + celsius*9/5;
-
cout<<"\nTemperature in Fahrenheit is: ";
-
cout<<fahr <<"\n\n\n";
-
}
-
-
if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
-
cout<<"\nThe rules of the Matrix give you no third choice. "
-
<<"\nChoose F or C.\n\n\n";
-
}
-
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
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: -
if (a == b) {
-
//... do stuff
-
}
-
else if (a == c) {
-
//... do other stuff
-
}
-
else {
-
//... do some other stuff
-
}
-
That would show your code like this: -
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 <<"\n\n\n";
-
}
-
-
if (temp=='F' || temp=='f') {
-
cout<<"\nEnter temperature in Celsius: ";
-
cin>>celsius;
-
-
fahr = 32 + celsius*9/5;
-
cout<<"\nTemperature in Fahrenheit is: ";
-
cout<<fahr <<"\n\n\n";
-
}
-
-
if (temp!='F' && temp!='f' && temp!='C' && temp!='c')
-
cout<<"\nThe rules of the Matrix give you no third choice. "
-
<<"\nChoose F or C.\n\n\n";
-
}
-
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.
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: - 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.
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: - 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: - 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: - 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<<celsius <<"\n\n\n";
-
}
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.
Thanks Ganon. I set up the following variables: - 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: - 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<<celsius <<"\n\n\n";
-
}
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!
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....
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.
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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!...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |