Array

Pages: 1234
The task is to implement the function void CTempSensor::calculateMinMaxTemp()

.In the function I should search for the maximum temperature and the minimum temperature in the array and save it then in variable maxTemp and minTemp which is in the header.

I was not sure if Ihave to do this in the beginning :
float min = m_last24hrsTemp[0];
float max = m_last24hrsTemp[0];

I hope somebody can help?
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
ifndef CTEMPSENSOR_H_
#define CTEMPSENSOR_H_

#include <iostream>


using namespace std;



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();



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]


};


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


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();
    m_minTemp = minTemp;

    m_maxTemp = maxTemp;



}

// 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(){


	 float min = m_last24hrsTemp[0];
	 float max = m_last24hrsTemp[0];

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > max)
	         max = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < min)
	         min = m_last24hrsTemp[i];
	 }


	 }


The other functions were given. Only the constructor had to be implemented.
Last edited on
1) As I'm sure you can see for yourself, your code tags are messed up. Please can you fix them, to make your code readable?

2) Yes, you'll need to initialise those variables. Otherwise, what value will you be comparing against in the first iteration of your loop?

3) At present, your code doesn't actually do anything with those min and max values you've calculated - they just get thrown away when the method returns.
I was not sure if Ihave to do this in the beginning :
minTemp/maxTemp are provided as parameter for the constructor which seems not to be overly useful.

Currently nothing is done with those member variables (except print()). I would think that they are calculated in calculateMinMaxTemp() in place of min/max.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CTempSensor::calculateMinMaxTemp(){


	 float min = m_last24hrsTemp[0];
	 float max = m_last24hrsTemp[0];

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > max)
	         max = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < min)
	         min = m_last24hrsTemp[i];
	 }
            max = m_maxTemp;
            min = m_minTemp;


	 }


Is the code so better ?
I saved the max in m_max......
I saved the max in m_max......

Um... no you didn't. In fact, you overwrote the max (and min) that you just calculated.
my next try is so :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void CTempSensor::calculateMinMaxTemp(){


	 float m_minTemp = m_last24hrsTemp[0];
	 float m_maxTemp = m_last24hrsTemp[0];

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > max)
	         m_maxTemp = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < min)
	         m_minTemp = m_last24hrsTemp[i];
	 }


	 }


But eclipse shows an error ?

Can I erase that and do it so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void CTempSensor::calculateMinMaxTemp(){


	

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] >m_last24hrsTemp[ m_maxTemp])
	         m_maxTemp = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] <     m_last24hrsTemp[ m_minTemp]         )
	         m_minTemp = m_last24hrsTemp[i];
	 }


	 }

But eclipse shows an error ?

What error? How can we be effective in helping you, if you conceal important information from us?

In your second snippet, how does it make sense to use a temperature value as an array index?
I post my Header and 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

#ifndef CTEMPSENSOR_H_
#define CTEMPSENSOR_H_

#include <iostream>


using namespace std;



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();



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]


};






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
* 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>


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();
    m_minTemp = minTemp;

    m_maxTemp = maxTemp;



}

// 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(){


	 float m_minTemp = m_last24hrsTemp[0];
	 float m_maxTemp = m_last24hrsTemp[0];

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > max)
	         m_maxTemp = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < min)
	         m_minTemp = m_last24hrsTemp[i];
	 }


	 }






The Errors are:

Description Resource Path Location Type
Invalid overload of 'min' CTempSensor.cpp /CTemp line 90 Semantic Error
Invalid overload of 'max' CTempSensor.cpp /CTemp line 88 Semantic Error

Can you help me better now?
Last edited on
Lines 87/89 (cpp) have still max/min which are not defined anymore.
After you change max => m_maxTemp in line 87 and min => m_minTemp in line 89, you still have the problem mentioned a time or 2 in earlier posts. Now that you have determined which are your max and min temperatures, you need to return these values to the caller so that the caller can do something with them.

You can either return the two values in a struct, or you can pass 2 reference values in as arguments and populate them inside the function. It would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void doSomething(int& first, int& second)
{
   first = 8;
   second = 20;
}

int main()
{
   int x = 5;
   int y = 10;

   doSomething (x, y);
   // now x = 8, y = 20
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void CTempSensor::calculateMinMaxTemp(float m_minTemp,float m_maxTemp){


	 float m_minTemp = m_last24hrsTemp[0];
	 float m_maxTemp = m_last24hrsTemp[0];

	 int len = 24-1;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > m_maxTemp)
	         m_maxTemp = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < m_minTemp)
	         m_minTemp = m_last24hrsTemp[i];
	 }


	 }




OMG I tried to correct it , but it shows error again?

This task is making me crazy.

Has somebody an idea ?
If you put float in front of m_minTemp and m_maxTemp then you will be declaring NEW LOCAL VARIABLES that will hide the variables of the same name already declared in your class. Since it is presumably the latter that you want to update, then don't put 'float' in here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CTempSensor::calculateMinMaxTemp(){


         m_minTemp = m_last24hrsTemp[0];   // DON'T REDEFINE THE VARIABLES
         m_maxTemp = m_last24hrsTemp[0];   //   ALREADY DECLARED IN YOUR CLASS

         int len = 24-1;                                // ARE YOU SURE YOU DON'T MEAN 24?

         for(int i = 1; i < len ; i++)
         {
             if(m_last24hrsTemp[i] > m_maxTemp)
                 m_maxTemp = m_last24hrsTemp[i];
             if(m_last24hrsTemp[i] < m_minTemp)
                 m_minTemp = m_last24hrsTemp[i];
         }


}



