473,224 Members | 1,746 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,224 software developers and data experts.

help getting average

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 start, and my
stpid book doesnt seem to help.

here is my program

#include <iostream.h>
void main( )
{
int num1, num2, count;
char choice, again;
count=1;

do
{cout<<"This Program will Convert Temperature"<<endl<<endl;
cout<<"Press 1 to Convert Fahrenheight to Celcius"<<endl;
cout<<"Press 2 to Convert Celcius to Fahrenheight"<<endl<<endl;
cin>>choice;
switch (choice)
{ case '1':
cout<<endl<<"Please Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"The Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
case '2':
cout<<endl<<"Please Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"The Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
default:
cout<<"Invalid Entry, Try Again";
}

count++;
cout<<"Type N to stop program";
cin>>again;
} while (again!= 'N' && again!='n');
}

Feb 15 '06 #1
4 3374
Gary wrote:
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 start, and my
stpid book doesnt seem to help.


Consider this:
To print the average you need to calculate it first. For this you need to
divide the sum of all values inputted by the amount of values inputted. The
sum is variable, increasing by the inputted value every time the loop runs.
The amount is variable as well, increasing by one every time the loop runs.

Small fragment example:

int sum, count; sum=0; count=0;
do {
count++;
int number=readNumber(); // Or use cin or whatever
sum=sum+number;
// IMPORTANT! If your number is integer, you need to convert
// to double first to avoid getting integer division
cout << "The average is " << sum/(double)count << endl;
} while (runLoop);

Feb 15 '06 #2
Gary wrote:
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 start, and my
stpid book doesnt seem to help.

here is my program

#include <iostream.h>
I take it, you meant:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
void main( )
That should be

int main ( )
{
int num1, num2, count;
char choice, again;
count=1;

do
{cout<<"This Program will Convert Temperature"<<endl<<endl;
cout<<"Press 1 to Convert Fahrenheight to Celcius"<<endl;
cout<<"Press 2 to Convert Celcius to Fahrenheight"<<endl<<endl;
cin>>choice;
switch (choice)
{ case '1':
cout<<endl<<"Please Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"The Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
case '2':
cout<<endl<<"Please Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"The Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
default:
cout<<"Invalid Entry, Try Again";
}

count++;
cout<<"Type N to stop program";
cin>>again;
} while (again!= 'N' && again!='n');
}


In order to compute an average of the temperatures, you need to
a) express all the different temperature using the same units, e.g, Celsius.
b) at the end, compute and output the average.

The most straight forward way to do that, would be to store the temperatures
in a std::vector<> while they are entered and use them at the end to form
an average.
Best

Kai-Uwe Bux
Feb 15 '06 #3
Thank you so much. Got it to work!!

Feb 15 '06 #4

Kai-Uwe Bux wrote:
Gary wrote:
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 start, and my
stpid book doesnt seem to help.

here is my program

#include <iostream.h>


I take it, you meant:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
void main( )


That should be

int main ( )
{
int num1, num2, count;
char choice, again;
count=1;

do
{cout<<"This Program will Convert Temperature"<<endl<<endl;
cout<<"Press 1 to Convert Fahrenheight to Celcius"<<endl;
cout<<"Press 2 to Convert Celcius to Fahrenheight"<<endl<<endl;
cin>>choice;
switch (choice)
{ case '1':
cout<<endl<<"Please Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"The Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
case '2':
cout<<endl<<"Please Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"The Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<endl;
break;
default:
cout<<"Invalid Entry, Try Again";
}

count++;
cout<<"Type N to stop program";
cin>>again;
} while (again!= 'N' && again!='n');
}


In order to compute an average of the temperatures, you need to
a) express all the different temperature using the same units, e.g, Celsius.
b) at the end, compute and output the average.

The most straight forward way to do that, would be to store the temperatures
in a std::vector<> while they are entered and use them at the end to form
an average.


One drawback with retaining all of the previous temperatures though is
that both the amount of memory needed by the vector to hold the
previous results and the amount of time needed to update their average
will keep increasing as each new data point is added. In other words,
our program would not be able to run indefinitely since at some point
it would run out of memory or become too slow for anyone to want to use
it.

A more efficient solution would be to maintain a running average of the
temperature conversions. For a running average the program needs to
keep only a running total of the number of previous conversions
performed. Then each new conversion performed adds 1.0/runningTotal *
conversionValue to the running average (which was initialized to 0
before the first conversion). An actual C++ implementation of this
technique has been left as an exercise for the reader.

Greg

Feb 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: pkilambi | last post by:
I wrote this function which does the following: after readling lines from file.It splits and finds the word occurences through a hash table...for some reason this is quite slow..can some one...
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...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
4
by: danbuttercup | last post by:
Hi everyone I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start. ...
12
by: miguelguerin | last post by:
Im having trouble getting the input from one file to another. Main file: package assigment3; /** * * @author mguer017 */ public class Main {
1
by: Sleepwalker817 | last post by:
Hello, I am trying to create a program that is supposed to calculate and print the average of several grades entered by the user. The output is supposed to look something like this:...
2
by: Deathtotock | last post by:
I am inputing from a file and I am outputting to a file. well when i run the program it just puts the headers up there and no names or grades. This is the input. Balto 85 83 77 91 76...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.