erstellt ein Programm, um den Benutzer zur Eingabe von Testergebnissen und die Suche nach dem Durchschnitt, höchste und sortieren Sie die Testergebnisse. Ich habe Probleme, ich kann nicht scheinen, um die höchsten Testergebnisse von der Funktion zu erhalten, die ich gemacht habe, und als ich die sortierende Testergebnisfunktion schuf, stürzt das Programm ab. Kann jemand meine Funktionen anschauen und sehen, wo mein Problem liegt, scheint mir der durchschnittliche Testergebnis zu zeigen.C++ höchste Punktzahl erhalten und Zeiger verwenden
double getAverage(double*, int);
double gethighest(double*, int);
double getlowest(double*, int);
void arrayAscending(double*, int);
int main()
{
// Variable declarations
double *testScores; // Dynamically allocate an array of test scores
double average, highest, lowest;
int numTests; // Number of test scores
int count; // Counter variable
// Get the number of tests
cout << "How many tests do you wish to process? ";
cin >> numTests;
// Verify input is not a negative number
while (numTests <= 0)
{
// Get input again.
cout << "Please enter a positive number: ";
cin >> numTests;
}
// Dynamically allocate an array large enough to hold the test scores
testScores = new double[numTests];
// Get the specified number of test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> *(testScores + count);
// Verify input is not a negative number
while (*(testScores + count) < 0)
{
// Get input again.
cout << "Please enter a valid test score.\n";
cin >> *(testScores + count);
}
}
// Calculate the average test score
average = getAverage(testScores, numTests);
highest = gethighest(testScores, numTests);
lowest = getlowest(testScores, numTests);
// Display the results.
arrayAscending(testScores, numTests);
cout << endl;
cout << "The average of those scores is: " << average << endl;
cout << "The higest of those scores is: " << highest << endl;
cout << "The lowest of those scores is: " << lowest << endl;
// Free dynamically allocated memory
delete[] testScores;
testScores = 0; // Make testScores point to null
return 0;
}
//function getAverage - calculates the average of the test scores
double getAverage(double* scores, int num)
{
double avg;
double total = 0.0;
for (int count = 0; count < num; count++)
{
total += scores[count];
}
avg = total/num;
return avg;
}
double gethighest(double* scores, int num)
{
double highnum = 0.0;
for (int i = 0; i < num; i++)
{
if (scores[i] > highnum)
highnum = scores[i];
}
return highnum;
}
double getlowest(double* scores, int num)
{
double lowestnum = 100;
for (int i = 0; i < num; i++)
{
if (scores[i] < lowestnum)
lowestnum = scores[i];
}
return lowestnum;
}
void arrayAscending(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ((array[index]) < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
danke für Ihre Hilfe können Sie auf meinen aktualisierten Code zu sehen lief es keine Fehler, aber Array nicht angezeigt @vsoftco –
@MikeShasaco Funktioniert gut auf meiner Seite. Führen Sie es von einem Terminal aus und Sie werden das Ergebnis sehen. Siehe [hier] (http://coliru.stacked-crooked.com/a/ec36b87e770cb441). – vsoftco
Ich sehe die Ergebnisse der niedrigsten, höchsten und durchschnittlichen, aber ich sehe nicht die Ergebnisse aus dem ArrayAscending –