In future, provide code that is
(a) complete;
(b) up to date;
(c) able to run. (We need main() and your header file is incomplete.)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void CTempSensor::calculateMinMaxTemp(float m_minTemp,float m_maxTemp){

	 int len = 24;

	 for(int i = 1; i < len ; i++)
	 {
	     if(m_last24hrsTemp[i] > m_maxTemp)
	         m_maxTemp = m_last24hrsTemp[i];
	     if(m_last24hrsTemp[i] < m_minTemp)
	         m_minTemp = m_last24hrsTemp[i];
	 }


	 }


But it shows still error?
Description Resource Path Location Type
make: *** [CTempSensor.o] Error 1 CTemp C/C++ Problem
redefinition of 'm_maxTemp' CTempSensor.cpp /CTemp C/C++ Problem
redefinition of 'm_minTemp' CTempSensor.cpp /CTemp C/C++ Problem
previous definition is here CTempSensor.cpp /CTemp C/C++ Problem
edit: this is a pretty late post

... You realize that m_minTemp is also defined in the class aswell? And it is defined in the function parameters? Why did you add that to the parameters? Why are you not showing what error you have?, it also cant possibly be the same one.

Anyways you have to remove the re-definitions of the min and max value in the function, and len = 24-1 is skipping the last entry.
edit: didn't noticed you already fixed that.

Maybe next time try using std::array and iterators.

Iterators are verbose but they help with having a better understanding of what range are you going through and its not as verbose, especially now since you can use the "auto" keyword, in C arrays you have to deal with that fact that if you want to skip a number at the beginning you have to +1 "i" and to skip a number at the end you have to -1 the max and remember not to use the wrong less than operator, and this could very easily lead to silly "off by 1" errors that anyone could make, and don't forget that if you access beyond the C array the runtime might not segfault (likely with optimized code) and you will have weird results (stack overflow / memory corruption / undefined behavior land).

edit: you removed the definitions to m_minTemp in your last post, but you should keep that without redefining the float. Also funny thing is that I believe that if you redefine member variables with variables inside the function, you will not get an error, but you will if you redefine variables in the function parameters, you will.
Last edited on
@Lex,
Why don't you simply insert the segment of code that I gave you?

As @poteto has just pointed out, you have instead put a redefinition of variables into the function's parameter list instead of the body of the function. It is just as bad. You are hiding (by redeclaring) variables that are members of your class. Look at the word "redefinition" in the error message.

Please supply your WHOLE code - including main().
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

 void CTempSensor::calculateMinMaxTemp(float m_minTemp,float m_maxTemp){

	 
	

	 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];
	 }


	 }


OMG the code is working now ?

I only erased the definition of len ?

I think the code is right now ?
Eclipse is also not showing any errors now
@Lex33,
Your code is worse than it was before. The fact that it might compile doesn't make it right. It has nothing whatsoever to do with len. In your header file there are class variables called m_maxTemp and m_minTemp - you do not need to declare their type as float ever again.

You have completely ignored all advice given to you on this thread, so there is no point offering any more: you do not read it.
Sorry now I understand it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CTempSensor::calculateMinMaxTemp(){

	 float m_minTemp = m_last24hrsTemp[0];
	 	 float 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];
	 }


	 }



But now it says member decleration not found ?
Why ?
I dont understand it .
Last edited on
Lex33 wrote:
Sorry now I understand it


Clearly you DON'T understand it.

This is the last version of your class definition that you supplied:
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
ifndef CTEMPSENSOR_H_
#define CTEMPSENSOR_H_

#include <iostream>


using namespace std;



class CTempSensor
{

private:
	float m_last24hrsTemp[24]; // array of last 24 measurements [vorgegeben]
	float m_minTemp;           // minimum Temp of last 24hrs [vorgegeben]   <<< *** LOOK ***
	float m_maxTemp;           // maximum Temp of last 24hrs [vorgegeben]   <<< *** LOOK ***
	void calculateMinMaxTemp();

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]

};
#endif             // <<< ADD THIS LINE 


Do you really not see those two lines declaring variables m_minTemp and m_maxTemp within your class CTempSensor?

If you try to REDECLARE these variables in function
1
2
3
4
void CTempSensor::calculateMinMaxTemp(){

float m_minTemp = m_last24hrsTemp[0];    // <<< JUST REMOVE THE WORD float
float m_maxTemp = m_last24hrsTemp[0];    // <<< JUST REMOVE THE WORD float 


then you will HIDE (i.e. completely ignore) the member variables of the class with the same name. Don't remove the whole line, just read the comment: // <<< JUST REMOVE THE WORD float


Unless you upload an UP-TO-DATE and COMPLETE (including main() ) program it will be nearly impossible to track your multiple errors.
Last edited on
Pages: 1234