Array

Pages: 1234

In the class CSensorGrid, implement the method print (tSensorType), which outputs the sensor type as well as the minimum, maximum and average temperatures for (3) all sensors of a certain sensor type of the sensor network. The parameter value determines from which sensor type the objects are displayed.
If you need implementation access to the CTempSensor :: m_SensorType attribute, implement the get method.
The output format should look like in the example below; Use a suitable method or function of the CTempSensor class for the display.

https://www.pic-upload.de/view-35223573/Bildschirmfoto2018-04-25um12.02.26.png.html

I tried the task but to much errors?
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
/*
 * CSensorGrid.cpp
 *
 *  Created on: 23.04.2018
 *      Author: macbook
 */

#include "CSensorGrid.h"

CSensorGrid::CSensorGrid(int maxSensorIdx) {

	if(m_maxSensorIdx >= 5 ){

		m_maxSensorIdx = maxSensorIdx;
	}
	else{
		m_maxSensorIdx = 10;

	}
	m_pSensor = new CTempSensor [m_maxSensorIdx];

}

CSensorGrid::~CSensorGrid() {
	delete[] m_pSensor;
}

void CSensorGrid::operator += (const CTempSensor&rop){

	if(m_nextSensorIdx >= m_maxSensorIdx){

		CTempSensor* sensor_temp= new CTempSensor[m_maxSensorIdx+5];

		for(int i = 0; i<m_maxSensorIdx; i++){

			sensor_temp[i]	= m_pSensor[i];
		}
		delete[] m_pSensor;
		m_pSensor=   sensor_temp;


	}
	m_pSensor[m_nextSensorIdx++] = rop;

}
void CSensorGrid::print(tSensorType type){

	if(type == GENERAL){

		cout << "Typ:" << type << "min:" << CTempSensor::m_minTemp << ""<< "max:" << CTempSensor::m_maxTemp << ""<< "Durschnit::" << CTempSensor::calculateMeanTemp() << endl;
	}


}




Description Resource Path Location Type
'm_maxTemp' is a private member of 'CTempSensor' CSensorGrid.cpp /CTemp line 50 C/C++ Problem
'm_minTemp' is a private member of 'CTempSensor' CSensorGrid.cpp /CTemp line 50 C/C++ Problem
call to non-static member function without an object argument CSensorGrid.cpp /CTemp line 50 C/C++ Problem
Invalid arguments '
Candidates are:
double calculateMeanTemp()
' CSensorGrid.cpp /CTemp line 50 Semantic Error
Invalid overload of 'endl' CSensorGrid.cpp /CTemp line 50 Semantic Error
invalid use of non-static data member 'm_maxTemp' CSensorGrid.cpp /CTemp line 50 C/C++ Problem
invalid use of non-static data member 'm_minTemp' CSensorGrid.cpp /CTemp line 50 C/C++ Problem
make: *** [CSensorGrid.o] Error 1 CTemp C/C++ Problem
in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions] CSensorGrid.h /CTemp line 20 C/C++ Problem
declared private here CTempSensor.h /CTemp line 23 C/C++ Problem
declared private here CTempSensor.h /CTemp line 24 C/C++ Problem

Please help
Do you understand the difference between the public interface of a class, and the private implementation of a class?

Do you understand the difference between a class and an object?

If not, then you probably need to go back to your textbook, or find a good tutorial on the subject of classes. You'll need to understand these things if you want to use classes.

You might find the tutorials here helpful:

http://www.cplusplus.com/doc/tutorial/
Yeah I know the difference.

You cant access to private implementation of a class?

But how should I do it else?

Yeah I know the difference.

Line 50 suggests you don't, for either of my questions, because you're both:
- trying to directly access the private implementation of your object
- trying to call a method on the class, when you should be calling it on an object

You cant access to private implementation of a class?

Is that actually a question? Do you not know the answer?

But how should I do it else?

Add methods to your public interface to allow access to the things you need.

This, incidentally, is covered in the tutorial I gave you a link to, and is almost certainly covered by your textbook.
Should I create than get Methods in the CtempSensor cpp?
float CTempSensor::getminTemp(){
return m_minTemp;

};
floatCTempSensor::getmaxTemp(){

return m_maxTemp;


};

tSensorType CTempSensor::getSensorType(){

return m_sensorType;

}

is it ok so?
To repeat the question you've been asked before: what does your testing tell you?

I mean, you've written the code, so how much more work it be to actually run it and find out for yourself?
My compiler shows only errors at the moment .
I cant test it with eclipse

