How to separate two kinds of outputs and average them?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shk253
    New Member
    • Feb 2007
    • 20

    How to separate two kinds of outputs and average them?

    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:

    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";
    }
    Last edited by AdrianH; Feb 20 '07, 02:33 PM. Reason: Reindented code
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by shk253
    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:

    Code:
    if (a == b) {
      //... do stuff
    }
    else if (a == c) {
      //... do other stuff
    }
    else {
      //... do some other stuff
    }
    That would show your code like this:
    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";
    }
    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

    Comment

    • shk253
      New Member
      • Feb 2007
      • 20

      #3
      Originally posted by AdrianH
      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:

      Code:
      if (a == b) {
        //... do stuff
      }
      else if (a == c) {
        //... do other stuff
      }
      else {
        //... do some other stuff
      }
      That would show your code like this:
      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";
      }
      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.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        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:

        Code:
        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.

        Comment

        • shk253
          New Member
          • Feb 2007
          • 20

          #5
          Originally posted by Ganon11
          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:

          Code:
          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:

          Code:
              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:

          Code:
                                   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.

          Comment

          • shk253
            New Member
            • Feb 2007
            • 20

            #6
            Originally posted by shk253
            Thanks Ganon. I set up the following variables:

            Code:
                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:

            Code:
                                     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!

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by shk253
              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....

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                • shk253
                  New Member
                  • Feb 2007
                  • 20

                  #9
                  Originally posted by Ganon11
                  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.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    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.

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by shk253
                      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

                      Comment

                      Working...