tags:

views:

110

answers:

4
 struct WeatherStation  {

string Name;
double Temperature;

 };

 void Initialize(WeatherStation[]);
 void HL(WeatherStation List[]);

int main()

 {
 string Command;
  WeatherStation Stations[5];

  //some commands

 }
 void Initialize(WeatherStation StationList[])

 {

  StationList[0].Name = "A";
  StationList[0].Temperature = 0.0;

  StationList[1].Name = "B";
  StationList[1].Temperature = 0.0;

  StationList[2].Name = "C";
  StationList[2].Temperature = 0.0;

  StationList[3].Name = "D";
  StationList[3].Temperature = 0.0;

  StationList[4].Name = "E";
  StationList[4].Temperature = 0.0;

  }

 void HL(WeatherStation List[])
  {
   int K;
   int Low = List[0];            
   int High = List[0];       

   for(K = 0 ; K < 5 ; K++)      
    if(List[K] < Low)
    Low = List[K];

   for(K=0 ;  K < 5 ; K++)    
    if(List[K] > High)
    High = List[K];

   cout << "Lowest Temperature:  " <<Low << endl;
   cout << "Highest Temperature: "<< High << endl;
   } 

The last part is tripping me up.

chief.cpp: In function ‘void HL(WeatherStation*)’:
chief.cpp:124: error: cannot convert ‘WeatherStation’ to ‘int’ in initialization
chief.cpp:125: error: cannot convert ‘WeatherStation’ to ‘int’ in initialization
chief.cpp:128: error: no match for ‘operator<’ in ‘*(List + ((unsigned int)(((unsigned int)K) * 12u))) < Low’
chief.cpp:129: error: cannot convert ‘WeatherStation’ to ‘int’ in assignment
chief.cpp:132: error: no match for ‘operator>’ in ‘*(List + ((unsigned int)(((unsigned int)K) * 12u))) > High’
chief.cpp:133: error: cannot convert ‘WeatherStation’ to ‘int’ in assignment

A: 

you have not initialized any array inside initilize() function.

what you did there was assuming StatusList was created before being passed in to the function.

and to find min and max value, you could have done it in 1 for loop which contains 2 if tests instead of 2 loop loops with 2 if tests..

stdnoit
+1  A: 

It cannot convert WeatherStation to int because WeatherStation is a structure. If you want to get a member of a structure you should write, for instance, List[0].Temperature.

Kirill V. Lyadvinsky
And temperature is a double, so double Low = blah..
Moron
Must use an *epsilon* factor instead of comparing for equality with doubles. Doubles are rarely exactly equal.
Thomas Matthews
May be `WeatherStation::Temperature` should be `int` from the other side.
Kirill V. Lyadvinsky
@THomas: For max and min you are doing equality checks?
Moron
@Kir: Doubt if Temperature should be int. Double/float for temperature makes more sense. 13.4 etc etc. (I gave it a +1, btw, don't know why someone gave a -1).
Moron
@Moron, I suppose that temperature values is not in wide range usually, so it is makes sense to keep it as `int`. `13.4` will be `13400` etc etc. The idea is that integer arithmetic is usually faster then floating point.
Kirill V. Lyadvinsky
@Kir: Perhaps. Depends on the application and how the temperature values are used. Besides, with an FPU available on almost all CPUs these days integer arithmetic might actually become the slower option! Also, I was talking only in terms of the 'type' of temperature which, given that it is likely to be homework, makes more sense (to me, at least) to make a double/float.
Moron
@Moron: For min and max, either *less than* or *greater than* will suffice. The standard operators can be used with `double` or `float` types. Equality is troublesome because not all floating point numbers can be represented internally exactly.
Thomas Matthews
@Tho: Yes, and that is 'off-topic' for the question.
Moron
A: 

You should use C++ containers instead of arrays

If you don't like std::vector, you can use std::array

void Initialize(std::vector<WeatherStation>&);
 void HL(const std::vector<WeatherStation>&);

int main()

 {
 string Command;
  std::vector<WeatherStation> Stations;

  //some commands

 }
 void Initialize(std::vector<WeatherStation>& StationsList)

 {

  StationList.push_back({"A", 0.0});
  StationList.push_back({"B", 0.0});
  StationList.push_back({"C", 0.0});
  StationList.push_back({"D", 0.0});
  StationList.push_back({"E", 0.0});

  }

 void HL(const std::vector<WeatherStation>& List)
  {
   cout << "Lowest Temperature:  " << std::min_element(List.begin(), List.end())->Temperature << endl;
   cout << "Highest Temperature: "<< std::max_element(List.begin(), List.end())->Temperature << endl;
   } 

Also note that it's not a very good idea to name your variables the same way as you name your types (I mean capitalized)

Tomaka17
A: 

The problem you're having (or at least the main problem) is right here:

if(List[K] < Low)
    Low = List[K];

if(List[K] > High)
    High = List[K];

List is defined as an array of WeatherStation structures. You want something like:

if (list[K].Temperature < Low) 
    Low = List[K].Temperature;

Edit: You might also want to look into using std::min_element and std::max_element instead.

Jerry Coffin