Programmieraufgabe: Functions, Templates, etc.
-
Hallo zusammen,
ich habe folgende Programmieraufgabe:
Write a program that
• defines a structure Point holding a xcoordinate
and a y-coordinate of a point
• reads a number of point values from keyboard
• stores those point elements in a vector called
aPointCollection of type vector<Point>
• defines a function calcCenterOfGraity(…)
which calculates the centre of gravity
(arithmetic average of x-coordinate and ycoordinate)
of all points stored in the vector
and extend it with:
o functions getMinimumValue(bool x) and getMaximumValue(bool x) that return
the minimum/maximum of all x-values (y-values) if parameter x is true (false)
o a function removeNegative(…) that removes all points from the original vector
that have any negative coordinate values (note: use call by reference)
o Test all functions with outputs to the screens;Leider stehe ich nun voll an und habe keine Ahnung mehr, was ich noch ausprobieren soll.
Wäre super, wenn mir irgendjemand helfen könnte. 
[cpp]//library includes #include <iostream> #include <vector> using namespace std; // Template for the function to calculate the Minimum Value template <typename T> T MinimumValue(T a, T b) { return (a<b) ? a : b; } // Template for the function to calculate the Maximum Value template <typename T> T MaximumValue(T a, T b) { return (a>b) ? a : b; } struct Point { float xCoordinate; float yCoordinate; }; // Function to calculate the Center of Gravity float calcCenterOfGravity(vector<Point> aPointCollection) { float CenterOfGravity; vector<Point>::iterator iter; // Definition of the iterator for vector Point for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) // Iterate through all elements of vector Point { CenterOfGravity = iter->xCoordinate/iter->yCoordinate; // Calculate the Center of Gravity } return CenterOfGravity; } // Function to find minimum element float getMinimumValue(vector<Point> aPointCollection, char xy) { bool check = (xy == 'x'); // Check if the bool Variable is true or false float Min; if (check) { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Min = MinimumValue(iter->xCoordinate, iter->xCoordinate); // If check is true calculate the min of x } } else { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Min = MinimumValue(iter->yCoordinate, iter->yCoordinate); // If check is false calculate the min of y } return Min; } } // Function to find maximum element float getMaximumValue(vector<Point> aPointCollection, char xy) { bool check = (xy == 'x'); // Check if the bool Variable is true or false float Max; if (check) { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Max = MaximumValue(iter->xCoordinate, iter->xCoordinate); // If check is true calculate the min of x } } else { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Max = MaximumValue(iter->yCoordinate, iter->yCoordinate); // If check is false calculate the min of y } return Max; } } // Function to remove negative elements void removeNegative(vector<Point> aPointCollection) { vector<Point>:: iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { if (&iter < 0) // Check if element is negative { aPointCollection.erase(iter); // Remove the negative elements } else { cout << &iter << endl; // Print of the positive elements } } } // main function int main() { Point aPoint; vector<Point> aPointCollection; // Definition of the Vector aPointCollection with type vector<Point> for (int count = 0; count < 2; count++) { // Type in the x coordinate for all 3 variables cout << "Please enter the floating x coordinate for Variable "<< count + 1 << endl; cin >> aPoint.xCoordinate; // Type in the y coordinate for all 3 variables cout << "Please enter the floating y coordinate for Variable "<< count + 1 << endl; cin >> aPoint.yCoordinate; aPointCollection.push_back(aPoint); // Print the result on the screen cout << endl << "The center of gravity of x and y is: "; // Call of the calCenterOfGravity function cout << calcCenterOfGravity(aPointCollection) << endl << endl; } char xy; // Definition of the Bool Variable cout << "Please key in x or y for getting the minimum values: "; cin >> xy; // Print and call of the getMinimumValue function cout << "The minimum value of " << xy << " is: " << getMinimumValue(aPointCollection, xy) << endl; // Print and call of the getMaximumValue function cout << "The maximum value of " << xy << " is: " << getMaximumValue(aPointCollection, xy) << endl; // Print and call of the getremoveNegative function cout << "After removing the negative values, the vector looks like this: "; removeNegative(aPointCollection); cin >> xy; getchar(); return EXIT_SUCCESS; }
-
Ähhh LOL sind x und y Fließkommazahlen? Pixel werden aber mit ganz normalen Integern behandelt (oder sollten!).
-
Hacker schrieb:
Ähhh LOL sind x und y Fließkommazahlen? Pixel werden aber mit ganz normalen Integern behandelt (oder sollten!).
Ja, die x und y Zahlen sind Kommazahlen. Dürfte ja eigentlich auch Wurst sein, wenn ich sonst alles richtig definiere, oder etwa nicht??

-
Was ist hier eigentlich die Frage?

