beginner problem

i'm new to C++ and i'm taking the C++ tutorial on the net lately. i have a question regarding switch case. i'm not really familiar with this function and it's quite confusing. can someone help me out on this question?
========================================
Tutorial question:-
A wether station records the temperature every hour. Read the 24 temperatures recorded during one day-night period and output the low, high and average temperature for the period.Example:-
<20 (low temperature)
<40 (high temperature)
20-40 (average temperature)
this is only an example and it's not necessary to follow the temperature. any temperature will do.
i know how to use the switch case i'm confusing on how it can read the temperature during one day-night and output the low, high and average temperature? does it require loop?
nikisunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nikisunAuthor Commented:
Edited text of question.
0
mohitdasCommented:
see, from 24 times, its clear that you have to record the temperature for every hour. so you can use a loop for 24 times with frequency of 1 hr. i.e  for example
for( hr = 0; hr < 24; hr++ ) { do the nesessary action};
and i'm sure this will perfectly work if u can manage it.
G'Luck.
0
nikisunAuthor Commented:
how about the switch case? can the for loop combine together with the switch case?
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

mohitdasCommented:
yup. u have to use the case structure inside the for loop.
like
for ( ... ) {
.....
......
   select( .. ) {
    case a:
      low;
      break;
    case b:
      high;
      break;
    case c:
      average;
      break;
    }

}

is that clear ?? any further querry is welcome.  :- )

0
graham_kCommented:
hmm, I would guess that you want to use ranges in your CASEs.

Something like

/* what's the lowest practical temp we can measure ?*/
#define LOWEST_TEMP     -75

for (int i = 0; i < 24; i++)
  switch (temp[i])
  {
    case LOWEST_TEMP .. 20: low++: break;
    case  20 .. 40: average++; break;
    default: high++; break;
  }

This is  a lousy example for a switch statement, though. I'd have coded it with IF THEN ELSE myself.  Hmmm, maybe they're afetr something else. Anyone else have any ideas?
0
mohitdasCommented:
well this can be done in anyway and u can use any loop as per your convinience. as u asked about the case structure, i have used that. but for a small thing like this, u can either use if-then-elseif-else  structure.
 
0
graham_kCommented:
hi Mohitdas, please don't think that I was trying to steal your points or anything - just trying to help out.

Like I said, I - personally - would code the whole thing without a switch, which is why I think that it is a bad example for an exercise.

The only problem with your code is that the CASE statements check for exact values, so you would need something like:

  case 20:  /* all of tehse do nothing & fall through ... */
  case 21:
  case 22:
  .....
  case 38:
  case 39: average++; break;

That's why I used ranges, which I personally find messy on SWITCH/CASE.

