help getting average

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary

    #1

    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');
    }

  • default

    #2
    Re: help getting average

    Gary wrote:
    [color=blue]
    > 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.
    >[/color]

    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);

    Comment

    • Kai-Uwe Bux

      #3
      Re: help getting average

      Gary wrote:
      [color=blue]
      > 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>[/color]

      I take it, you meant:

      #include <iostream>

      using std::cin;
      using std::cout;
      using std::endl;
      [color=blue]
      > void main( )[/color]

      That should be

      int main ( )
      [color=blue]
      > {
      > 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');
      > }[/color]

      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

      Comment

      • Gary

        #4
        Re: help getting average

        Thank you so much. Got it to work!!

        Comment

        • Greg

          #5
          Re: help getting average


          Kai-Uwe Bux wrote:[color=blue]
          > Gary wrote:
          >[color=green]
          > > 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>[/color]
          >
          > I take it, you meant:
          >
          > #include <iostream>
          >
          > using std::cin;
          > using std::cout;
          > using std::endl;
          >[color=green]
          > > void main( )[/color]
          >
          > That should be
          >
          > int main ( )
          >[color=green]
          > > {
          > > 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');
          > > }[/color]
          >
          > 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.[/color]

          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

          Comment

          Working...