-
MyMaus schrieb:
Hacker schrieb:
Ähhh LOL sind x und y Fließkommazahlen? Pixel werden aber mit ganz normalen Integern behandelt (oder sollten!).
Ja, die x und y Zahlen sind Kommazahlen. Dürfte ja eigentlich auch Wurst sein, wenn ich sonst alles richtig definiere, oder etwa nicht??

Dieser selbsternannte Hacker hat keine Ahnung. Koordinaten hält man fast nie Ganzzahl (wenn dann als unsigned int, aber das auch nur selten). Für Berechnungen wie bei dir sind floats goldrichtig.
-
Ich danke dir, aber leider hilft mir das bei meinem Problem nicht weiter
Das Center of Gravity funktioniert einwandfrei, aber bei der Min und Max Berechnung kommt immer eine komische Fehlermeldung beim Durchführen und ich habe keine Ahnung, woran das liegen könnte. Hab wirklich alles probiert!!!
Und beim rausnehmen der negativen Werte glaube ich das mit dem call by reference nicht ganz stimmt. Bin wirklich für jeden Hinweis dankbar!
-
Das hilft doch schon was, jetzt weiss ich, wo ich den Fehler suchen soll. Dein Code, schön formatiert:
// Function to find minimum element float getMinimumValue(vector<Point> aPointCollection, char xy) { bool check = (xy == 'x'); // Check if the bool Variable is true or false float Min; if (check) { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Min = MinimumValue(iter->xCoordinate, iter->xCoordinate); // If check is true calculate the min of x } } else { vector<Point>::iterator iter; for (iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) { Min = MinimumValue(iter->yCoordinate, iter->yCoordinate); // If check is false calculate the min of y } return Min; } }Es fällt auf:
- Das return wird nur im zweiten Fall ausgeführt.
- Zeile 10 sollte wohl Min = MinimumValue(Min, iter->yCoordinate); heissen (und dann Zeile 5 float Min=-1000;), analog bei Zeile 15.
- Den Vektor als Wert zu übergeben führt dazu, dass dieser jedesmal neu kopiert wird, nimm besser vector<Point> const& aPointCollection als Parameter und dann Zeile 8 und 15 als vector<Point>::const_iterator iter;
Das behebt einmal die schlimmsten Fehler.Ich wundere mich aber, dass dein Compiler keine Warnungen ausgegeben hat, denn so etwas sollte er doch sehen. Und was war das für eine Fehlermeldung beim Durchführen, könntest du die auch posten?
-
Wenn du nächstes mal eine Fehlermeldung bekommst, dann sei bitte so nett und poste sie.
Ach übrigens: Nice copy & paste work!
-
Danke erstmal vielmals für die Antworten und auch das Lob zur Formatierung :)!!
Das Problem ist eben, dass er wenn ich x eingebe um die minimas und maximas für x auszugeben, dann meldet er mir für die Werte immer -1.#IND und für meinen neuen Vektor kommt: 003EF654003EF654 nach Ausführung des Programms. Wenn ich aber y eingeb, dann kommt für min nun die -1000.
Infos vom Compiler sind nur:
'IOM_project.exe': Loaded 'C:\Users\Myriam\Documents\Visual Studio 2010\Projects\IOM_project\Debug\IOM_project.exe', Symbols loaded.
'IOM_project.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'IOM_project.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'IOM_project.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'IOM_project.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x13e8) has exited with code -1073741510 (0xc000013a).
The program '[5764] IOM_project.exe: Native' has exited with code -1073741510 (0xc000013a).Aber damit fang ich leider nicht wirklich was an!
Für jede weiter Hilfe natürlich sehr dankbar!
-
Erste Anlaufstelle bei allen Problemen:
Google: Cannot find or open the PDB file
-
Danke für das mit dem PDB-File. Die Fehlermeldung kommt jetzt auch nicht mehr. Aber leider bekomm ich immer noch nicht die richtigen Werte für meine Funktionen?? Noch irgendwelche Ideen??
-
float getMinimumValue(const vector<Point>& aPointCollection, char xy) { float Min; if (xy == 'x') for (std::vector<Point>::const_iterator iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) Min = MinimumValue(iter->xCoordinate, iter->xCoordinate); // If check is true calculate the min of x else for (std::vector<Point>::const_iterator iter = aPointCollection.begin(); iter != aPointCollection.end(); iter++) Min = MinimumValue(iter->yCoordinate, iter->yCoordinate); // If check is false calculate the min of y return Min; }Jetzt kommt bei mir statt nan
60eine Zahl...Edit: schalt mal -fpermissive an, dann dürfte das garnicht kompilieren
Edit²: ..., was du hast
-
@ Hacker: ich danke dir wirklich sehr für deine Hilfe, leider bekomme ich es irgendwie nicht hin. Werde es jetzt wohl so abgeben.
Danke aber nochmals!
LG