1 2 3 4 5 6 7 8 9 10 11 12
|
double CTempSensor::calculateMeanTemp(){
double summe = 0;
for(int i = 0; i<24 ; i++){
summe += m_last24hrsTemp[i];
}
summe = (summe)/(24);
return summe;
}
|
I think , that s it?
The next task is a little difficult:
I habe problem with task f)
d) Add a private method double calculateMeanTemp () that returns the average temperature of the 24 metrics from m_last24hrsTemp [24].
e) Declare a tSensorType enumeration that defines the following constants to later determine the nature of the sensor (for example, if the sensor measures water, oil, or air temperature):
GENERAL, WATER, OIL, AIR. Add an attribute m_sensorType of the type
tSensorType in class CTempSensor.
f) Implement a private method void generateSensorType (), which randomly assigns the value for the sensor type. Call this method in the constructor of the CTempSensor class
to assign a sensor type to each sensor.
Note 1: You can find the CTempSensor :: retrieveLastMeasurements () method
Example of how random numbers are generated.
Note 2: You need to think about how to turn numbers into tSensorType constants. Note 3: If you can not implement the method, initialize the m_sensorType attribute in the constructor with any allowed value.
My code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#ifndef CTEMPSENSOR_H_
#define CTEMPSENSOR_H_
#include <iostream>
using namespace std;
enum tSensorType{GENERAL ,WATER , OIL , AIR};
class CTempSensor
{
private:
float m_last24hrsTemp[24]; // array of last 24 measurements [vorgegeben]
float m_minTemp; // minimum Temp of last 24hrs [vorgegeben]
float m_maxTemp; // maximum Temp of last 24hrs [vorgegeben]
void calculateMinMaxTemp();
tSensorType m_sensorType;
public:
CTempSensor(float minTemp = 0 , float maxTemp = 0);
virtual ~CTempSensor();
void retrieveLastMeasurements(); // erzeugt 24 Zufallswerte [vorgegeben]
void print(); // gibt die Messdaten aus [vorgegeben]
float checkTempRange(float, float); // [vorgegeben]
double calculateMeanTemp();
};
#endif /* CTEMPSENSOR_H_ */
|
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
/*
* CTempSensor.cpp
*
* Created on: 23.06.2017
* Author: buergy
*/
#include "CTempSensor.h"
#include <iostream> // Header fuer die Standard-IO-Objekte (z.B. cout, cin)
#include <cstdlib> // fuer random values
#include <iomanip> // fuer setw()
#include <string>
#include<cmath>
using namespace std; // Erspart den scope vor Objekte der
// C++-Standard-Bibliothek zu schreiben
// z.B. statt "std::cout" kann man "cout" schreiben
CTempSensor::CTempSensor(float minTemp,float maxTemp)
{
CTempSensor::retrieveLastMeasurements();
CTempSensor::calculateMinMaxTemp();
}
// generiert 24 Zufallswerte zw. 5 und 95 Grad [vorgegeben]
void CTempSensor::retrieveLastMeasurements()
{
for (int i=0; i<24;i++) {
m_last24hrsTemp[i] = (rand() % 900 + 50) / (float)10;
}
}
// gibt min/max und die 24 Messwerte aus [vorgegeben]
void CTempSensor::print()
{
cout << "min: " << m_minTemp << " | max: " << m_maxTemp << " | Letzte 24 Messwerte: ";
for (int i=0; i<24;i++) {
cout << setw(4) << m_last24hrsTemp[i] << " | " ;
}
cout << endl;
}
// gibt den Prozentsatz der Werte in der Range zurueck [gegeben fuer Aufgane 3c]
float CTempSensor::checkTempRange(float low, float high)
{
int inRange = 0;
int outOfRange = 0;
for (int i=0; i<24;i++) {
if (m_last24hrsTemp[i] < low || m_last24hrsTemp[i] > high) {
outOfRange++;
}
else {
inRange++;
}
}
return 100*inRange/24.;
}
CTempSensor::~CTempSensor()
{
// TODO Auto-generated destructor stub
}
void CTempSensor::calculateMinMaxTemp(){
m_minTemp = m_last24hrsTemp[0];
m_maxTemp = m_last24hrsTemp[0];
for(int i = 1; i < 24 ; i++)
{
if(m_last24hrsTemp[i] > m_maxTemp)
m_maxTemp = m_last24hrsTemp[i];
if(m_last24hrsTemp[i] < m_minTemp)
m_minTemp = m_last24hrsTemp[i];
}
}
double CTempSensor::calculateMeanTemp(){
double summe = 0;
for(int i = 0; i<24 ; i++){
summe += m_last24hrsTemp[i];
}
summe = (summe)/(24);
return summe;
}
|
void CTempSensor::generateSensorType(){
m_sensorType = (rand() % 900 + 50) / (float)10;
So?