Ниже мой код: он работает хорошо, но как сделать его средним все баллы, введенные пользователем, но сбросить самый низкий счет до вычисления? Я вставил какой-то код, но я считаю его неправильным, потому что я не могу заставить его работать правильно. Есть ли более короткий, самый простой способ написать этот код? Или лучший способ написать его? Спасибо.Как мне получить мою программу для вычисления среднего падения самого низкого балла?
#include <iostream>
#include <iomanip>
using namespace std;
//void sortArray(double arr[], int numTest, int scoreNum);
//void displayArray(double arr[], int numTest, int scoreNum);
int main()
{
//To dynamically allocate an array, Accumulator, to hold the average scores.
double *score;
double total = 0;
double average;
//int for counter, to hold the number of test scores.
int count;
int numTest;
// To obtain the number of test scores the user would like to enter.
cout << "How many test scores would you like to enter? " << endl;
cin >> numTest;
//Dynamically allocates an array large enough to hold the amount of test scores to enter.
score = new double[numTest];
//Get the test scores.
cout << "Enter the test score desired. " << endl;
for (count = 0; count < numTest; count++)
{
cout << "Score " << (count + 1) << ": ";
cin >> score[count];
}
//sortArray(score, numTest, scoreNum); (Need to get this part to work)
//displayArray(score, numTest, scoreNum); (Need to get this part to work)
//Calculate the total test scores.
for (count = 0; count < numTest; count++)
{
total += score[count];
}
//Calculate the test scores average minus the lowest score. (I need help here - is this how I drop the lowest score?)
average = total/numTest;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Test Scores Average with the lowest dropped is: " << average << endl;
//Free dynamically allocated memory
delete [] score;
score = 0; // Makes score point to null.
system("pause");
return 0;
}
/* void sortArray(double arr[], int numTest, int scoreNum)
{
double num = 0;
int posNum = 0;
for (int i = 0; i < numTest; i++)
{
for (int x = (i + 1); x < numTest; x++)
{
if (arr[i] > arr[x])
{
num = score[i];
posNum = scoreNum[i];
arr[i] = arr[x];
scoreNum[i] = scoreNum[x];
arr[x] = num;
scoreNum[x] = posNum;
}
}
}
}
void displayArray(double arr[], int numTest, int scoreNum)
{
double average = 0.0;
double sum = 0.0;
int x;
for (int i = 0; i < numTest; i++)
{
for (x = 1; x < numTest; x++)
{
sum += arr[x];
}
}
average = sum(numTest - 1);
cout << fixed << showpoint << setprecision(2) << endl;
cout << "The average of all test scores dropping the lowest is: " << average << endl;
}
*/
Какие проблемы вы с с кодом? – Xymostech
Сортировка кажется излишним для поиска наименьшего значения. Измените оценку как вектор, затем сделайте следующее: double minScore = 1e6; int minScoreIndex; for (count = 0; count score [count]) {minScore = score [count]; minScoreIndex = кол; }} оценка.erase (оценка.begin() + minScoreIndex); –
DigitalGhost
Мне нужно, чтобы он сбросил самый низкий балл, а затем вычислил его. Прямо сейчас я могу получить его, чтобы рассчитать общие баллы, не снижая самый низкий балл. Я немного неясен относительно того, как правильно записать функцию, чтобы сбросить самый низкий балл. Я написал что-то вниз (что я прокомментировал внизу), но я не получаю этого, чтобы работать вообще. Могу ли я использовать что-то вроде: voidFindLowest ..., а затем average = total/(numTest - lower)? Или у меня должен быть массив сортировки, а затем найти самый низкий, а затем удалить его и отобразить? – user1787078