473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"<<e ndl<<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<<"Pl ease Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"Th e Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
break;
case '2':
cout<<endl<<"Pl ease Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"Th e Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<end l;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
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 3409
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=readNumb er(); // 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"<<e ndl<<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<<"Pl ease Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"Th e Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
break;
case '2':
cout<<endl<<"Pl ease Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"Th e Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<end l;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
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"<<e ndl<<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<<"Pl ease Type In the Fahrenheight Temperature: ";
cin>>num1;
cout<<endl<<"Th e Temperature in Celcius is:
"<<(num1-32)*5/9<<endl<<endl;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
break;
case '2':
cout<<endl<<"Pl ease Type In the Celcius Temperature: ";
cin>>num2;
cout<<endl<<"Th e Temperature in Fahrenheight is:
"<<num2*9/5+32<<endl<<end l;
cout<<"Thank You for Using Temperature Converter v.1"<<endl<<end l;
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
1988
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 help me make it faster... f = open(filename) lines = f.readlines() def create_words(lines): cnt = 0 spl_set = '+' for content in lines:
6
3138
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 the number of 2 or 12 rolled, then display those numbers and average rolls. I didn't know how to do it exactly, but I came up with this.
21
2556
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 run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
1
3563
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 run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
1
6106
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. Please follow the specifications for assignment 3 as described below otherwise points will be taken off. Input from a file Please create an input file named input.txt using a text editor like notepad or the Visual C++ editor and put the following...
4
3516
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. 1.Several pairs (x,y) of numbers are to keyed in. Any pair with x=y terminates the loop. Determine and display counts of how many pairs satisfy x<y and how many pairs satisfy x>y. 2.Two numbers x and y are to be keyed in. If the sum of x and y is greater...
12
2003
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
1604
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: ---------------------------------------------------------------------- This program calculates the average of as many grades you wish to enter. First, enter the number of grades to process: 4
2
3945
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 Mickey 80 90 95 93 48 Minnie 78 81 11 90 73 Doc 92 83 30 69 87 Goofy 23 45 96 38 59 Duckey 60 85 45 39 67 Grumpy 27 31 52 74 83 Sunny 93 94 89 77 97 Piggy 79 85 28 93 82
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.