I tried to correct it but it shows error OMG?
CTempSensor 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * 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 = static_cast<tSensorType>(rand() % 4);
 }

 string CTempSensor::getSensorTypeAsString(){
	   string result;
	   switch(m_sensorType){
	   case WATER:
		   result = "WATER";
		   break;

	   case GENERAL:
	   		   result = "GENERAL";
	   		   break;

	   case OIL:
	   		   result = "OIL";
	   		   break;
	   case AIR:
	   		   result = "AIR";
	   		   break;


	   }

	   return result;




 }

 ostream& operator << (ostream& lop, CTempSensor& rop){

	 lop << "Typ:"  << rop.m_sensorType << "" << "min:" << rop.m_minTemp  << "" << "max:"<< rop.m_maxTemp<<""<<"Durchschnitt:"<<rop.calculateMeanTemp()<< endl;

	 return lop;


}

 float CTempSensor::getminTemp(){
 return m_minTemp;

 };
  float CTempSensor::getmaxTemp(){

 return m_maxTemp;


 };

 tSensorType CTempSensor::getSensorType(){

 return m_sensorType;

 }



CSensor Grid 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


/*
 * CSensorGrid.cpp
 *
 *  Created on: 23.04.2018
 *      Author: macbook
 */

#include "CSensorGrid.h"

CSensorGrid::CSensorGrid(int maxSensorIdx) {

	if(m_maxSensorIdx >= 5 ){

		m_maxSensorIdx = maxSensorIdx;
	}
	else{
		m_maxSensorIdx = 10;

	}
	m_pSensor = new CTempSensor [m_maxSensorIdx];

}

CSensorGrid::~CSensorGrid() {
	delete[] m_pSensor;
}

void CSensorGrid::operator += (const CTempSensor&rop){

	if(m_nextSensorIdx >= m_maxSensorIdx){

		CTempSensor* sensor_temp= new CTempSensor[m_maxSensorIdx+5];

		for(int i = 0; i<m_maxSensorIdx; i++){

			sensor_temp[i]	= m_pSensor[i];
		}
		delete[] m_pSensor;
		m_pSensor=   sensor_temp;


	}
	m_pSensor[m_nextSensorIdx++] = rop;

}
void CSensorGrid::print(tSensorType type){

	if(type == GENERAL){

		cout << "Typ:" << type << "min:" << CTempSensor::getminTemp() << ""<< "max:" << CTempSensor::getmaxTemp() << ""<< "Durschnit::" << CTempSensor::calculateMeanTemp() << endl;
	}


}



Description Resource Path Location Type
call to non-static member function without an object argument CSensorGrid.cpp /CTemp line 50 C/C++ Problem
Invalid arguments '
Candidates are:
double calculateMeanTemp()
' CSensorGrid.cpp /CTemp line 50 Semantic Error
Invalid arguments '
Candidates are:
float getmaxTemp()
' CSensorGrid.cpp /CTemp line 50 Semantic Error
Invalid arguments '
Candidates are:
float getminTemp()
' CSensorGrid.cpp /CTemp line 50 Semantic Error
Invalid overload of 'endl' CSensorGrid.cpp /CTemp line 50 Semantic Error
make: *** [CSensorGrid.o] Error 1 CTemp C/C++ Problem
in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions] CSensorGrid.h /CTemp line 20 C/C++ Problem


Why is it still showing errors?
call to non-static member function without an object argument CSensorGrid.cpp /CTemp line 50 C/C++ Problem

You still have not resolved your confusion between classes and objects. I've already told you what the problem is in a previous post.
Last edited on
cout << "Typ:" << type << "min:" << m_minTemp << ""<< "max:" << m_maxTemp<<...

Should I doit so?
No.

I don't say this to be rude, or insulting, but to be helpful:

It's very obvious that you don't understand how to work with classes and objects in C++. This isn't one small mistake you're making; you don't seem to understand how to use them at all.

The best thing you can do is go back to your textbook, or to a suitable tutorial, and learn the fundamentals about using classes and objects. I don't just mean skim it, hack together some code, and then post it here to ask whether it's right. I mean read properly, understand properly, and learn properly.

Now, if there's anything in your book you don't understand, then run it by us, and we'll try and help.

But just randomly writing bits of code without knowing what you're doing, is not helping you.
Last edited on
I have only one subject where we are starting to learn programming .
Normally I understand c++ a little .

What should I otherwise do?
Somebody there ?
Can somebody help me?
Can someone help me so that I can complete the task?
Last edited on


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



/*
 * CSensorGrid.cpp
 *
 *  Created on: 23.04.2018
 *      Author: macbook
 */

#include "CSensorGrid.h"