So, the questioner really has to try to understand what the course is asking for when they say taht he should use switch/case.  I'd like to see him post the suggested answer here when it becomes available.
0
nietodCommented:
>> I would guess that you want to use ranges in your CASEs
>>   *   *   *
>>  switch (temp[i])
>> {
>>     case LOWEST_TEMP .. 20: low++: break

Except C++ doesn't allow ranges in its case statements.  That is not legal.

>> I - personally - would code the whole thing without a switch
Agreed.  You don't want to use a switch() statement when figuring out the low, average, and high values.  It is not appriate for that.  That is because you do not have a small number of distinct values that termine each case.   However as, mohitas suggested, you could use the switch() statement in a "menu" that would allow the user to request which value (low, average, high) should be printed.  
0
nikisunAuthor Commented:
the tutorial stated that i must use switch case. i still don't get it. i'm getting more confusing. all i wanna do is, read the 24 temperature recorded during one day-night period and output the low, high and average temperature for the period. but the problem is how can i read and output the temperature? please tell me in a straightforward way coz i'm quite dummy on understanding confusing tutorial.
0
mohitdasCommented:
i think ur problem is u don't know how to read 24 values and store it in an array or something like that. then u don't know how to calculate lowest value, highest value and average of 24 numbers. and after that u don't know how to print it.
if this is the case, then u should know the basic things like how to use cin and cout statements or reading from a file or like that and then try to solve the problem. well i can provide you with complete program for it using classes or simply like that. but u should try it urself, as its a simple one.
G'Luck.
0
nietodCommented:
>> The tutorial stated that i must use switch case
Tht seems very unlikely, at least for the part you've mentioned.  Perhaps they were thinking about a different part.  Or perhaps you mistunderstood.  

Could you post the totorial question?  That might help us to help you.
0
nikisunAuthor Commented:
to mohitdas:-
as i told you, i'm a beginner in learning C++.of course i know what is cin(call in) and cout(call out). i just don't understand what you are trying to say. you didn't explain more and said that i don't know anything at all. this is unsatisfactory.
----------------------------------------
to nietod:-
sorry, i made a mistake. the tutorial didn't stated what function should be used. any function will do. for your information, this is my tutorial question.
A wether station records the temperature every hour. Read the 24 temperatures recorded during one day-night period and output the low, high and average temperature for the period.
i hope you can help me out.
0
mohitdasCommented:
i'm really sorry if i hurt you. thats not what i meant. anyway .. i'll provide you the answer. but i do have some important work now. i'll try it after that.
0
nietodCommented:
Okay, no switch()

Let me give you some "pieces"  see if you can fit them together.  If not let me know.

first of all you are going to need to delcare 3 numeric variables to store the 3 values of interest (low, high, average).  I'm not sure if these should be type int or type double.  it depends on whethor or not the temperatures will always be integer or if they might be decimal.  Do you know?

You will need to write a loop that reads the 24 values.  each time it reads a value, it will determine if the value is the new low and/or if it is the new high.  it will also ADD the value to the average.  (This does not give you an average.  i know that!)

To figure out if the value is the new low (or new high) you just compare it with the "current" low (or high) value in an if () statement.  if the value is lower than the current low (or higher than the current high), change the current low (or current high) to the new value.  (This has one problem.  It doesn't work right for the first value.  Can you fix that?  If not at least try to write it woithout the fix.)




0
nietodCommented:
Opps forgot one detail   After the loop ends the average doesn't hold the average.  It holds the sum (addition) of the 24 values.  You have to convert that to an average.  Do you know how to do that.

>> 'll provide you the answer.
He doesn't need the answer.  He needs to find the anwer.  There is a big difference.
0
nikisunAuthor Commented:
to mohitdas:
nevermind..it's alright. no big deal.
----------------------------------------
to nietod:
i have try to write the program but i'm not sure whether it is correct or not. please correct me as soon as possible if i'm wrong.

#include <iostream.h>
main()
{
short total,high,low;
short temperature[24];
                     
for (short x=0;x<=23;x++)
{
cout<<"Enter temperature:"<<endl;
cin>>temperature;
}
                       
total = high = low = *temperature;
for (short x=1;x<=23;x++)
high = current temperature
high = high
low = temperature[x] < low;
total+=temperature[x];
}
cout<<"Average temperature is:"<<total/24<<endl;
cout<<"High is: "<<high<<endl;
cout<<"Low is: "<<low<<endl;
}
0
graham_kCommented:
oooooooops!!! sorry!!! ranges on cases are Pascal, aren't they? Sorry about that :-)
0
nietodCommented:
Pascal does allow ranges in its case statment.  But you want to avoid large ranges.  A range should only cover a few values   (Maybe its not such a big deal these days, now with good optimizers, but when I was writting in pascal it was ver innefficient to use large ranges.)
0
nikisunAuthor Commented:
then, could you please me how should i do?
0
nietodCommented:
nikisun,
   Sorry, I didn't mean to ignore you.  I didn't see your previous post, the one with the code.  

Inside the loop

for (short x=0;x<=23;x++)
{
   cout<<"Enter temperature:"<<endl;
   cin>>temperature;
}

You need to "do the math".  i.e you need to figure out the low, high and total values.  I'l get you started.

for (short x=0;x<=23;x++)
{
   cout<<"Enter temperature:"<<endl;
   cin>>temperature;
   if (temperature < low) // If new temp. is lower than previous low,
      low = temperature; // Save a new low temperature.
}

Can you add the logic to handle the high and the total?

Now you don't want that 2nd loop.  The stuff you were doing there needs to be in the 1st loop--sort of.

The rest is good.

When you get this loop done it will be almost finished.  The only problem is that it won't handle the first value it reads very well.  but get this much done.
0
mohitdasCommented:
well i hope the problem is solved now.
0
graham_kCommented:
off topic (soory), but just out of curiosity, nietod, why should the size of the range make any diffefrence - think of the generated assembler. It is basically two CMPs - against the low & high of the range.
0
ozoCommented:
That could be one implementation.
If there are many such ranges a jump table could be more efficient.
A clever compiler might find an optimal combination of tables and range compares, balancing speed and size.
0
nikisunAuthor Commented:
then, does my program correct or not?
0
nietodCommented:
>> It is basically two CMPs -
>> against the low & high of the range
If it is done that way.

A pascal case statement was designed to be translated into a jump table, so you need an entry for every case.   With optimizing compilers it could be translated into a bunch of comparisons--or a hybrid of the two, but originally, that would not have been feasable.

>> my program correct or not?
Not the last one you posted. A good start, but not finished.  Did you see the code I suggested?
0
nikisunAuthor Commented:
i saw your code put i'm not really understand what you are going to tell about. i have try it but it can't run too.
0
nietodCommented:
Alright this code should work to do 1 of the 3 things you need to do.  This will figure out the low value.   Can you make it figure out the high and average?

int main()
{
   short total,high,low;
   short temperature;
   bool FirstTime = true;
                                         
   for (short x=0;x<=23;x++)
   {
      cout<<"Enter temperature:"<<endl;
      cin>>temperature;
      if (FirstTime || temperature < low)
         low = temperature; // Save a new low temperature.
      FirstTime = false;
   }

   cout<<"Average temperature is:"<<total/24<<endl;
   cout<<"High is: "<<high<<endl;
   cout<<"Low is: "<<low<<endl;
   return 0;
}
0
nietodCommented:
Try to make it do the high and the average and post what you get done.
0
nikisunAuthor Commented:
does the high and average step same as the low step that you wrote?
0
nietodCommented:
What do you meant by "step"?

You need to add code to the design I showed you to make the high and average work.  
0
nikisunAuthor Commented:
it is the same method as the low program that you did?
0
nietodCommented:
High is similar to low, you need to think about it.  Average is similar to what you already had for average.
0
nikisunAuthor Commented:
sorry.....i have misunderstood the tutorial question. i think this is the correct one, but the problem i have now is why the Result of the word low,high and average temperature automatically jump down each time i enter the temperature? why can't it put on the same row? this is the program i done. please help me out.
--------------------------
#include<iostream.h>
main()
{
int time=1,temp;
char zone[4]="a.m";
cout<<"Enter 24 hour temperature\n\n";
cout<<"Time\t\tTemperature\t\tResult\n";
cout<<time<<".00"<<zone<<"\t\t";
cin>>temp;
for(time=2,temp;time<=24;time++)
{
if(temp<20)
cout<<"Low temperature\n";
else if (temp>40)
cout<<"High temperature\n";
else
cout<<"Average temperature\n";
cout<<time<<".00"<<zone<<"\t\t";
cin>>temp;
}
}
0
nietodCommented:
>> why the Result of the word low,high and
>> average temperature automatically jump
>> down each time i enter the temperature?
Because you press <Enter> (return) after each number you enter.  So the cursor moves down to the next line after you enter the number.  so the number you ouput (write to the screen) will be on the next line down.

There is nothing you can do about this in standard C++.  That is the way that standard C++ I/O (input/output) works.   When you write code that is designed to work on particular types of computers, like for Windows or for Macintosh, there are non-standard procedures you can use to help you move the cursor about and make I/O a little fancier.  But that's probably not a good concern for you at this point.

The code however, is not right.  You have a couple of problems.  First of all you are displaying the low,high, and average temperature before the use is done entering the 24 temperatures.  That isn't right.  You don't know any of these values until the user is done entering all 24 inputs.

The program should obtain all 24 values and "work" with them, THEN it should print out its results.

Another problem is that you are comparing the temperature to 20 to figure out if it is low and to 40 to figure out if it is high?  Why?  the original question didn't say to do that.  The original question stated

>> A wether station records the temperature every
>> hour. Read the 24 temperatures recorded during
>> one day-night period and output the low, high
>> and average temperature for the period.

There is no mention of 20 or 40 or any other temperatures.  You are supposed to print out the lowest value of all 24 values and the highest values of all 24 values.  So you must look at all 24 values and figure out which one is the lowest and print that one.  You must look at all 24 values and figure out which one is the highest and print out that one.

Look back at my code.  That is what it did to find the lowest value.  It stores the first value it gets in "low"  Because when only 1 value has been entered, that value has to be the lowest.  Then when the 2nd number is entered, it looks to see if this new number is lower than the previously recorded lowest (the first value).  If this 2nd number is lower, it records it in "low" as the new lowest value.  If the 2nd number is higher, it keeps the older "low" the same.  Then it does the same thing with the next number and the next and the next...  On each number it looks to see if the numer is lower than the lowest of all the previous values.  If it is, it records a new low value.  if not, it keeps the previous old low.

Does that make sense?

I'm leaving tomorrow afternoon (USA) and will be gone about 5 days.  I won;t be able to help you during that time.
0
nikisunAuthor Commented:
i program the code you gave but still can't manage to run. it has error on it. it can't run.
0
nietodCommented:
What is the error?
0
nikisunAuthor Commented:
the calculation and the boolean error.
0
nietodCommented:
What do you mean?  I need details.
0
nikisunAuthor Commented:
what details you want?
0
nietodCommented:
Is it a compile time error?  (the code doesn't compile.)  if so what error messages are reported.

Is it a run time error?  (The code compiles but runs incorrectly.)  If so, what behavior do you see?

I'm leaving in about 2 hours.  I'll be gone for 5 days.
0
abdijCommented:
Here is the code you need to do what your tutorial says:

// A wether station records the temperature every hour.
// Read the 24 temperatures recorded during one day-night period
// and output the low, high and average temperature for the period.

#include "iostream.h"
#define MAX_VAL 24
int main(int argc, char* argv[])
{
      int            nIndex;                              // a loop variant
      float      faTemperature[MAX_VAL]; // array to hold temperatures
      float      fHighestTemperature;      // highest temperature holder
      float      fLowestTemperature;            // lowest temperature holder
      float      fAvgTemperature;            // average temperature holder

      // gt all the 24 hour values first
      printf("Enter the temperature values of the day-night \n");

      for(nIndex = 0; nIndex < MAX_VAL; nIndex++)
      {
            cout << "Hour" << (nIndex+1) << "\t";
            cin >> faTemperature[nIndex];            
      } // end of for

      // initialize the variables
      fHighestTemperature = fLowestTemperature = faTemperature[0];
      fAvgTemperature = 0;

      // now calculate the avg highest and lowest temperatures
      for(nIndex = 0; nIndex < MAX_VAL; nIndex++)
      {
            // calc average
            fAvgTemperature = fAvgTemperature + faTemperature[nIndex];

            // find highest temperature
            if(fHighestTemperature < faTemperature[nIndex])
                  fHighestTemperature = faTemperature[nIndex];

            // find highest temperature
            if(fLowestTemperature > faTemperature[nIndex])
                  fLowestTemperature = faTemperature[nIndex];
      } // end of for loop

      // correst the avg
      fAvgTemperature = fAvgTemperature/MAX_VAL;

      // print the result
      cout << "\n\nThe Results are as below:" << endl << endl;
      cout << "The Highest temperature recorded is                  "<< fHighestTemperature << endl;
      cout << "The Lowest temperature recorded is                  "<< fLowestTemperature << endl;
      cout << "The Average of the temperatures recorded is            "<< fAvgTemperature << endl;

      // return success
      return 0;
} // end of main

I hope i have helped you out
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
abdij, did you read the question history?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.