Link to home
Start Free TrialLog in
Avatar of nikisun
nikisun

asked on

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?
Avatar of nikisun
nikisun

ASKER

Edited text of question.
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.
Avatar of nikisun

ASKER

how about the switch case? can the for loop combine together with the switch case?
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.  :- )

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?
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.
 
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.
>> 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.  
Avatar of nikisun

ASKER

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.
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.
>> 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.
Avatar of nikisun

ASKER

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




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.
Avatar of nikisun

ASKER

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;
}
oooooooops!!! sorry!!! ranges on cases are Pascal, aren't they? Sorry about that :-)
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.)
Avatar of nikisun

ASKER

then, could you please me how should i do?
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.
well i hope the problem is solved now.
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.
Avatar of ozo
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.
Avatar of nikisun

ASKER

then, does my program correct or not?
>> 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?
Avatar of nikisun

ASKER

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.
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;
}
Try to make it do the high and the average and post what you get done.
Avatar of nikisun

ASKER

does the high and average step same as the low step that you wrote?
What do you meant by "step"?

You need to add code to the design I showed you to make the high and average work.  
Avatar of nikisun

ASKER

it is the same method as the low program that you did?
High is similar to low, you need to think about it.  Average is similar to what you already had for average.
Avatar of nikisun

ASKER

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;
}
}
>> 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.
Avatar of nikisun

ASKER

i program the code you gave but still can't manage to run. it has error on it. it can't run.
What is the error?
Avatar of nikisun

ASKER

the calculation and the boolean error.
What do you mean?  I need details.
Avatar of nikisun

ASKER

what details you want?
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.
ASKER CERTIFIED SOLUTION
Avatar of abdij
abdij

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
abdij, did you read the question history?