CSensorGrid::CSensorGrid(int maxSensorIdx) {

	if(m_maxSensorIdx >= 5 ){

		m_maxSensorIdx = maxSensorIdx;
	}
	else{
		m_maxSensorIdx = 10;

	}
	m_pSensor = new CTempSensor [m_maxSensorIdx];

}

CSensorGrid::~CSensorGrid() {
	delete[] m_pSensor;
}

void CSensorGrid::operator += (const CTempSensor&rop){

	if(m_nextSensorIdx >= m_maxSensorIdx){

		CTempSensor* sensor_temp= new CTempSensor[m_maxSensorIdx+5];

		for(int i = 0; i<m_maxSensorIdx; i++){

			sensor_temp[i]	= m_pSensor[i];
		}
		delete[] m_pSensor;
		m_pSensor=   sensor_temp;


	}
	m_pSensor[m_nextSensorIdx++] = rop;

}
void CSensorGrid::print(tSensorType type){


	for (int i = 0; i < m_maxSensorIdx; i++) {
		if(m_pSensor[i].getSensorType() == type) {
			cout << "Typ:" << m_pSensor[i].getSensorTypeAsString() << "min:" << m_pSensor[i].getminTemp() << ""<< "max:" << m_pSensor[i].getmaxTemp() << ""<< "Durschnit::" << m_pSensor[i].calculateMeanTemp() << endl;
		}
	}




}










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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*

 * 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 = static_cast<tSensorType>(rand() % 4);
 }

 string CTempSensor::getSensorTypeAsString(){
	   string result;
	   switch(m_sensorType){
	   case WATER:
		   result = "WATER";
		   break;

	   case GENERAL:
	   		   result = "GENERAL";
	   		   break;

	   case OIL:
	   		   result = "OIL";
	   		   break;
	   case AIR:
	   		   result = "AIR";
	   		   break;


	   }

	   return result;




 }

 ostream& operator << (ostream& lop, CTempSensor& rop){

	 lop << "Typ:"  << rop.m_sensorType << "" << "min:" << rop.m_minTemp  << "" << "max:"<< rop.m_maxTemp<<""<<"Durchschnitt:"<<rop.calculateMeanTemp()<< endl;

	 return lop;


}

 float CTempSensor::getminTemp(){
 return m_minTemp;

 };
  float CTempSensor::getmaxTemp(){

 return m_maxTemp;


 };

 tSensorType CTempSensor::getSensorType(){

 return m_sensorType;

 }



I have solved it by myself now .

Is it ok so?



Has someone some tipps for this task?

Now calculate - in another method void calculateMedianTemp () - the median value of all measured values ​​of all sensors in the grid and enter them in the
Console off. Test the method by calling it in main.cpp and see the result
Show.
Start by:
first write the relevant values ​​into a sufficiently large array; Sortthen sort the values ​​in the array;
Berechnen then calculate the median (statistical mean);
(The calculation formula is as shown on the right, since there are 24 values ​​per sensor, you can assume an even number of values ​​and only have to implement this case)

https://www.pic-upload.de/view-35281601/Bildschirmfoto2018-05-06um14.21.05.png.html
Last edited on
?????
I don't see any obvious bugs or syntax errors. Some thoughts and tips about what you've written:

1) There are a lot of magic numbers in here. I see "5", "10", "24". It's much better to use named constants, as (a) it makes it absolutely clear what the numbers mean, and (b) it makes it easier to change the values if necessary, because all you need to do is change it in one place.

2) I'm confused about the logic of your CSensorGrid constructor. If the max index is set to 4, you allocate space for 10 sensors, but if it's set to 5, you only allocate space for 5. Why is that? What's special about that 5 - 9 range, that you allocate less space than if the number was lower?

3) The word "grid" implies a 2-dimensional layout, but you only use a 1-dimensional array of sensors. Are you sure that's right?

4) In the CTempSensor constructor, you're passing in 2 arguments that aren't used. Surely your compiler is warning you about that already?

5) Your indentation is messy and inconsistent. Adopting a clear, consistent style helps you (and us) read your code more easily, and find problems in it more easily

6) You don't need the semi-colon in lines 154 and 160 of CTempSensor.cpp

As for the new task, you've been given some reasonably detailed steps to follow. Which bit are you having trouble with?
I dont really understand in the new task which kind of Array should I create ?
If you read the problem description, it's obvious:

Now calculate - in another method void calculateMedianTemp () - the median value of all measured values ​​of all sensors in the grid
[...]
first write the relevant values ​​into a sufficiently large array

Analysis is an important part of writing software - you need to look carefully at the details of the problem you're trying to solve.
m_pSensor[........];

What should I put in the brackets?
Pages: 1234